@lwrjs/client-modules 0.20.4 → 0.20.6
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/build/bundle/prod/lwr/init/init.js +1 -1
- package/build/bundle/prod/lwr/lockerDefine/lockerDefine.js +1 -1
- package/build/es/modules/lwr/init/init.js +5 -39
- package/build/es/modules/lwr/scheduler/scheduler.d.ts +21 -0
- package/build/es/modules/lwr/scheduler/scheduler.js +66 -0
- package/build/modules/lwr/init/init.js +5 -47
- package/build/modules/lwr/lockerDefine/lockerDefine.js +361 -427
- package/build/modules/lwr/lockerSandbox/lockerSandbox.js +361 -427
- package/build/modules/lwr/scheduler/scheduler.js +85 -0
- package/package.json +8 -9
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result of checking whether to yield to the main thread
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Track the time of last yield for scheduler batching
|
|
6
|
+
let timeOfLastYield = 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reset the internal yield tracking state. Used primarily for testing.
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export function resetYieldTracking() {
|
|
13
|
+
timeOfLastYield = 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Yields control to the main thread if enough time has elapsed since the last yield.
|
|
18
|
+
* This automatically tracks yield timing internally to break up long-running tasks
|
|
19
|
+
* into batches, improving responsiveness without requiring manual state management.
|
|
20
|
+
*
|
|
21
|
+
* @returns Promise that resolves after yielding (if necessary) to the main thread
|
|
22
|
+
*/
|
|
23
|
+
export async function yieldIfNecessary() {
|
|
24
|
+
const result = checkShouldYield(timeOfLastYield);
|
|
25
|
+
if (result.shouldYield) {
|
|
26
|
+
timeOfLastYield = result.timeOfLastYield;
|
|
27
|
+
await yieldToMainThread();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Checks if the current execution should yield to the main thread based on elapsed time.
|
|
33
|
+
* Break up long-running tasks into timed batches to improve responsiveness.
|
|
34
|
+
* Borrowed from https://tinyurl.com/5b4fw7eb
|
|
35
|
+
*
|
|
36
|
+
* @param timeOfLastYield - Timestamp of the last yield (from performance.now())
|
|
37
|
+
* @returns Object containing whether to yield and the updated timestamp
|
|
38
|
+
*/
|
|
39
|
+
function checkShouldYield(timeOfLastYield) {
|
|
40
|
+
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
41
|
+
if (!globalThis.performance || !getSSREnabled()) {
|
|
42
|
+
return {
|
|
43
|
+
shouldYield: false,
|
|
44
|
+
timeOfLastYield
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const TASK_BATCH_DURATION = 50;
|
|
48
|
+
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
49
|
+
const now = globalThis.performance.now();
|
|
50
|
+
if (now - timeOfLastYield > TASK_BATCH_DURATION) {
|
|
51
|
+
return {
|
|
52
|
+
shouldYield: true,
|
|
53
|
+
timeOfLastYield: now
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
shouldYield: false,
|
|
58
|
+
timeOfLastYield
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Yields control to the main thread during long-running tasks to improve responsiveness.
|
|
64
|
+
* Uses the scheduler.yield() API if available, otherwise falls back to setTimeout.
|
|
65
|
+
*
|
|
66
|
+
* @returns Promise that resolves after yielding to the main thread
|
|
67
|
+
*/
|
|
68
|
+
async function yieldToMainThread() {
|
|
69
|
+
const scheduler = globalThis.scheduler;
|
|
70
|
+
// eslint-disable-next-line lwr/no-unguarded-apis
|
|
71
|
+
return scheduler?.yield ? scheduler.yield() : new Promise(resolve => setTimeout(resolve, 0));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Gets the SSREnabled flag from the global LWR environment
|
|
76
|
+
*
|
|
77
|
+
* @returns Whether SSR is enabled
|
|
78
|
+
*/
|
|
79
|
+
function getSSREnabled() {
|
|
80
|
+
const globalThisLWR = globalThis;
|
|
81
|
+
const {
|
|
82
|
+
SSREnabled
|
|
83
|
+
} = globalThisLWR.LWR && globalThisLWR.LWR.env || {};
|
|
84
|
+
return !!SSREnabled;
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.20.
|
|
7
|
+
"version": "0.20.6",
|
|
8
8
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -29,19 +29,17 @@
|
|
|
29
29
|
"build:ts": "tsc -b",
|
|
30
30
|
"build": "yarn build:ts && node ../../../bin/pack-lwc --dir modules build/modules && yarn build:locker && yarn build:bundle",
|
|
31
31
|
"build:locker": "rollup --config ./scripts/rollup.config.locker.js",
|
|
32
|
-
"build:bundle": "rollup --config scripts/rollup.moduleBundle.config.cjs"
|
|
33
|
-
"build:platform": "node scripts/strip-for-core.js"
|
|
32
|
+
"build:bundle": "rollup --config scripts/rollup.moduleBundle.config.cjs"
|
|
34
33
|
},
|
|
35
34
|
"dependencies": {
|
|
36
|
-
"@locker/sandbox": "0.
|
|
37
|
-
"@lwrjs/shared-utils": "0.20.
|
|
35
|
+
"@locker/sandbox": "0.26.2",
|
|
36
|
+
"@lwrjs/shared-utils": "0.20.6"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|
|
40
|
-
"@lwrjs/types": "0.20.
|
|
39
|
+
"@lwrjs/types": "0.20.6",
|
|
41
40
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
42
41
|
"@rollup/plugin-sucrase": "^5.0.2",
|
|
43
42
|
"@rollup/plugin-terser": "^0.4.4",
|
|
44
|
-
"fs-extra": "^11.2.0",
|
|
45
43
|
"rollup": "^2.79.2"
|
|
46
44
|
},
|
|
47
45
|
"lwc": {
|
|
@@ -61,7 +59,8 @@
|
|
|
61
59
|
"lwr/lockerSandbox",
|
|
62
60
|
"lwr/metrics",
|
|
63
61
|
"lwr/profiler",
|
|
64
|
-
"lwr/serverDataCallback"
|
|
62
|
+
"lwr/serverDataCallback",
|
|
63
|
+
"lwr/scheduler"
|
|
65
64
|
]
|
|
66
65
|
},
|
|
67
66
|
"engines": {
|
|
@@ -70,5 +69,5 @@
|
|
|
70
69
|
"volta": {
|
|
71
70
|
"extends": "../../../package.json"
|
|
72
71
|
},
|
|
73
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "754d28a0d270ca347d1eb769b34bd62bc1e85119"
|
|
74
73
|
}
|