Skip to content

fix: move warning and return if already defined #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ test("loader should resolve immediately when google.maps defined", async () => {
expect(console.warn).toHaveBeenCalledTimes(1);
});

test("loader should not warn if done and google.maps is defined", async () => {
const loader = new Loader({ apiKey: "foo" });
loader["done"] = true;
window.google = { maps: { version: "3.*.*" } as any };
console.warn = jest.fn();
await expect(loader.loadPromise()).resolves.toBeUndefined();
delete window.google;
expect(console.warn).toHaveBeenCalledTimes(0);
});

test("deleteScript removes script tag from head", () => {
const loader = new Loader({ apiKey: "foo" });
loader["setScript"]();
Expand Down
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,21 @@ export class Loader {
}

private execute(): void {
if (window.google && window.google.maps && window.google.maps.version) {
console.warn(
"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();
}

this.resetIfRetryingFailed();

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." +
"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 {
Expand Down