Skip to content

Commit 70f0cdc

Browse files
authored
feat: change to singleton and fix resolve error (#91)
1 parent adda1d6 commit 70f0cdc

File tree

7 files changed

+232
-25
lines changed

7 files changed

+232
-25
lines changed

examples/index.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script
3737
nonce="caffe67d7b989af3a1c7f4a1a6c79bd9fb2b4eb0"
3838
type="text/javascript"
39-
src="../dist/index.dev.js"
39+
src="../dist/index.umd.js"
4040
></script>
4141

4242
<script
@@ -76,6 +76,22 @@
7676
// new google.maps.Map(document.getElementById("map"), mapOptions);
7777
}
7878
});
79+
80+
// An error is thrown when instantiating loader with new options
81+
try {
82+
new google.maps.plugins.loader.Loader({apiKey: 'foo'});
83+
} catch (e) {
84+
console.log(e.message)
85+
}
86+
87+
// The loader is a singleton and new loaders will resolve with the old
88+
const anotherLoader = new google.maps.plugins.loader.Loader(
89+
loader.options
90+
);
91+
anotherLoader.load().then(() => {
92+
console.log("another loader was used with same options");
93+
});
94+
7995
</script>
8096
</head>
8197

package-lock.json

Lines changed: 119 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
"devDependencies": {
3232
"@babel/preset-env": "^7.9.5",
3333
"@babel/runtime-corejs3": "^7.9.2",
34+
"@rollup/plugin-node-resolve": "^10.0.0",
3435
"@types/googlemaps": "^3.39.3",
3536
"@types/jest": "^26.0.10",
37+
"@types/lodash": "^4.14.165",
3638
"@types/selenium-webdriver": "^4.0.9",
3739
"@typescript-eslint/eslint-plugin": ">=2.25.0",
3840
"@typescript-eslint/parser": ">=2.25.0",
@@ -56,5 +58,8 @@
5658
"publishConfig": {
5759
"access": "public",
5860
"registry": "https://wombat-dressing-room.appspot.com"
61+
},
62+
"dependencies": {
63+
"lodash": "^4.17.20"
5964
}
6065
}

rollup.config.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import babel from "rollup-plugin-babel";
1818
import commonjs from "rollup-plugin-commonjs";
19+
import { nodeResolve } from "@rollup/plugin-node-resolve";
1920
import { terser } from "rollup-plugin-terser";
2021
import typescript from "rollup-plugin-typescript2";
2122

@@ -25,11 +26,16 @@ const babelOptions = {
2526

2627
const terserOptions = { output: { comments: "" } };
2728

29+
const resolveOptions = {
30+
mainFields: ["browser", "jsnext:main", "module", "main"],
31+
};
32+
2833
export default [
2934
{
3035
input: "src/index.ts",
3136
plugins: [
3237
typescript(),
38+
nodeResolve(resolveOptions),
3339
commonjs(),
3440
babel(babelOptions),
3541
terser(terserOptions),
@@ -45,6 +51,7 @@ export default [
4551
input: "src/index.ts",
4652
plugins: [
4753
typescript(),
54+
nodeResolve(resolveOptions),
4855
commonjs(),
4956
babel(babelOptions),
5057
terser(terserOptions),
@@ -57,7 +64,12 @@ export default [
5764
},
5865
{
5966
input: "src/index.ts",
60-
plugins: [typescript(), commonjs(), babel(babelOptions)],
67+
plugins: [
68+
typescript(),
69+
nodeResolve(resolveOptions),
70+
commonjs(),
71+
babel(babelOptions),
72+
],
6173
output: {
6274
file: "dist/index.dev.js",
6375
format: "iife",
@@ -66,7 +78,7 @@ export default [
6678
},
6779
{
6880
input: "src/index.ts",
69-
plugins: [typescript()],
81+
plugins: [typescript(), nodeResolve(resolveOptions), commonjs()],
7082
output: {
7183
file: "dist/index.esm.js",
7284
format: "esm",

src/index.test.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Loader, LoaderOptions } from ".";
1818

1919
afterEach(() => {
2020
document.getElementsByTagName("html")[0].innerHTML = "";
21+
delete Loader["instance"];
2122
});
2223

2324
test.each([
@@ -108,20 +109,50 @@ test("loadCallback callback should fire", () => {
108109
window.__googleMapsCallback(null);
109110
});
110111

111-
test("script onerror should reject promise", () => {
112+
test("script onerror should reject promise", async () => {
112113
const loader = new Loader({ apiKey: "foo" });
113114

114-
expect.assertions(3);
115+
const rejection = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
115116

116-
const promise = loader.load().catch((e) => {
117-
expect(e).toBeTruthy();
118-
expect(loader["done"]).toBeTruthy();
119-
expect(loader["loading"]).toBeFalsy();
120-
});
117+
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
118+
119+
await rejection;
120+
expect(loader["done"]).toBeTruthy();
121+
expect(loader["loading"]).toBeFalsy();
122+
});
123+
124+
test("script onerror should reject promise with multiple loaders", async () => {
125+
const loader = new Loader({ apiKey: "foo" });
126+
const extraLoader = new Loader({ apiKey: "foo" });
121127

128+
let rejection = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
122129
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
123130

124-
return promise;
131+
await rejection;
132+
expect(loader["done"]).toBeTruthy();
133+
expect(loader["loading"]).toBeFalsy();
134+
expect(loader["onerrorEvent"]).toBeInstanceOf(ErrorEvent);
135+
rejection = expect(extraLoader.load()).rejects.toBeInstanceOf(ErrorEvent);
136+
137+
await rejection;
138+
expect(extraLoader["done"]).toBeTruthy();
139+
expect(extraLoader["loading"]).toBeFalsy();
140+
});
141+
142+
test("singleton should be used", () => {
143+
const loader = new Loader({ apiKey: "foo" });
144+
const extraLoader = new Loader({ apiKey: "foo" });
145+
expect(extraLoader).toBe(loader);
146+
147+
loader["done"] = true;
148+
expect(extraLoader["done"]).toBe(loader["done"]);
149+
});
150+
151+
test("singleton should throw with different options", () => {
152+
new Loader({ apiKey: "foo" });
153+
expect(() => {
154+
new Loader({ apiKey: "bar" });
155+
}).toThrowError();
125156
});
126157

127158
test("loader should resolve immediately when successfully loaded", async () => {

0 commit comments

Comments
 (0)