Skip to content

Commit 47a4239

Browse files
committed
feat: add status getter
1 parent 9945c5c commit 47a4239

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/index.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
/* eslint @typescript-eslint/no-explicit-any: 0 */
17-
import { DEFAULT_ID, Loader, LoaderOptions } from ".";
17+
import { DEFAULT_ID, Loader, LoaderOptions, LoaderStatus } from ".";
1818

1919
jest.useFakeTimers();
2020

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

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

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

302304
loader["done"] = true;
303305
expect(extraLoader["done"]).toBe(loader["done"]);
306+
expect(loader.status).toBe(LoaderStatus.SUCCESS);
304307
});
305308

306309
test("singleton should throw with different options", () => {
@@ -333,7 +336,7 @@ test("loader should resolve immediately when failed loading", async () => {
333336
test("loader should wait if already loading", () => {
334337
const loader = new Loader({ apiKey: "foo", retries: 0 });
335338
loader["loading"] = true;
336-
339+
expect(loader.status).toBe(LoaderStatus.LOADING);
337340
loader.load();
338341
});
339342

src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ export interface LoaderOptions {
165165
retries?: number;
166166
}
167167

168+
/**
169+
* The status of the [[Loader]].
170+
*/
171+
export enum LoaderStatus {
172+
INITIALIZED,
173+
LOADING,
174+
SUCCESS,
175+
FAILURE,
176+
}
177+
168178
/**
169179
* [[Loader]] makes it easier to add Google Maps JavaScript API to your application
170180
* dynamically using
@@ -314,6 +324,19 @@ export class Loader {
314324
};
315325
}
316326

327+
public get status(): LoaderStatus {
328+
if (this.errors.length) {
329+
return LoaderStatus.FAILURE;
330+
}
331+
if (this.done) {
332+
return LoaderStatus.SUCCESS;
333+
}
334+
if (this.loading) {
335+
return LoaderStatus.LOADING;
336+
}
337+
return LoaderStatus.INITIALIZED;
338+
}
339+
317340
private get failed(): boolean {
318341
return this.done && !this.loading && this.errors.length >= this.retries + 1;
319342
}

0 commit comments

Comments
 (0)