@nxtedition/lib 23.17.14 → 23.17.15

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 -88
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.17.14",
3
+ "version": "23.17.15",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/yield.js CHANGED
@@ -1,12 +1,6 @@
1
1
  import { FixedQueue } from './fixed-queue.js'
2
- import * as timers from './timers.js'
3
2
 
4
- export const YIELD_PRIORITY_HIGH = 0
5
- export const YIELD_PRIORITY_NORMAL = 1
6
- export const YIELD_PRIORITY_LOW = 2
7
- export const YIELD_PRIORITY_COUNT = 3
8
-
9
- const yieldQueue = new Array(YIELD_PRIORITY_COUNT).fill(null).map(() => new FixedQueue())
3
+ const yieldQueue = new FixedQueue()
10
4
  const yieldSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
11
5
 
12
6
  let yieldTimeout = 40
@@ -14,8 +8,6 @@ let yieldActive = false
14
8
  let yieldScheduled = false
15
9
  let yieldTime = performance.now()
16
10
 
17
- const POOL = []
18
-
19
11
  export function setYieldTimeout(timeout) {
20
12
  if (typeof timeout !== 'number' || timeout < 0) {
21
13
  throw new TypeError('timeout must be a positive number')
@@ -23,26 +15,6 @@ export function setYieldTimeout(timeout) {
23
15
  yieldTimeout = timeout
24
16
  }
25
17
 
26
- export function setTimeout(callback, timeout, opaque, priority) {
27
- if (typeof callback !== 'function') {
28
- throw new TypeError('callback must be a function')
29
- }
30
- if (typeof timeout !== 'number' || timeout < 0) {
31
- throw new TypeError('timeout must be a positive number')
32
- }
33
-
34
- return timers.setTimeout((opaque) => maybeYield(callback, opaque, priority), timeout, opaque)
35
- }
36
-
37
- export function clearTimeout(timeout) {
38
- timers.clearTimeout(timeout)
39
- }
40
-
41
- // TODO (fix): Return a handle?
42
- export function setImmediate(callback, opaque, priority) {
43
- maybeYield(callback, opaque, priority)
44
- }
45
-
46
18
  export function shouldYield() {
47
19
  if (!yieldActive && performance.now() - yieldTime >= yieldTimeout) {
48
20
  yieldActive = true
@@ -50,99 +22,54 @@ export function shouldYield() {
50
22
  return yieldActive
51
23
  }
52
24
 
53
- export function maybeYield(callback, opaque, priority) {
25
+ export function maybeYield(callback) {
54
26
  if (callback != null && typeof callback !== 'function') {
55
27
  throw new TypeError('callback must be a function')
56
28
  }
57
29
 
58
30
  if (shouldYield()) {
59
- return doYield(callback, opaque, priority)
31
+ return doYield(callback)
60
32
  }
61
33
 
62
34
  if (callback != null) {
63
- callback(opaque)
35
+ callback(null)
64
36
  }
65
37
 
66
38
  return null
67
39
  }
68
40
 
69
- export function doYield(callback, opaque, priority) {
41
+ export function doYield(callback) {
70
42
  if (callback != null && typeof callback !== 'function') {
71
43
  throw new TypeError('callback must be a function')
72
44
  }
73
45
 
74
- if (priority === undefined) {
75
- priority = YIELD_PRIORITY_NORMAL
76
- }
77
-
78
- if (priority < 0 || priority >= YIELD_PRIORITY_COUNT) {
79
- throw new RangeError('invalid priority')
80
- }
81
-
82
46
  if (!yieldScheduled) {
83
47
  yieldScheduled = true
84
48
  yieldSchedule(dispatchYield)
85
49
  }
86
50
 
87
51
  if (callback != null) {
88
- if (opaque !== undefined) {
89
- const arr = POOL.pop() ?? [null, null]
90
- arr[0] = callback
91
- arr[1] = opaque
92
- yieldQueue[priority].push(arr)
93
- } else {
94
- yieldQueue[priority].push(callback)
95
- }
52
+ yieldQueue.push(callback)
96
53
  return null
97
54
  }
98
55
 
99
56
  return new Promise((resolve) => {
100
- if (opaque !== undefined) {
101
- const arr = POOL.pop() ?? [null, null]
102
- arr[0] = resolve
103
- arr[1] = opaque
104
- yieldQueue[priority].push(arr)
105
- } else {
106
- yieldQueue[priority].push(callback)
107
- }
57
+ yieldQueue.push(resolve)
108
58
  })
109
59
  }
110
60
 
111
- function dispatchItem(item) {
112
- if (item == null) {
113
- return
114
- }
115
-
116
- let callback
117
- let opaque = null
118
-
119
- if (Array.isArray(item)) {
120
- callback = item[0]
121
- opaque = item[1]
122
- if (POOL.length < 4096) {
123
- item[0] = null
124
- item[1] = null
125
- POOL.push(item)
126
- }
127
- } else {
128
- callback = item
129
- }
130
-
131
- callback(opaque)
132
- }
133
-
134
61
  function dispatchYield() {
135
62
  yieldTime = performance.now()
136
63
  yieldActive = false
137
64
 
138
- for (let i = 0; i < YIELD_PRIORITY_COUNT; i++) {
139
- const queue = yieldQueue[i]
140
- while (!queue.isEmpty()) {
141
- if (shouldYield()) {
142
- yieldSchedule(dispatchYield)
143
- return
144
- }
145
- dispatchItem(queue.shift())
65
+ while (!yieldQueue.isEmpty()) {
66
+ const resolve = yieldQueue.shift()
67
+
68
+ resolve(null)
69
+
70
+ if (shouldYield()) {
71
+ yieldSchedule(dispatchYield)
72
+ return
146
73
  }
147
74
  }
148
75