Skip to content

feat: add status getter #488

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
Jan 4, 2022
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
7 changes: 5 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
/* eslint @typescript-eslint/no-explicit-any: 0 */
import { DEFAULT_ID, Loader, LoaderOptions } from ".";
import { DEFAULT_ID, Loader, LoaderOptions, LoaderStatus } from ".";

jest.useFakeTimers();

Expand Down Expand Up @@ -54,6 +54,7 @@ test.each([
])("createUrl is correct", (options: LoaderOptions, expected: string) => {
const loader = new Loader(options);
expect(loader.createUrl()).toEqual(expected);
expect(loader.status).toBe(LoaderStatus.INITIALIZED);
});

test("uses default id if empty string", () => {
Expand Down Expand Up @@ -128,6 +129,7 @@ test("script onerror should reject promise", async () => {
expect(loader["done"]).toBeTruthy();
expect(loader["loading"]).toBeFalsy();
expect(loader["errors"].length).toBe(1);
expect(loader.status).toBe(LoaderStatus.FAILURE);
});

test("script onerror should reject promise with multiple loaders", async () => {
Expand Down Expand Up @@ -301,6 +303,7 @@ test("singleton should be used", () => {

loader["done"] = true;
expect(extraLoader["done"]).toBe(loader["done"]);
expect(loader.status).toBe(LoaderStatus.SUCCESS);
});

test("singleton should throw with different options", () => {
Expand Down Expand Up @@ -333,7 +336,7 @@ test("loader should resolve immediately when failed loading", async () => {
test("loader should wait if already loading", () => {
const loader = new Loader({ apiKey: "foo", retries: 0 });
loader["loading"] = true;

expect(loader.status).toBe(LoaderStatus.LOADING);
loader.load();
});

Expand Down
23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ export interface LoaderOptions {
retries?: number;
}

/**
* The status of the [[Loader]].
*/
export enum LoaderStatus {
INITIALIZED,
LOADING,
SUCCESS,
FAILURE,
}

/**
* [[Loader]] makes it easier to add Google Maps JavaScript API to your application
* dynamically using
Expand Down Expand Up @@ -314,6 +324,19 @@ export class Loader {
};
}

public get status(): LoaderStatus {
if (this.errors.length) {
return LoaderStatus.FAILURE;
}
if (this.done) {
return LoaderStatus.SUCCESS;
}
if (this.loading) {
return LoaderStatus.LOADING;
}
return LoaderStatus.INITIALIZED;
}

private get failed(): boolean {
return this.done && !this.loading && this.errors.length >= this.retries + 1;
}
Expand Down