|
| 1 | + |
| 2 | +export class DelayedScriptLoader { |
| 3 | + |
| 4 | + private delayInMilliseconds: number; |
| 5 | + private scriptPromise: Promise<void> | null; |
| 6 | + private urls: string[]; |
| 7 | + |
| 8 | + // I initialize the delayed script loader service. |
| 9 | + constructor( urls: string[], delayInMilliseconds: number ); |
| 10 | + constructor( urls: string, delayInMilliseconds: number ); |
| 11 | + constructor( urls: any, delayInMilliseconds: number ) { |
| 12 | + |
| 13 | + this.delayInMilliseconds = delayInMilliseconds; |
| 14 | + this.scriptPromise = null; |
| 15 | + this.urls = Array.isArray( urls ) |
| 16 | + ? urls |
| 17 | + : [ urls ] |
| 18 | + ; |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + // --- |
| 23 | + // PUBLIC METHODS. |
| 24 | + // --- |
| 25 | + |
| 26 | + // I load the the underlying Script tags. Returns a Promise. |
| 27 | + public load() : Promise<void> { |
| 28 | + |
| 29 | + // If we've already configured the script request, just return it. This will |
| 30 | + // naturally queue-up the requests until the script is resolved. |
| 31 | + if ( this.scriptPromise ) { |
| 32 | + |
| 33 | + return( this.scriptPromise ); |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + // By using a Promise-based workflow to manage the deferred script loading, |
| 38 | + // requests will naturally QUEUE-UP IN-MEMORY (not a concern) until the delay has |
| 39 | + // passed and the remote-scripts have been loaded. In this case, we're not even |
| 40 | + // going to load the remote-scripts until they are requested FOR THE FIRST TIME. |
| 41 | + // Then, we will use they given delay, after which the in-memory queue will get |
| 42 | + // flushed automatically - Promises rock!! |
| 43 | + this.scriptPromise = this.delay( this.delayInMilliseconds ) |
| 44 | + .then( |
| 45 | + () => { |
| 46 | + |
| 47 | + var scriptPromises = this.urls.map( |
| 48 | + ( url ) => { |
| 49 | + |
| 50 | + return( this.loadScript( url ) ); |
| 51 | + |
| 52 | + } |
| 53 | + ); |
| 54 | + |
| 55 | + return( Promise.all( scriptPromises ) ); |
| 56 | + |
| 57 | + } |
| 58 | + ) |
| 59 | + .then( |
| 60 | + () => { |
| 61 | + |
| 62 | + // No-op to generate a Promise<void> from the Promise<Any[]>. |
| 63 | + |
| 64 | + } |
| 65 | + ) |
| 66 | + ; |
| 67 | + |
| 68 | + return( this.scriptPromise ); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + // --- |
| 73 | + // PRIVATE METHODS. |
| 74 | + // --- |
| 75 | + |
| 76 | + // I return a Promise that resolves after the given delay. |
| 77 | + private delay( delayInMilliseconds: number ) : Promise<any> { |
| 78 | + |
| 79 | + var promise = new Promise( |
| 80 | + ( resolve ) => { |
| 81 | + |
| 82 | + setTimeout( resolve, delayInMilliseconds ); |
| 83 | + |
| 84 | + } |
| 85 | + ); |
| 86 | + |
| 87 | + return( promise ); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + // I inject a Script tag with the given URL into the head. Returns a Promise. |
| 93 | + private loadScript( url: string ) : Promise<any> { |
| 94 | + |
| 95 | + var promise = new Promise( |
| 96 | + ( resolve, reject ) => { |
| 97 | + |
| 98 | + var commentNode = document.createComment( " Script injected via DelayedScriptLoader. " ); |
| 99 | + |
| 100 | + var scriptNode = document.createElement( "script" ); |
| 101 | + scriptNode.type = "text/javascript"; |
| 102 | + scriptNode.onload = resolve; |
| 103 | + scriptNode.onerror = reject; |
| 104 | + scriptNode.src = url; |
| 105 | + |
| 106 | + document.head.appendChild( commentNode ); |
| 107 | + document.head.appendChild( scriptNode ); |
| 108 | + |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + return( promise ); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments