@internetarchive/lazy-loader-service
Advanced tools
Comparing version 0.2.0-alpha.2 to 0.2.0-alpha.3
import { BundleType } from './bundle-type'; | ||
import { Unsubscribe } from 'nanoevents'; | ||
export interface LazyLoaderServiceEvents { | ||
scriptLoadRetried: (src: string, retryCount: number) => void; | ||
scriptLoadRetried: (src: string, retryNumber: number) => void; | ||
scriptLoadFailed: (src: string, error: string | Event) => void; | ||
@@ -6,0 +6,0 @@ } |
@@ -53,4 +53,4 @@ import { __awaiter } from "tslib"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const retryCount = (_a = options.retryCount) !== null && _a !== void 0 ? _a : 0; | ||
const scriptSelector = `script[src='${options.src}'][async][retryCount='${retryCount}']`; | ||
const retryNumber = (_a = options.retryNumber) !== null && _a !== void 0 ? _a : 0; | ||
const scriptSelector = `script[src='${options.src}'][async][retryCount='${retryNumber}']`; | ||
let script = this.container.querySelector(scriptSelector); | ||
@@ -81,8 +81,8 @@ if (!script) { | ||
const hasBeenRetried = script.getAttribute('hasBeenRetried'); | ||
if (retryCount < this.retryCount && !hasBeenRetried) { | ||
if (retryNumber < this.retryCount && !hasBeenRetried) { | ||
script.setAttribute('hasBeenRetried', 'true'); | ||
yield promisedSleep(this.retryInterval * 1000); | ||
const newRetryCount = retryCount + 1; | ||
this.emitter.emit('scriptLoadRetried', options.src, newRetryCount); | ||
this.doLoad(Object.assign(Object.assign({}, options), { retryCount: newRetryCount, scriptBeingRetried: script })); | ||
const newRetryNumber = retryNumber + 1; | ||
this.emitter.emit('scriptLoadRetried', options.src, newRetryNumber); | ||
this.doLoad(Object.assign(Object.assign({}, options), { retryNumber: newRetryNumber, scriptBeingRetried: script })); | ||
} | ||
@@ -89,0 +89,0 @@ else { |
@@ -226,5 +226,21 @@ import { __awaiter } from "tslib"; | ||
}); | ||
yield lazyLoader.loadScript({ src: serverUrl }); | ||
let testRetryCount = 0; | ||
lazyLoader.on('scriptLoadRetried', () => { | ||
testRetryCount += 1; | ||
}); | ||
let testFailedCount = 0; | ||
lazyLoader.on('scriptLoadFailed', () => { | ||
testFailedCount += 1; | ||
}); | ||
// make multiple simultaneous loads of the URL to verify we only end up | ||
// with one retry event | ||
const promises = [ | ||
lazyLoader.loadScript({ src: serverUrl }), | ||
lazyLoader.loadScript({ src: serverUrl }), | ||
lazyLoader.loadScript({ src: serverUrl }), | ||
]; | ||
yield Promise.all(promises); | ||
// verify we have two script tags with the expected url and a propery retryCount on each | ||
const scriptTags = container.querySelectorAll('script'); | ||
expect(scriptTags.length).to.equal(2); | ||
scriptTags.forEach((tag, index) => { | ||
@@ -234,2 +250,4 @@ expect(tag.src).to.equal(serverUrl); | ||
}); | ||
expect(testRetryCount).to.equal(1); | ||
expect(testFailedCount).to.equal(0); | ||
// verify the final load actually loaded the service | ||
@@ -236,0 +254,0 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any |
{ | ||
"name": "@internetarchive/lazy-loader-service", | ||
"version": "0.2.0-alpha.2", | ||
"version": "0.2.0-alpha.3", | ||
"description": "A small library to lazy load javascript with a Promise", | ||
@@ -5,0 +5,0 @@ "license": "AGPL-3.0-only", |
@@ -5,3 +5,3 @@ import { BundleType } from './bundle-type'; | ||
export interface LazyLoaderServiceEvents { | ||
scriptLoadRetried: (src: string, retryCount: number) => void; | ||
scriptLoadRetried: (src: string, retryNumber: number) => void; | ||
scriptLoadFailed: (src: string, error: string | Event) => void; | ||
@@ -8,0 +8,0 @@ } |
@@ -98,7 +98,7 @@ import { createNanoEvents, Unsubscribe } from 'nanoevents'; | ||
attributes?: Record<string, string>; | ||
retryCount?: number; | ||
retryNumber?: number; | ||
scriptBeingRetried?: HTMLScriptElement; | ||
}): Promise<void> { | ||
const retryCount = options.retryCount ?? 0; | ||
const scriptSelector = `script[src='${options.src}'][async][retryCount='${retryCount}']`; | ||
const retryNumber = options.retryNumber ?? 0; | ||
const scriptSelector = `script[src='${options.src}'][async][retryCount='${retryNumber}']`; | ||
let script = this.container.querySelector( | ||
@@ -140,10 +140,10 @@ scriptSelector | ||
const hasBeenRetried = script.getAttribute('hasBeenRetried'); | ||
if (retryCount < this.retryCount && !hasBeenRetried) { | ||
if (retryNumber < this.retryCount && !hasBeenRetried) { | ||
script.setAttribute('hasBeenRetried', 'true'); | ||
await promisedSleep(this.retryInterval * 1000); | ||
const newRetryCount = retryCount + 1; | ||
this.emitter.emit('scriptLoadRetried', options.src, newRetryCount); | ||
const newRetryNumber = retryNumber + 1; | ||
this.emitter.emit('scriptLoadRetried', options.src, newRetryNumber); | ||
this.doLoad({ | ||
...options, | ||
retryCount: newRetryCount, | ||
retryNumber: newRetryNumber, | ||
scriptBeingRetried: script, | ||
@@ -150,0 +150,0 @@ }); |
@@ -261,6 +261,26 @@ import { expect, fixture, html } from '@open-wc/testing'; | ||
}); | ||
await lazyLoader.loadScript({ src: serverUrl }); | ||
let testRetryCount = 0; | ||
lazyLoader.on('scriptLoadRetried', () => { | ||
testRetryCount += 1; | ||
}); | ||
let testFailedCount = 0; | ||
lazyLoader.on('scriptLoadFailed', () => { | ||
testFailedCount += 1; | ||
}); | ||
// make multiple simultaneous loads of the URL to verify we only end up | ||
// with one retry event | ||
const promises = [ | ||
lazyLoader.loadScript({ src: serverUrl }), | ||
lazyLoader.loadScript({ src: serverUrl }), | ||
lazyLoader.loadScript({ src: serverUrl }), | ||
]; | ||
await Promise.all(promises); | ||
// verify we have two script tags with the expected url and a propery retryCount on each | ||
const scriptTags = container.querySelectorAll('script'); | ||
expect(scriptTags.length).to.equal(2); | ||
scriptTags.forEach((tag, index) => { | ||
@@ -271,2 +291,5 @@ expect(tag.src).to.equal(serverUrl); | ||
expect(testRetryCount).to.equal(1); | ||
expect(testFailedCount).to.equal(0); | ||
// verify the final load actually loaded the service | ||
@@ -273,0 +296,0 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
406274
6065