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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | export interface ScriptLoaderConfig {
/**
* A function used to verify the script has successfully loaded.
*/
test: () => boolean;
/**
* Attributes to be added to the <script> tag.
*/
attrs?: Record<string, string>;
/**
* Timeout, in ms. Defaults to 5000ms.
*/
timeout?: number;
}
/**
* A script loader that checks to verify if the script has loaded by repeatedly
* calling a test method using RAF.
*
* Example loading the YT API:
*
* ```
* const scriptLoader = new RAFScriptLoader();
* scriptLoader.load('https://www.youtube.com/iframe_api', {
* test: () => window.YT && window.YT['loaded'] === 1,
* }).then(() => {
* // The YT API should be loaded now.
* const player = new YT.Player(...);
* });
* ```
*/
export class ScriptLoader {
private loadedScripts: Record<string, Promise<void>> = {};
private disposed = false;
/**
* Loads a script element onto the page.
*
* @param url The script src.
* @param test A function used to verify the script has successfully loaded.
* @param options Config options for loading the script.
*/
public load(url: string, options: ScriptLoaderConfig): Promise<void> {
// Avoid repeated calls to the same URL by returning the previous promise.
const previousPromise = this.loadedScripts[url];
Iif (previousPromise) {
return previousPromise;
}
const promise: Promise<void> = new Promise((resolve, reject) => {
// Avoid adding the script to the page if the test fn already passes.
Iif (options.test()) {
resolve();
return;
}
// Render the <script> tag to the DOM.
this.renderScriptTag(url, options);
// Using RAF, repeatedly check if the script has been loaded.
const startTime = Date.now();
const timeout = options.timeout || 5000;
const callback = () => {
Iif (this.disposed) {
reject('script loader is disposed');
return;
}
Iif (options.test()) {
resolve();
return;
}
const elapsed = Date.now() - startTime;
Iif (elapsed > timeout) {
reject(`failed to load ${url} due to timeout`);
return;
}
window.requestAnimationFrame(callback);
};
window.requestAnimationFrame(callback);
});
this.loadedScripts[url] = promise;
promise.catch((err) => {
console.error(`failed to load ${url}`);
console.error(err);
delete this.loadedScripts[url];
});
return promise;
}
/**
* Renders the DOM for the script element. Subclasses should override this
* method for more complex script renderings.
*/
protected renderScriptTag(url: string, options?: ScriptLoaderConfig): void {
const script = document.createElement('script');
Iif (options?.attrs) {
for (const key in options.attrs) {
const val = options.attrs[key];
script.setAttribute(key, val);
}
}
script.src = url;
document.body.appendChild(script);
}
/** @deprecated */
protected renderDom(url: string): void {
// Deprecated. To be removed in a future version.
this.renderScriptTag(url);
}
dispose() {
this.loadedScripts = {};
this.disposed = true;
}
}
|