Skip to content

fix: loader must not reset if successfully loaded #106

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 2 commits into from
Nov 17, 2020
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
18 changes: 15 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ test("script onerror should not reset retry mechanism with parallel loaders", as
expect(console.log).toHaveBeenCalledTimes(loader.retries);
});

test("loader should not reset retry mechanism if successfully loaded", () => {
const loader = new Loader({ apiKey: "foo", retries: 0 });
const deleteScript = jest.spyOn(loader, "deleteScript");

loader["done"] = true;
expect(loader.load()).resolves.toBeUndefined();

expect(loader["done"]).toBeTruthy();
expect(loader["loading"]).toBeFalsy();
expect(deleteScript).toHaveBeenCalledTimes(0);
});

test("singleton should be used", () => {
const loader = new Loader({ apiKey: "foo" });
const extraLoader = new Loader({ apiKey: "foo" });
Expand All @@ -231,23 +243,23 @@ test("singleton should throw with different options", () => {

test("loader should resolve immediately when successfully loaded", async () => {
// use await/async pattern since the promise resolves without trigger
const loader = new Loader({ apiKey: "foo" });
const loader = new Loader({ apiKey: "foo", retries: 0 });
loader["done"] = true;

await expect(loader.loadPromise()).resolves.toBeUndefined();
});

test("loader should resolve immediately when failed loading", async () => {
// use await/async pattern since the promise rejects without trigger
const loader = new Loader({ apiKey: "foo" });
const loader = new Loader({ apiKey: "foo", retries: 0 });
loader["done"] = true;
loader["onerrorEvent"] = document.createEvent("ErrorEvent");

await expect(loader.loadPromise()).rejects.toBeDefined();
});

test("loader should wait if already loading", () => {
const loader = new Loader({ apiKey: "foo" });
const loader = new Loader({ apiKey: "foo", retries: 0 });
loader["loading"] = true;

loader.load();
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ export class Loader {
}

private resetIfRetryingFailed(): void {
if (this.done && !this.loading && this.errors.length >= this.retries) {
const possibleAttempts = this.retries + 1;
if (this.done && !this.loading && this.errors.length >= possibleAttempts) {
this.deleteScript();
this.done = false;
this.loading = false;
Expand Down