Skip to content

Commit

Permalink
fix(core): CHECKOUT-3012 Avoid loading the same script twice
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchin committed Mar 15, 2018
1 parent 67a1b46 commit 871ae47
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/create-script-loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ScriptLoader from './script-loader';

export default function createScriptLoader(): ScriptLoader {
return new ScriptLoader(document);
return new ScriptLoader();
}
12 changes: 12 additions & 0 deletions src/get-script-loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import getScriptLoader from './get-script-loader';
import ScriptLoader from './script-loader';

describe('getScriptLoader()', () => {
it('returns same `ScriptLoader` instance', () => {
const loader = getScriptLoader();
const loader2 = getScriptLoader();

expect(loader).toBe(loader2);
expect(loader).toBeInstanceOf(ScriptLoader);
});
});
12 changes: 12 additions & 0 deletions src/get-script-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ScriptLoader from './script-loader';
import createScriptLoader from './create-script-loader';

let instance: ScriptLoader;

export default function getScriptLoader(): ScriptLoader {
if (!instance) {
instance = createScriptLoader();
}

return instance;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as ScriptLoader } from './script-loader';
export { default as createScriptLoader } from './create-script-loader';
export { default as getScriptLoader } from './get-script-loader';
9 changes: 8 additions & 1 deletion src/script-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('ScriptLoader', () => {
element.onreadystatechange(new Event('readystatechange'))
);

loader = new ScriptLoader(document);
loader = new ScriptLoader();
});

afterEach(() => {
Expand Down Expand Up @@ -43,4 +43,11 @@ describe('ScriptLoader', () => {
expect(output).toBeInstanceOf(Event);
}
});

it('does not load same script twice', async () => {
await loader.loadScript('https://code.jquery.com/jquery-3.2.1.min.js');
await loader.loadScript('https://code.jquery.com/jquery-3.2.1.min.js');

expect(document.body.appendChild).toHaveBeenCalledTimes(1);
});
});
26 changes: 14 additions & 12 deletions src/script-loader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
export default class ScriptLoader {
constructor(
private _document: Document
) {}
private _scripts: { [key: string]: Promise<Event> } = {};

loadScript(src: string): Promise<Event> {
return new Promise((resolve, reject) => {
const script = this._document.createElement('script') as LegacyHTMLScriptElement;
if (!this._scripts[src]) {
this._scripts[src] = new Promise((resolve, reject) => {
const script = document.createElement('script') as LegacyHTMLScriptElement;

script.onload = (event) => resolve(event);
script.onreadystatechange = (event) => resolve(event);
script.onerror = (event) => reject(event);
script.async = true;
script.src = src;
script.onload = (event) => resolve(event);
script.onreadystatechange = (event) => resolve(event);
script.onerror = (event) => reject(event);
script.async = true;
script.src = src;

this._document.body.appendChild(script);
});
document.body.appendChild(script);
});
}

return this._scripts[src];
}
}

Expand Down

0 comments on commit 871ae47

Please sign in to comment.