Skip to content

Commit f24a1ae

Browse files
authored
Fix: Fix a timing issue for sequential importLibrary calls (#843)
#809 Fixes #809
1 parent 959ea63 commit f24a1ae

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/index.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,23 @@ test("importLibrary resolves correctly", async () => {
425425
expect(core).toEqual({ core: "fake" });
426426
});
427427

428+
test("importLibrary resolves correctly without warning with sequential await", async () => {
429+
console.warn = jest.fn();
430+
window.google = { maps: {} } as any;
431+
google.maps.importLibrary = async (name) => {
432+
google.maps.version = "3.*.*";
433+
return { [name]: "fake" } as any;
434+
};
435+
436+
const loader = new Loader({ apiKey: "foo" });
437+
const core = await loader.importLibrary("core");
438+
const marker = await loader.importLibrary("marker");
439+
440+
expect(console.warn).toHaveBeenCalledTimes(0);
441+
expect(core).toEqual({ core: "fake" });
442+
expect(marker).toEqual({ marker: "fake" });
443+
});
444+
428445
test("importLibrary can also set up bootstrap libraries (if bootstrap libraries empty)", async () => {
429446
const loader = new Loader({ apiKey: "foo" });
430447
loader.importLibrary("marker");

src/index.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -623,26 +623,26 @@ export class Loader {
623623
private execute(): void {
624624
this.resetIfRetryingFailed();
625625

626+
if (this.loading) {
627+
// do nothing but wait
628+
return;
629+
}
630+
626631
if (this.done) {
627632
this.callback();
628633
} else {
629634
// short circuit and warn if google.maps is already loaded
630635
if (window.google && window.google.maps && window.google.maps.version) {
631636
console.warn(
632-
"Google Maps already loaded outside @googlemaps/js-api-loader." +
637+
"Google Maps already loaded outside @googlemaps/js-api-loader. " +
633638
"This may result in undesirable behavior as options and script parameters may not match."
634639
);
635640
this.callback();
636641
return;
637642
}
638643

639-
if (this.loading) {
640-
// do nothing but wait
641-
} else {
642-
this.loading = true;
643-
644-
this.setScript();
645-
}
644+
this.loading = true;
645+
this.setScript();
646646
}
647647
}
648648
}

0 commit comments

Comments
 (0)