smart-load-manager 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +27 -8
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +27 -8
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -63,7 +63,12 @@ class SmartService {
|
|
|
63
63
|
this._mutex = Promise.resolve();
|
|
64
64
|
}
|
|
65
65
|
static config(options) {
|
|
66
|
-
if (options)
|
|
66
|
+
if (options) {
|
|
67
|
+
this._config = __spreadValues$1(__spreadValues$1({}, this._config), options);
|
|
68
|
+
if (Object.prototype.hasOwnProperty.call(this, "_instance") && this._instance) {
|
|
69
|
+
this._instance._config = this._config;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
67
72
|
return this._config;
|
|
68
73
|
}
|
|
69
74
|
/** Create a new instance of SmartService */
|
|
@@ -72,7 +77,7 @@ class SmartService {
|
|
|
72
77
|
}
|
|
73
78
|
/** Get the singleton instance of SmartService */
|
|
74
79
|
static get instance() {
|
|
75
|
-
if (!this._instance) {
|
|
80
|
+
if (!Object.prototype.hasOwnProperty.call(this, "_instance") || !this._instance) {
|
|
76
81
|
this._instance = new this(this._config);
|
|
77
82
|
}
|
|
78
83
|
return this._instance;
|
|
@@ -225,7 +230,7 @@ class SmartLoad {
|
|
|
225
230
|
(() => __async$1(this, null, function* () {
|
|
226
231
|
try {
|
|
227
232
|
yield this._whenStarted.promise;
|
|
228
|
-
yield previousTask();
|
|
233
|
+
yield typeof previousTask === "function" ? previousTask() : previousTask;
|
|
229
234
|
} catch (e) {
|
|
230
235
|
} finally {
|
|
231
236
|
resolve();
|
|
@@ -300,6 +305,11 @@ var __async = (__this, __arguments, generator) => {
|
|
|
300
305
|
const IDLE_THRESHOLD_DURATION = 46.6;
|
|
301
306
|
const IDLE_THRESHOLD_RATIO = 0.9;
|
|
302
307
|
const IDLE_TIMEOUT = 1e4;
|
|
308
|
+
const reqIdle = (cb, opts) => typeof requestIdleCallback === "function" ? requestIdleCallback(cb, opts) : setTimeout(() => cb({ didTimeout: false, timeRemaining: () => 50 }), 1);
|
|
309
|
+
const cancelIdle = (id) => {
|
|
310
|
+
if (id === void 0) return;
|
|
311
|
+
typeof cancelIdleCallback === "function" ? cancelIdleCallback(id) : clearTimeout(id);
|
|
312
|
+
};
|
|
303
313
|
function promisifyIdle(options = {}) {
|
|
304
314
|
var _a, _b;
|
|
305
315
|
const idleThresholdRatio = ((_a = options.thresholds) == null ? void 0 : _a.ratio) || IDLE_THRESHOLD_RATIO;
|
|
@@ -310,9 +320,15 @@ function promisifyIdle(options = {}) {
|
|
|
310
320
|
return new Promise((resolve, reject) => {
|
|
311
321
|
const start = performance.now();
|
|
312
322
|
let previousFrameEnd = start;
|
|
323
|
+
let idleId;
|
|
324
|
+
let settled = false;
|
|
313
325
|
const log = [];
|
|
314
326
|
const last3 = [0, 0, 0];
|
|
315
327
|
function finish(withResolve, reason) {
|
|
328
|
+
if (settled) return;
|
|
329
|
+
settled = true;
|
|
330
|
+
!withResolve && cancelIdle(idleId);
|
|
331
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
316
332
|
if (debug) {
|
|
317
333
|
console.table(log);
|
|
318
334
|
console.log(`Idle has ${withResolve ? "reached" : "aborted"} after ${performance.now() - start}ms`);
|
|
@@ -321,8 +337,11 @@ function promisifyIdle(options = {}) {
|
|
|
321
337
|
}
|
|
322
338
|
return withResolve ? resolve(true) : reject(reason);
|
|
323
339
|
}
|
|
340
|
+
const onAbort = () => finish(false, new Error("Rejected by abort signal"));
|
|
341
|
+
if (signal == null ? void 0 : signal.aborted) return onAbort();
|
|
342
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
|
|
324
343
|
const cb = (deadline) => {
|
|
325
|
-
if (signal == null ? void 0 : signal.aborted) return
|
|
344
|
+
if (signal == null ? void 0 : signal.aborted) return onAbort();
|
|
326
345
|
const allocated = deadline.timeRemaining();
|
|
327
346
|
const frameEnd = performance.now() + allocated;
|
|
328
347
|
const frameLength = frameEnd - previousFrameEnd;
|
|
@@ -337,15 +356,15 @@ function promisifyIdle(options = {}) {
|
|
|
337
356
|
debug && !isBadFrame && log.push([timeout, frameLength, allocated, idleRatio, idleRatio > idleThresholdRatio ? 1 : 0, last3.toString()]);
|
|
338
357
|
if (isIdleNow || deadline.didTimeout) return finish(true);
|
|
339
358
|
!isBadFrame && (previousFrameEnd = frameEnd);
|
|
340
|
-
|
|
359
|
+
idleId = reqIdle(cb, { timeout });
|
|
341
360
|
};
|
|
342
|
-
|
|
361
|
+
idleId = reqIdle(cb, { timeout: idleTimeout });
|
|
343
362
|
});
|
|
344
363
|
}
|
|
345
364
|
function asyncSeries(tasks) {
|
|
346
365
|
return __async(this, null, function* () {
|
|
347
366
|
for (const task of tasks) {
|
|
348
|
-
yield task().catch();
|
|
367
|
+
yield task().catch(() => void 0);
|
|
349
368
|
}
|
|
350
369
|
});
|
|
351
370
|
}
|
|
@@ -380,7 +399,7 @@ function waitUserActivity() {
|
|
|
380
399
|
}
|
|
381
400
|
function waitIdle(options = {}) {
|
|
382
401
|
return (abortSignal) => __async(null, null, function* () {
|
|
383
|
-
return promisifyIdle(__spreadProps(__spreadValues({}, options), { signal: abortSignal }));
|
|
402
|
+
return promisifyIdle(__spreadProps(__spreadValues({}, options), { signal: abortSignal != null ? abortSignal : options.signal }));
|
|
384
403
|
});
|
|
385
404
|
}
|
|
386
405
|
|
package/dist/index.d.cts
CHANGED
|
@@ -65,6 +65,7 @@ declare class SmartService {
|
|
|
65
65
|
protected _debug(...args: unknown[]): void;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
type PreviousTask = (() => Promise<unknown>) | Promise<unknown>;
|
|
68
69
|
/** Manager for smart loading of services */
|
|
69
70
|
declare class SmartLoad {
|
|
70
71
|
protected static _whenStarted: _exadel_esl_modules_esl_utils_async.Deferred<void>;
|
|
@@ -76,9 +77,9 @@ declare class SmartLoad {
|
|
|
76
77
|
static onLoaded(): Promise<void>;
|
|
77
78
|
/** Wait until the document is fully loaded and then resolve */
|
|
78
79
|
static onComplete(): Promise<void>;
|
|
79
|
-
protected static createMutex(previousTask:
|
|
80
|
+
protected static createMutex(previousTask: PreviousTask): Promise<void>;
|
|
80
81
|
/** Queue a service to be loaded after the given task */
|
|
81
|
-
static queue(service: typeof SmartService | SmartService, after?:
|
|
82
|
+
static queue(service: typeof SmartService | SmartService, after?: PreviousTask): void;
|
|
82
83
|
/** Start the smart loading process */
|
|
83
84
|
static start(): void;
|
|
84
85
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -65,6 +65,7 @@ declare class SmartService {
|
|
|
65
65
|
protected _debug(...args: unknown[]): void;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
type PreviousTask = (() => Promise<unknown>) | Promise<unknown>;
|
|
68
69
|
/** Manager for smart loading of services */
|
|
69
70
|
declare class SmartLoad {
|
|
70
71
|
protected static _whenStarted: _exadel_esl_modules_esl_utils_async.Deferred<void>;
|
|
@@ -76,9 +77,9 @@ declare class SmartLoad {
|
|
|
76
77
|
static onLoaded(): Promise<void>;
|
|
77
78
|
/** Wait until the document is fully loaded and then resolve */
|
|
78
79
|
static onComplete(): Promise<void>;
|
|
79
|
-
protected static createMutex(previousTask:
|
|
80
|
+
protected static createMutex(previousTask: PreviousTask): Promise<void>;
|
|
80
81
|
/** Queue a service to be loaded after the given task */
|
|
81
|
-
static queue(service: typeof SmartService | SmartService, after?:
|
|
82
|
+
static queue(service: typeof SmartService | SmartService, after?: PreviousTask): void;
|
|
82
83
|
/** Start the smart loading process */
|
|
83
84
|
static start(): void;
|
|
84
85
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -61,7 +61,12 @@ class SmartService {
|
|
|
61
61
|
this._mutex = Promise.resolve();
|
|
62
62
|
}
|
|
63
63
|
static config(options) {
|
|
64
|
-
if (options)
|
|
64
|
+
if (options) {
|
|
65
|
+
this._config = __spreadValues$1(__spreadValues$1({}, this._config), options);
|
|
66
|
+
if (Object.prototype.hasOwnProperty.call(this, "_instance") && this._instance) {
|
|
67
|
+
this._instance._config = this._config;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
65
70
|
return this._config;
|
|
66
71
|
}
|
|
67
72
|
/** Create a new instance of SmartService */
|
|
@@ -70,7 +75,7 @@ class SmartService {
|
|
|
70
75
|
}
|
|
71
76
|
/** Get the singleton instance of SmartService */
|
|
72
77
|
static get instance() {
|
|
73
|
-
if (!this._instance) {
|
|
78
|
+
if (!Object.prototype.hasOwnProperty.call(this, "_instance") || !this._instance) {
|
|
74
79
|
this._instance = new this(this._config);
|
|
75
80
|
}
|
|
76
81
|
return this._instance;
|
|
@@ -223,7 +228,7 @@ class SmartLoad {
|
|
|
223
228
|
(() => __async$1(this, null, function* () {
|
|
224
229
|
try {
|
|
225
230
|
yield this._whenStarted.promise;
|
|
226
|
-
yield previousTask();
|
|
231
|
+
yield typeof previousTask === "function" ? previousTask() : previousTask;
|
|
227
232
|
} catch (e) {
|
|
228
233
|
} finally {
|
|
229
234
|
resolve();
|
|
@@ -298,6 +303,11 @@ var __async = (__this, __arguments, generator) => {
|
|
|
298
303
|
const IDLE_THRESHOLD_DURATION = 46.6;
|
|
299
304
|
const IDLE_THRESHOLD_RATIO = 0.9;
|
|
300
305
|
const IDLE_TIMEOUT = 1e4;
|
|
306
|
+
const reqIdle = (cb, opts) => typeof requestIdleCallback === "function" ? requestIdleCallback(cb, opts) : setTimeout(() => cb({ didTimeout: false, timeRemaining: () => 50 }), 1);
|
|
307
|
+
const cancelIdle = (id) => {
|
|
308
|
+
if (id === void 0) return;
|
|
309
|
+
typeof cancelIdleCallback === "function" ? cancelIdleCallback(id) : clearTimeout(id);
|
|
310
|
+
};
|
|
301
311
|
function promisifyIdle(options = {}) {
|
|
302
312
|
var _a, _b;
|
|
303
313
|
const idleThresholdRatio = ((_a = options.thresholds) == null ? void 0 : _a.ratio) || IDLE_THRESHOLD_RATIO;
|
|
@@ -308,9 +318,15 @@ function promisifyIdle(options = {}) {
|
|
|
308
318
|
return new Promise((resolve, reject) => {
|
|
309
319
|
const start = performance.now();
|
|
310
320
|
let previousFrameEnd = start;
|
|
321
|
+
let idleId;
|
|
322
|
+
let settled = false;
|
|
311
323
|
const log = [];
|
|
312
324
|
const last3 = [0, 0, 0];
|
|
313
325
|
function finish(withResolve, reason) {
|
|
326
|
+
if (settled) return;
|
|
327
|
+
settled = true;
|
|
328
|
+
!withResolve && cancelIdle(idleId);
|
|
329
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
314
330
|
if (debug) {
|
|
315
331
|
console.table(log);
|
|
316
332
|
console.log(`Idle has ${withResolve ? "reached" : "aborted"} after ${performance.now() - start}ms`);
|
|
@@ -319,8 +335,11 @@ function promisifyIdle(options = {}) {
|
|
|
319
335
|
}
|
|
320
336
|
return withResolve ? resolve(true) : reject(reason);
|
|
321
337
|
}
|
|
338
|
+
const onAbort = () => finish(false, new Error("Rejected by abort signal"));
|
|
339
|
+
if (signal == null ? void 0 : signal.aborted) return onAbort();
|
|
340
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
|
|
322
341
|
const cb = (deadline) => {
|
|
323
|
-
if (signal == null ? void 0 : signal.aborted) return
|
|
342
|
+
if (signal == null ? void 0 : signal.aborted) return onAbort();
|
|
324
343
|
const allocated = deadline.timeRemaining();
|
|
325
344
|
const frameEnd = performance.now() + allocated;
|
|
326
345
|
const frameLength = frameEnd - previousFrameEnd;
|
|
@@ -335,15 +354,15 @@ function promisifyIdle(options = {}) {
|
|
|
335
354
|
debug && !isBadFrame && log.push([timeout, frameLength, allocated, idleRatio, idleRatio > idleThresholdRatio ? 1 : 0, last3.toString()]);
|
|
336
355
|
if (isIdleNow || deadline.didTimeout) return finish(true);
|
|
337
356
|
!isBadFrame && (previousFrameEnd = frameEnd);
|
|
338
|
-
|
|
357
|
+
idleId = reqIdle(cb, { timeout });
|
|
339
358
|
};
|
|
340
|
-
|
|
359
|
+
idleId = reqIdle(cb, { timeout: idleTimeout });
|
|
341
360
|
});
|
|
342
361
|
}
|
|
343
362
|
function asyncSeries(tasks) {
|
|
344
363
|
return __async(this, null, function* () {
|
|
345
364
|
for (const task of tasks) {
|
|
346
|
-
yield task().catch();
|
|
365
|
+
yield task().catch(() => void 0);
|
|
347
366
|
}
|
|
348
367
|
});
|
|
349
368
|
}
|
|
@@ -378,7 +397,7 @@ function waitUserActivity() {
|
|
|
378
397
|
}
|
|
379
398
|
function waitIdle(options = {}) {
|
|
380
399
|
return (abortSignal) => __async(null, null, function* () {
|
|
381
|
-
return promisifyIdle(__spreadProps(__spreadValues({}, options), { signal: abortSignal }));
|
|
400
|
+
return promisifyIdle(__spreadProps(__spreadValues({}, options), { signal: abortSignal != null ? abortSignal : options.signal }));
|
|
382
401
|
});
|
|
383
402
|
}
|
|
384
403
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smart-load-manager",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A service and add-on utilities for managing third-party service (e.g., AdSense) loading for your web application that help improve Core Web Vitals.",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|