@nxtedition/deepstream.io-client-js 23.4.3 → 23.4.4
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/package.json +1 -1
- package/src/record/record-handler.js +6 -8
- package/src/utils/timers.js +97 -0
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@ const jsonPath = require('./json-path')
|
|
|
9
9
|
const utils = require('../utils/utils')
|
|
10
10
|
const rx = require('rxjs/operators')
|
|
11
11
|
const xuid = require('xuid')
|
|
12
|
+
const timers = require('../utils/timers')
|
|
12
13
|
|
|
13
14
|
const kEmpty = Symbol('kEmpty')
|
|
14
15
|
|
|
@@ -180,7 +181,7 @@ class RecordHandler {
|
|
|
180
181
|
signal?.removeEventListener('abort', onAbort)
|
|
181
182
|
|
|
182
183
|
if (timeoutHandle) {
|
|
183
|
-
clearTimeout(timeoutHandle)
|
|
184
|
+
timers.clearTimeout(timeoutHandle)
|
|
184
185
|
timeoutHandle = null
|
|
185
186
|
}
|
|
186
187
|
|
|
@@ -207,8 +208,7 @@ class RecordHandler {
|
|
|
207
208
|
const onTimeout = () => {
|
|
208
209
|
const elapsed = Date.now() - this._connected
|
|
209
210
|
if (elapsed < timeoutValue) {
|
|
210
|
-
timeoutHandle = setTimeout(onTimeout, timeoutValue - elapsed)
|
|
211
|
-
timeoutHandle.unref?.()
|
|
211
|
+
timeoutHandle = timers.setTimeout(onTimeout, timeoutValue - elapsed)
|
|
212
212
|
} else {
|
|
213
213
|
for (const rec of records.filter((rec) => !rec.isReady)) {
|
|
214
214
|
this._client._$onError(C.TOPIC.RECORD, C.EVENT.TIMEOUT, 'record timeout', [
|
|
@@ -230,8 +230,7 @@ class RecordHandler {
|
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
if (timeoutValue) {
|
|
233
|
-
timeoutHandle = setTimeout(onTimeout, timeoutValue)
|
|
234
|
-
timeoutHandle.unref?.()
|
|
233
|
+
timeoutHandle = timers.setTimeout(onTimeout, timeoutValue)
|
|
235
234
|
}
|
|
236
235
|
|
|
237
236
|
signal?.addEventListener('abort', onAbort)
|
|
@@ -373,7 +372,7 @@ class RecordHandler {
|
|
|
373
372
|
}
|
|
374
373
|
|
|
375
374
|
if (timeoutHandle) {
|
|
376
|
-
clearTimeout(timeoutHandle)
|
|
375
|
+
timers.clearTimeout(timeoutHandle)
|
|
377
376
|
timeoutHandle = null
|
|
378
377
|
}
|
|
379
378
|
|
|
@@ -397,7 +396,7 @@ class RecordHandler {
|
|
|
397
396
|
const record = this.getRecord(name).subscribe(onUpdate)
|
|
398
397
|
|
|
399
398
|
if (timeoutValue && state && record.state < state) {
|
|
400
|
-
timeoutHandle = setTimeout(() => {
|
|
399
|
+
timeoutHandle = timers.setTimeout(() => {
|
|
401
400
|
const expected = C.RECORD_STATE_NAME[state]
|
|
402
401
|
const current = C.RECORD_STATE_NAME[record.state]
|
|
403
402
|
o.error(
|
|
@@ -407,7 +406,6 @@ class RecordHandler {
|
|
|
407
406
|
)
|
|
408
407
|
)
|
|
409
408
|
}, timeoutValue)
|
|
410
|
-
timeoutHandle.unref?.()
|
|
411
409
|
}
|
|
412
410
|
|
|
413
411
|
if (record.version) {
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// undici timers
|
|
2
|
+
|
|
3
|
+
'use strict'
|
|
4
|
+
|
|
5
|
+
let fastNow = Date.now()
|
|
6
|
+
let fastNowTimeout
|
|
7
|
+
|
|
8
|
+
const fastTimers = []
|
|
9
|
+
|
|
10
|
+
function onTimeout() {
|
|
11
|
+
fastNow = Date.now()
|
|
12
|
+
|
|
13
|
+
let len = fastTimers.length
|
|
14
|
+
let idx = 0
|
|
15
|
+
while (idx < len) {
|
|
16
|
+
const timer = fastTimers[idx]
|
|
17
|
+
|
|
18
|
+
if (timer.state === 0) {
|
|
19
|
+
timer.state = fastNow + timer.delay
|
|
20
|
+
} else if (timer.state > 0 && fastNow >= timer.state) {
|
|
21
|
+
timer.state = -1
|
|
22
|
+
timer.callback(timer.opaque)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (timer.state === -1) {
|
|
26
|
+
timer.state = -2
|
|
27
|
+
if (idx !== len - 1) {
|
|
28
|
+
fastTimers[idx] = fastTimers.pop()
|
|
29
|
+
} else {
|
|
30
|
+
fastTimers.pop()
|
|
31
|
+
}
|
|
32
|
+
len -= 1
|
|
33
|
+
} else {
|
|
34
|
+
idx += 1
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (fastTimers.length > 0) {
|
|
39
|
+
refreshTimeout()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function refreshTimeout() {
|
|
44
|
+
if (fastNowTimeout && fastNowTimeout.refresh) {
|
|
45
|
+
fastNowTimeout.refresh()
|
|
46
|
+
} else {
|
|
47
|
+
clearTimeout(fastNowTimeout)
|
|
48
|
+
fastNowTimeout = setTimeout(onTimeout, 1e3)
|
|
49
|
+
if (fastNowTimeout.unref) {
|
|
50
|
+
fastNowTimeout.unref()
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class Timeout {
|
|
56
|
+
constructor(callback, delay, opaque) {
|
|
57
|
+
this.callback = callback
|
|
58
|
+
this.delay = delay
|
|
59
|
+
this.opaque = opaque
|
|
60
|
+
|
|
61
|
+
// -2 not in timer list
|
|
62
|
+
// -1 in timer list but inactive
|
|
63
|
+
// 0 in timer list waiting for time
|
|
64
|
+
// > 0 in timer list waiting for time to expire
|
|
65
|
+
this.state = -2
|
|
66
|
+
|
|
67
|
+
this.refresh()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
refresh() {
|
|
71
|
+
if (this.state === -2) {
|
|
72
|
+
fastTimers.push(this)
|
|
73
|
+
if (!fastNowTimeout || fastTimers.length === 1) {
|
|
74
|
+
refreshTimeout()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.state = 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
clear() {
|
|
82
|
+
this.state = -1
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
setTimeout(callback, delay, opaque) {
|
|
88
|
+
return delay < 1e3 ? setTimeout(callback, delay, opaque) : new Timeout(callback, delay, opaque)
|
|
89
|
+
},
|
|
90
|
+
clearTimeout(timeout) {
|
|
91
|
+
if (timeout instanceof Timeout) {
|
|
92
|
+
timeout.clear()
|
|
93
|
+
} else {
|
|
94
|
+
clearTimeout(timeout)
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
}
|