Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 94x 94x 94x 231x 231x 235x 235x 231x 231x 4x 231x 227x 4x 4x 4x 4x 4x 94x 94x 94x 95x 94x 93x 94x 94x 94x 94x 2959x 2959x 2959x 2959x 2959x 94x 95x 95x 95x 95x 94x 129x 129x 34x 34x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 94x 94x 94x 94x 94x 94x | /** * @module Utils */ import * as MH from "@lisn/globals/minification-helpers"; import { settings } from "@lisn/globals/settings"; /** * Returns a Promise that is resolved when the given `checkFn` function returns * a value other than `null` or `undefined`. * * The Promise is resolved with `checkFn`'s return value. * * The function is called initially, and then every time there are changes to * the DOM children. Uses * {@link https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | MutationObserver}. * * @param timeout If given, then if no such element is present after this many * milliseconds, the promise will resolve to `null`. * * @category DOM: Events */ export function waitForElement<F>(checkFn: () => F): Promise<NonNullable<F>>; export function waitForElement<F>( checkFn: () => F, timeout: number, ): Promise<null | NonNullable<F>>; export function waitForElement(checkFn: () => unknown, timeout?: number) { return MH.newPromise((resolve) => { const callFn = () => { const result = checkFn(); if (!MH.isNullish(result)) { resolve(result); return true; // done } return false; }; if (callFn()) { return; // resolved already } Iif (!MH.isNullish(timeout)) { setTimeout(() => { resolve(null); observer.disconnect(); }, timeout); } const observer = new MutationObserver(() => { if (callFn()) { observer.disconnect(); } }); observer.observe(document.documentElement, { childList: true, subtree: true, }); }); } /** * Returns a Promise that is resolved when the given `checkFn` function returns * a value other than `null` or `undefined` or the * {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState | Document:readyState} * becomes "interactive". * * It always calls the given `checkFn` first before examining the `readyState`. * * If the `readyState` became interactive before the element was found, the * Promise resolves to `null`. Otherwise the Promise is resolved with `checkFn`'s * return value. * * The function is called initially, and then every time there are changes to * the DOM children. Uses * {@link https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver | MutationObserver}. * * @category DOM: Events */ export const waitForElementOrInteractive = <F>(checkFn: () => F) => MH.newPromise<NonNullable<true | F> | null>((resolve) => { let isInteractive = false; // Check element first, then readyState. The callback to waitForElement is // run synchronously first time, so isInteractive will be false and checkFn // will run. waitForElement(() => isInteractive || checkFn()).then((res) => { if (!isInteractive) { resolve(res); } // else already resolved to null }); waitForInteractive().then(() => { isInteractive = true; resolve(null); }); }); /** * Returns a Promise that is resolved when the * {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState | Document:readyState} * is "interactive" (or if it's already "interactive" or "complete", the * Promise is fulfilled immediately). * * @category DOM: Events */ export const waitForInteractive = () => MH.newPromise<void>((resolve) => { const readyState = MH.getReadyState(); if (readyState === INTERACTIVE || readyState === COMPLETE) { resolve(); return; } MH.getDoc().addEventListener("DOMContentLoaded", () => resolve()); }); /** * Returns a Promise that is resolved when the * {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState | Document:readyState} * is "complete" (or if it's already "complete", the Promise is fulfilled * immediately). * * @category DOM: Events */ export const waitForComplete = () => MH.newPromise<void>((resolve) => { if (MH.getReadyState() === COMPLETE) { resolve(); return; } MH.getDoc().addEventListener("readystatechange", () => { Iif (MH.getReadyState() === COMPLETE) { resolve(); } }); }); /** * Returns a Promise that is resolved either when the * {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState | Document:readyState} * is "complete" or the `readyState` is "interactive" and at least * {@link settings.pageLoadTimeout} milliseconds have passed (if > 0) since it * became "interactive". * * @category DOM: Events */ export const waitForPageReady = () => MH.newPromise<void>((resolve) => { if (pageIsReady) { resolve(); return; } return waitForInteractive().then(() => { // Setup a listener for the complete state but wait at most // <pageLoadTimeout> (if specified) let timer: ReturnType<typeof setTimeout> | null = null; const dispatchReady = () => { pageIsReady = true; if (timer) { MH.clearTimer(timer); timer = null; } resolve(); }; if (settings.pageLoadTimeout > 0) { timer = MH.setTimer(() => { dispatchReady(); }, settings.pageLoadTimeout); } waitForComplete().then(dispatchReady); }); }); /** * Returns true if the page is "ready". See {@link waitForPageReady}. * * @category DOM: Events */ export const isPageReady = () => pageIsReady; // -------------------- const COMPLETE = "complete"; const INTERACTIVE = "interactive"; let pageIsReady = false; Iif (!MH.hasDOM()) { pageIsReady = true; } else { waitForPageReady(); // ensure pageIsReady is set even if waitForPageReady is not called } |