@nxtedition/lib 23.17.14 → 23.17.16

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/app.js CHANGED
@@ -38,6 +38,7 @@ import { isTimeBetween } from './time.js'
38
38
  import makeUnderPressure from './under-pressure.js'
39
39
  import undici from '@nxtedition/undici'
40
40
  import { isPrimary } from 'node:cluster'
41
+ import { doYield } from './yield.js'
41
42
 
42
43
  export function makeApp(appConfig, onTerminate) {
43
44
  let ds
@@ -397,7 +398,7 @@ export function makeApp(appConfig, onTerminate) {
397
398
  {
398
399
  userAgent,
399
400
  url: isProduction ? 'ws://deepstream:6020/deepstream' : 'ws://127.0.0.1:6020/deepstream',
400
- schedule: (fn) => setImmediate(fn),
401
+ schedule: (fn) => doYield(fn),
401
402
  },
402
403
  appConfig.deepstream,
403
404
  config.deepstream,
package/fixed-queue.js CHANGED
@@ -56,7 +56,7 @@ class FixedCircularBuffer {
56
56
  constructor() {
57
57
  this.bottom = 0
58
58
  this.top = 0
59
- this.list = new Array(kSize)
59
+ this.list = new Array(kSize).fill(undefined)
60
60
  this.next = null
61
61
  }
62
62
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.17.14",
3
+ "version": "23.17.16",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -4,6 +4,7 @@ import { initSync } from '@swc/wasm-web'
4
4
  import { makeTemplateCompiler as commonMakeTemplateCompiler } from './index-common.js'
5
5
  import { hashSync } from 'hasha'
6
6
  import { request } from '@nxtedition/nxt-undici'
7
+ import { doYield } from '../../yield.js'
7
8
 
8
9
  export * from './index-common.js'
9
10
 
@@ -28,7 +29,7 @@ export function makeTemplateCompiler({
28
29
  fetch: (resource, options) =>
29
30
  request(resource, options && dispatcher ? { dispatcher, ...options } : defaultOptions),
30
31
  ...platform,
31
- schedule: (fn) => setImmediate(fn),
32
+ schedule: (fn) => doYield(fn),
32
33
  },
33
34
  })
34
35
  }
package/yield.js CHANGED
@@ -1,21 +1,13 @@
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())
10
3
  const yieldSchedule = globalThis.setImmediate ?? ((fn) => setTimeout(fn, 0))
11
4
 
5
+ let yieldQueue = new FixedQueue()
12
6
  let yieldTimeout = 40
13
7
  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,101 +22,63 @@ 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
+ const queue = yieldQueue
66
+ yieldQueue = new FixedQueue()
67
+
68
+ while (!queue.isEmpty()) {
69
+ const resolve = queue.shift()
70
+
71
+ resolve(null)
72
+
73
+ if (shouldYield()) {
74
+ yieldSchedule(dispatchYield)
75
+ return
146
76
  }
147
77
  }
148
78
 
149
- yieldScheduled = false
79
+ if (yieldQueue.isEmpty()) {
80
+ yieldScheduled = false
81
+ } else {
82
+ yieldSchedule(dispatchYield)
83
+ }
150
84
  }