@nxtedition/deepstream.io-client-js 28.1.16 → 28.1.18
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 +99 -75
- package/src/utils/timers.js +4 -0
package/package.json
CHANGED
|
@@ -10,6 +10,8 @@ import * as utils from '../utils/utils.js'
|
|
|
10
10
|
import xuid from 'xuid'
|
|
11
11
|
import * as timers from '../utils/timers.js'
|
|
12
12
|
|
|
13
|
+
function noop() {}
|
|
14
|
+
|
|
13
15
|
const kEmpty = Symbol('kEmpty')
|
|
14
16
|
|
|
15
17
|
function onUpdate(record, subscription) {
|
|
@@ -265,96 +267,118 @@ class RecordHandler {
|
|
|
265
267
|
|
|
266
268
|
async sync(opts) {
|
|
267
269
|
// TODO (fix): Sync pending? What about VOID state?
|
|
268
|
-
|
|
269
|
-
let onAbort
|
|
270
|
+
// TODO (perf): Slow implementation...
|
|
270
271
|
|
|
271
272
|
const signal = opts?.signal
|
|
272
|
-
const timeout = opts?.timeout
|
|
273
|
-
|
|
274
|
-
const signalPromise = signal
|
|
275
|
-
? new Promise((resolve, reject) => {
|
|
276
|
-
onAbort = () => {
|
|
277
|
-
reject(signal.reason ?? new utils.AbortError())
|
|
278
|
-
}
|
|
279
|
-
signal.addEventListener('abort', onAbort)
|
|
280
|
-
})
|
|
281
|
-
: Promise.resolve()
|
|
282
|
-
signalPromise?.catch(() => {})
|
|
273
|
+
const timeout = opts?.timeout ?? 2 * 60e3
|
|
283
274
|
|
|
275
|
+
const disposers = []
|
|
284
276
|
try {
|
|
277
|
+
const signalPromise = signal
|
|
278
|
+
? new Promise((resolve, reject) => {
|
|
279
|
+
const onAbort = () => reject(signal.reason ?? new utils.AbortError())
|
|
280
|
+
signal.addEventListener('abort', onAbort)
|
|
281
|
+
disposers.push(() => signal.removeEventListener('abort', onAbort))
|
|
282
|
+
})
|
|
283
|
+
: null
|
|
284
|
+
|
|
285
|
+
signalPromise?.catch(noop)
|
|
286
|
+
|
|
285
287
|
if (this._patching.size) {
|
|
286
|
-
|
|
287
|
-
const patching = [...this._patching.values()]
|
|
288
|
+
const promises = []
|
|
288
289
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
290
|
+
{
|
|
291
|
+
const patchingPromises = []
|
|
292
|
+
for (const callbacks of this._patching.values()) {
|
|
293
|
+
patchingPromises.push(new Promise((resolve) => callbacks.push(resolve)))
|
|
294
|
+
}
|
|
295
|
+
promises.push(patchingPromises)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (timeout) {
|
|
299
|
+
promises.push(
|
|
300
|
+
new Promise((resolve) => {
|
|
301
|
+
const patchingTimeout = timers.setTimeout(() => {
|
|
302
|
+
this._client._$onError(
|
|
303
|
+
C.TOPIC.RECORD,
|
|
304
|
+
C.EVENT.TIMEOUT,
|
|
305
|
+
new Error('sync patching timeout'),
|
|
306
|
+
)
|
|
307
|
+
resolve(null)
|
|
308
|
+
}, timeout)
|
|
309
|
+
disposers.push(() => timers.clearTimeout(patchingTimeout))
|
|
310
|
+
}),
|
|
311
|
+
)
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (signalPromise) {
|
|
315
|
+
promises.push(signalPromise)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
await Promise.race(promises)
|
|
307
319
|
}
|
|
308
320
|
|
|
309
321
|
if (this._updating.size) {
|
|
310
|
-
|
|
311
|
-
const updating = [...this._updating.values()]
|
|
322
|
+
const promises = []
|
|
312
323
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
324
|
+
{
|
|
325
|
+
const updatingPromises = []
|
|
326
|
+
for (const callbacks of this._updating.values()) {
|
|
327
|
+
updatingPromises.push(new Promise((resolve) => callbacks.push(resolve)))
|
|
328
|
+
}
|
|
329
|
+
promises.push(updatingPromises)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (timeout) {
|
|
333
|
+
promises.push(
|
|
334
|
+
new Promise((resolve) => {
|
|
335
|
+
const updatingTimeout = timers.setTimeout(() => {
|
|
336
|
+
this._client._$onError(
|
|
337
|
+
C.TOPIC.RECORD,
|
|
338
|
+
C.EVENT.TIMEOUT,
|
|
339
|
+
new Error('sync updating timeout'),
|
|
340
|
+
)
|
|
341
|
+
resolve(null)
|
|
342
|
+
}, timeout)
|
|
343
|
+
disposers.push(() => timers.clearTimeout(updatingTimeout))
|
|
344
|
+
}),
|
|
345
|
+
)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
await Promise.race(promises)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
{
|
|
352
|
+
const promises = []
|
|
353
|
+
|
|
354
|
+
promises.push(
|
|
317
355
|
new Promise((resolve) => {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
C.EVENT.TIMEOUT,
|
|
322
|
-
new Error('sync updating timeout'),
|
|
323
|
-
)
|
|
324
|
-
resolve(null)
|
|
325
|
-
}, timeout)
|
|
356
|
+
const token = xuid()
|
|
357
|
+
this._syncEmitter.once(token, resolve)
|
|
358
|
+
this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
|
|
326
359
|
}),
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
if (timeout) {
|
|
363
|
+
promises.push(
|
|
364
|
+
new Promise((resolve, reject) => {
|
|
365
|
+
const serverTimeout = timers.setTimeout(() => {
|
|
366
|
+
reject(new Error('sync server timeout'))
|
|
367
|
+
}, timeout)
|
|
368
|
+
disposers.push(() => timers.clearTimeout(serverTimeout))
|
|
369
|
+
}),
|
|
370
|
+
)
|
|
371
|
+
}
|
|
332
372
|
|
|
333
|
-
|
|
334
|
-
|
|
373
|
+
if (signalPromise) {
|
|
374
|
+
promises.push(signalPromise)
|
|
375
|
+
}
|
|
335
376
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
this._syncEmitter.once(token, resolve)
|
|
339
|
-
this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
|
|
340
|
-
}),
|
|
341
|
-
new Promise((resolve) => {
|
|
342
|
-
serverTimeout = timers.setTimeout(() => {
|
|
343
|
-
this._client._$onError(
|
|
344
|
-
C.TOPIC.RECORD,
|
|
345
|
-
C.EVENT.TIMEOUT,
|
|
346
|
-
new Error('sync server timeout'),
|
|
347
|
-
)
|
|
348
|
-
resolve(null)
|
|
349
|
-
}, timeout)
|
|
350
|
-
}),
|
|
351
|
-
signalPromise,
|
|
352
|
-
]).finally(() => {
|
|
353
|
-
timers.clearTimeout(serverTimeout)
|
|
354
|
-
})
|
|
377
|
+
await Promise.race(promises)
|
|
378
|
+
}
|
|
355
379
|
} finally {
|
|
356
|
-
|
|
357
|
-
|
|
380
|
+
for (const disposer of disposers) {
|
|
381
|
+
disposer()
|
|
358
382
|
}
|
|
359
383
|
}
|
|
360
384
|
}
|