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