Skip to content

Commit

Permalink
Fix: Fix a timing issue for sequential importLibrary calls (#843)
Browse files Browse the repository at this point in the history
#809

Fixes #809
  • Loading branch information
chrisjshull authored Apr 26, 2024
1 parent 959ea63 commit f24a1ae
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,23 @@ test("importLibrary resolves correctly", async () => {
expect(core).toEqual({ core: "fake" });
});

test("importLibrary resolves correctly without warning with sequential await", async () => {
console.warn = jest.fn();
window.google = { maps: {} } as any;
google.maps.importLibrary = async (name) => {
google.maps.version = "3.*.*";
return { [name]: "fake" } as any;
};

const loader = new Loader({ apiKey: "foo" });
const core = await loader.importLibrary("core");
const marker = await loader.importLibrary("marker");

expect(console.warn).toHaveBeenCalledTimes(0);
expect(core).toEqual({ core: "fake" });
expect(marker).toEqual({ marker: "fake" });
});

test("importLibrary can also set up bootstrap libraries (if bootstrap libraries empty)", async () => {
const loader = new Loader({ apiKey: "foo" });
loader.importLibrary("marker");
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,26 +623,26 @@ export class Loader {
private execute(): void {
this.resetIfRetryingFailed();

if (this.loading) {
// do nothing but wait
return;
}

if (this.done) {
this.callback();
} else {
// short circuit and warn if google.maps is already loaded
if (window.google && window.google.maps && window.google.maps.version) {
console.warn(
"Google Maps already loaded outside @googlemaps/js-api-loader." +
"Google Maps already loaded outside @googlemaps/js-api-loader. " +
"This may result in undesirable behavior as options and script parameters may not match."
);
this.callback();
return;
}

if (this.loading) {
// do nothing but wait
} else {
this.loading = true;

this.setScript();
}
this.loading = true;
this.setScript();
}
}
}

0 comments on commit f24a1ae

Please sign in to comment.