@internetarchive/lazy-loader-service
Advanced tools
Comparing version
@@ -88,3 +88,8 @@ import { __awaiter } from "tslib"; | ||
else { | ||
this.emitter.emit('scriptLoadFailed', options.src, error); | ||
// only emit a failure event from the last attempt, which has not been retried. | ||
// otherwise you get failure events from each script tag, when we're really | ||
// only interested that the entire chain failed | ||
if (!hasBeenRetried) { | ||
this.emitter.emit('scriptLoadFailed', options.src, error); | ||
} | ||
originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error); | ||
@@ -91,0 +96,0 @@ reject(error); |
@@ -159,2 +159,36 @@ import { __awaiter } from "tslib"; | ||
})); | ||
it('Emits the expected number of retry events', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const container = (yield fixture(html ` <div></div> `)); | ||
const lazyLoader = new LazyLoaderService({ | ||
container, | ||
retryCount: 4, | ||
retryInterval: 0.01, | ||
}); | ||
let retryEvents = 0; | ||
lazyLoader.on('scriptLoadRetried', () => { | ||
retryEvents += 1; | ||
}); | ||
try { | ||
yield lazyLoader.loadScript({ src: '/base/test/blahblah.js' }); | ||
} | ||
catch (_a) { } | ||
expect(retryEvents).to.equal(4); | ||
})); | ||
it('Only emits a single failure event if there are multiple retry attempts', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const container = (yield fixture(html ` <div></div> `)); | ||
const lazyLoader = new LazyLoaderService({ | ||
container, | ||
retryCount: 4, | ||
retryInterval: 0.01, | ||
}); | ||
let failureEvents = 0; | ||
lazyLoader.on('scriptLoadFailed', () => { | ||
failureEvents += 1; | ||
}); | ||
try { | ||
yield lazyLoader.loadScript({ src: '/base/test/blahblah.js' }); | ||
} | ||
catch (_b) { } | ||
expect(failureEvents).to.equal(1); | ||
})); | ||
it('Retries the specified number of times', () => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -170,3 +204,3 @@ const container = (yield fixture(html ` <div></div> `)); | ||
} | ||
catch (_a) { } | ||
catch (_c) { } | ||
const scriptTags = container.querySelectorAll('script'); | ||
@@ -173,0 +207,0 @@ expect(scriptTags.length).to.equal(6); |
{ | ||
"name": "@internetarchive/lazy-loader-service", | ||
"version": "0.2.0-alpha.1", | ||
"version": "0.2.0-alpha.2", | ||
"description": "A small library to lazy load javascript with a Promise", | ||
@@ -5,0 +5,0 @@ "license": "AGPL-3.0-only", |
@@ -150,3 +150,8 @@ import { createNanoEvents, Unsubscribe } from 'nanoevents'; | ||
} else { | ||
this.emitter.emit('scriptLoadFailed', options.src, error); | ||
// only emit a failure event from the last attempt, which has not been retried. | ||
// otherwise you get failure events from each script tag, when we're really | ||
// only interested that the entire chain failed | ||
if (!hasBeenRetried) { | ||
this.emitter.emit('scriptLoadFailed', options.src, error); | ||
} | ||
originalOnError?.(error); | ||
@@ -153,0 +158,0 @@ reject(error); |
@@ -186,2 +186,42 @@ import { expect, fixture, html } from '@open-wc/testing'; | ||
it('Emits the expected number of retry events', async () => { | ||
const container = (await fixture(html` <div></div> `)) as HTMLElement; | ||
const lazyLoader = new LazyLoaderService({ | ||
container, | ||
retryCount: 4, | ||
retryInterval: 0.01, | ||
}); | ||
let retryEvents = 0; | ||
lazyLoader.on('scriptLoadRetried', () => { | ||
retryEvents += 1; | ||
}); | ||
try { | ||
await lazyLoader.loadScript({ src: '/base/test/blahblah.js' }); | ||
} catch {} | ||
expect(retryEvents).to.equal(4); | ||
}); | ||
it('Only emits a single failure event if there are multiple retry attempts', async () => { | ||
const container = (await fixture(html` <div></div> `)) as HTMLElement; | ||
const lazyLoader = new LazyLoaderService({ | ||
container, | ||
retryCount: 4, | ||
retryInterval: 0.01, | ||
}); | ||
let failureEvents = 0; | ||
lazyLoader.on('scriptLoadFailed', () => { | ||
failureEvents += 1; | ||
}); | ||
try { | ||
await lazyLoader.loadScript({ src: '/base/test/blahblah.js' }); | ||
} catch {} | ||
expect(failureEvents).to.equal(1); | ||
}); | ||
it('Retries the specified number of times', async () => { | ||
@@ -188,0 +228,0 @@ const container = (await fixture(html` <div></div> `)) as HTMLElement; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
403498
1.44%6029
1.28%