Skip to content
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
48 changes: 48 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ test("script onerror should reject promise with multiple loaders", async () => {
expect(loader["done"]).toBeTruthy();
expect(loader["loading"]).toBeFalsy();
expect(loader["onerrorEvent"]).toBeInstanceOf(ErrorEvent);

rejection = expect(extraLoader.load()).rejects.toBeInstanceOf(ErrorEvent);
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));

await rejection;
expect(extraLoader["done"]).toBeTruthy();
Expand All @@ -165,6 +167,52 @@ test("script onerror should retry", async () => {
expect(console.log).toHaveBeenCalledTimes(loader.retries);
});

test("script onerror should reset retry mechanism with next loader", async () => {
const loader = new Loader({ apiKey: "foo", retries: 1 });
const deleteScript = jest.spyOn(loader, "deleteScript");
// eslint-disable-next-line @typescript-eslint/no-empty-function
console.log = jest.fn();

let rejection = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
jest.runAllTimers();
await rejection;

rejection = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
expect(loader["done"]).toBeFalsy();
expect(loader["loading"]).toBeTruthy();
expect(loader["errors"].length).toBe(0);

loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
jest.runAllTimers();

await rejection;
expect(deleteScript).toHaveBeenCalledTimes(3);
expect(console.log).toHaveBeenCalledTimes(loader.retries * 2);
});

test("script onerror should not reset retry mechanism with parallel loaders", async () => {
const loader = new Loader({ apiKey: "foo", retries: 1 });
const deleteScript = jest.spyOn(loader, "deleteScript");
// eslint-disable-next-line @typescript-eslint/no-empty-function
console.log = jest.fn();

const rejection1 = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
const rejection2 = expect(loader.load()).rejects.toBeInstanceOf(ErrorEvent);
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
loader["loadErrorCallback"](document.createEvent("ErrorEvent"));
jest.runAllTimers();

await Promise.all([rejection1, rejection2]);
expect(loader["done"]).toBeTruthy();
expect(loader["loading"]).toBeFalsy();
expect(loader["errors"].length).toBe(2);
expect(deleteScript).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledTimes(loader.retries);
});

test("singleton should be used", () => {
const loader = new Loader({ apiKey: "foo" });
const extraLoader = new Loader({ apiKey: "foo" });
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ export class Loader {
}
}

private resetIfRetryingFailed(): void {
if (this.done && !this.loading && this.errors.length >= this.retries) {
this.deleteScript();
this.done = false;
this.loading = false;
this.errors = [];
}
}

private loadErrorCallback(e: ErrorEvent): void {
this.errors.push(e);

Expand Down Expand Up @@ -470,6 +479,7 @@ export class Loader {
this.callback();
}

this.resetIfRetryingFailed();
if (this.done) {
this.callback();
} else {
Expand Down