@nxtedition/lib 23.15.7 → 23.16.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/yield.js +15 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.15.7",
3
+ "version": "23.16.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/yield.js CHANGED
@@ -4,7 +4,8 @@ const yieldQueue = new FixedQueue()
4
4
  const yieldSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
5
5
 
6
6
  let yieldTimeout = 40
7
- let yieldState = 0
7
+ let yieldActive = false
8
+ let yieldScheduled = false
8
9
  let yieldTime = performance.now()
9
10
 
10
11
  export function setYieldTimeout(timeout) {
@@ -14,21 +15,24 @@ export function setYieldTimeout(timeout) {
14
15
  yieldTimeout = timeout
15
16
  }
16
17
 
18
+ export function shouldYield() {
19
+ if (!yieldActive && performance.now() - yieldTime >= yieldTimeout) {
20
+ yieldActive = true
21
+ }
22
+ return yieldActive
23
+ }
24
+
17
25
  export function maybeYield(callback) {
18
26
  if (callback != null && typeof callback !== 'function') {
19
27
  throw new TypeError('callback must be a function')
20
28
  }
21
29
 
22
- if (performance.now() - yieldTime < yieldTimeout) {
23
- return null
24
- }
25
-
26
- return doYield(callback) ?? true
30
+ return shouldYield() ? (doYield(callback) ?? true) : null
27
31
  }
28
32
 
29
33
  export function doYield(callback) {
30
- if (yieldState === 0) {
31
- yieldState = 1
34
+ if (!yieldScheduled) {
35
+ yieldScheduled = true
32
36
  yieldSchedule(dispatchYield)
33
37
  }
34
38
 
@@ -42,24 +46,19 @@ export function doYield(callback) {
42
46
  }
43
47
 
44
48
  function dispatchYield() {
45
- if (yieldState === 2) {
46
- return
47
- }
48
-
49
- yieldState = 2
50
49
  yieldTime = performance.now()
50
+ yieldActive = false
51
51
 
52
52
  while (!yieldQueue.isEmpty()) {
53
53
  const resolve = yieldQueue.shift()
54
54
 
55
55
  resolve(null)
56
56
 
57
- if (performance.now() - yieldTime > yieldTimeout) {
58
- yieldState = 1
57
+ if (shouldYield()) {
59
58
  yieldSchedule(dispatchYield)
60
59
  return
61
60
  }
62
61
  }
63
62
 
64
- yieldState = 0
63
+ yieldScheduled = false
65
64
  }