Repository: googlemaps/js-api-loader (ts)

Issue:
Loader reports warning when using importLibrary multiple times
When loading the maps library followed by another library using `loader.importLibrary()` and async/await syntax, a misleading warning-message `"Google Maps already loaded outside @googlemaps/js-api-loader.This may result in undesirable behavior as options and script parameters may not match."` is written to the console.

Here's the code that triggers this behavior:

```typescript
import Loader from '@googlemaps/js-api-loader';

async function main() {
  const loader = new Loader({apiKey: 'MY_API_KEY'});

  const {Map} = await loader.importLibrary('maps');
  
  // this line will trigger the warning message:
  await loader.importLibrary('marker');
}
```

The reason appears to be that the `done` flag isn't correctly set to `true` after the maps-api has initially been loaded.

Pull request:
Fix: a timing issue for sequential importLibrary calls
https://github.com/googlemaps/js-api-loader/issues/809

Thank you for opening a Pull Request!

---

Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
- [x] Make sure to open a GitHub issue as a bug/feature request before writing your code!  That way we can discuss the change, evaluate designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)

Fixes #809


Patch:
diff --git a/src/index.ts b/src/index.ts
index 546f5c1..a1f91d5 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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();
     }
   }
 }


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index 7ccc942..3fd017d 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -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");