@nxtedition/lib 24.0.1 → 24.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/yield.js +15 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "24.0.1",
3
+ "version": "24.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/yield.js CHANGED
@@ -1,4 +1,5 @@
1
- const yieldSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
1
+ const kDefaultSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
2
+ let yieldSchedule = kDefaultSchedule
2
3
  const yieldQueue = []
3
4
 
4
5
  let yieldIndex = 0
@@ -7,6 +8,10 @@ let yieldActive = false
7
8
  let yieldScheduled = false
8
9
  let yieldTime = performance.now()
9
10
 
11
+ export function setYieldDispatcher(schedule) {
12
+ yieldSchedule = schedule ?? kDefaultSchedule
13
+ }
14
+
10
15
  export function setYieldTimeout(timeout) {
11
16
  if (typeof timeout !== 'number' || timeout < 0) {
12
17
  throw new TypeError('timeout must be a positive number')
@@ -14,19 +19,24 @@ export function setYieldTimeout(timeout) {
14
19
  yieldTimeout = timeout
15
20
  }
16
21
 
17
- export function shouldYield() {
18
- if (!yieldActive && performance.now() - yieldTime >= yieldTimeout) {
22
+ export function shouldYield(timeout) {
23
+ if (timeout === undefined) {
24
+ timeout = yieldTimeout
25
+ }
26
+
27
+ if (!yieldActive && performance.now() - yieldTime >= timeout) {
19
28
  yieldActive = true
20
29
  }
30
+
21
31
  return yieldActive
22
32
  }
23
33
 
24
- export function maybeYield(callback, opaque) {
34
+ export function maybeYield(callback, opaque, timeout) {
25
35
  if (callback != null && typeof callback !== 'function') {
26
36
  throw new TypeError('callback must be a function')
27
37
  }
28
38
 
29
- if (shouldYield()) {
39
+ if (shouldYield(timeout)) {
30
40
  return doYield(callback, opaque)
31
41
  }
32
42