@nxtedition/deepstream.io-client-js 24.0.13 → 24.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/deepstream.io-client-js",
3
- "version": "24.0.13",
3
+ "version": "24.0.15",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -226,67 +226,97 @@ class RecordHandler {
226
226
  }
227
227
  }
228
228
 
229
- async sync() {
229
+ async sync(opts) {
230
230
  // TODO (fix): Sync pending? What about VOID state?
231
231
 
232
- let patchingTimeout
233
- await Promise.race([
234
- Promise.all(
235
- [...this._patching.values()].map(
236
- (callbacks) => new Promise((resolve) => callbacks.push(resolve))
237
- )
238
- ),
239
- new Promise((resolve) => {
240
- patchingTimeout = timers.setTimeout(() => {
241
- this._client._$onError(
242
- C.TOPIC.RECORD,
243
- C.EVENT.TIMEOUT,
244
- new Error('sync patching timeout')
245
- )
246
- resolve(null)
247
- }, 2 * 60e3)
248
- }),
249
- ]).finally(() => {
250
- timers.clearTimeout(patchingTimeout)
251
- })
232
+ let onAbort
252
233
 
253
- let updatingTimeout
254
- await Promise.race([
255
- Promise.all(
256
- [...this._updating.values()].map(
257
- (callbacks) => new Promise((resolve) => callbacks.push(resolve))
258
- )
259
- ),
260
- new Promise((resolve) => {
261
- updatingTimeout = timers.setTimeout(() => {
262
- this._client._$onError(
263
- C.TOPIC.RECORD,
264
- C.EVENT.TIMEOUT,
265
- new Error('sync updating timeout')
266
- )
267
- resolve(null)
268
- }, 2 * 60e3)
269
- }),
270
- ]).finally(() => {
271
- timers.clearTimeout(updatingTimeout)
272
- })
234
+ const signal = opts?.signal
235
+ const timeout = opts?.timeout
273
236
 
274
- let serverTimeout
275
- await Promise.race([
276
- await new Promise((resolve) => {
277
- const token = xuid()
278
- this._syncEmitter.once(token, resolve)
279
- this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
280
- }),
281
- new Promise((resolve) => {
282
- serverTimeout = timers.setTimeout(() => {
283
- this._client._$onError(C.TOPIC.RECORD, C.EVENT.TIMEOUT, new Error('sync server timeout'))
284
- resolve(null)
285
- }, 2 * 60e3)
286
- }),
287
- ]).finally(() => {
288
- timers.clearTimeout(serverTimeout)
289
- })
237
+ const signalPromise = signal
238
+ ? new Promise((resolve, reject) => {
239
+ onAbort = () => {
240
+ reject(signal.reason ?? new utils.AbortError())
241
+ }
242
+ signal.addEventListener('abort', onAbort)
243
+ })
244
+ : null
245
+ signalPromise?.catch(() => {})
246
+
247
+ try {
248
+ if (this._patching.size) {
249
+ let patchingTimeout
250
+ const patching = [...this._patching.values()]
251
+ await Promise.race([
252
+ Promise.all(
253
+ patching.map((callbacks) => new Promise((resolve) => callbacks.push(resolve)))
254
+ ),
255
+ new Promise((resolve) => {
256
+ patchingTimeout = timers.setTimeout(() => {
257
+ this._client._$onError(
258
+ C.TOPIC.RECORD,
259
+ C.EVENT.TIMEOUT,
260
+ Object.assign(new Error('sync patching timeout'), { data: { patching, timeout } })
261
+ )
262
+ resolve(null)
263
+ }, timeout ?? 10 * 60e3)
264
+ }),
265
+ signalPromise,
266
+ ]).finally(() => {
267
+ timers.clearTimeout(patchingTimeout)
268
+ })
269
+ }
270
+
271
+ if (this._updating.size) {
272
+ let updatingTimeout
273
+ const updating = [...this._updating.values()]
274
+ await Promise.race([
275
+ Promise.all(
276
+ updating.map((callbacks) => new Promise((resolve) => callbacks.push(resolve)))
277
+ ),
278
+ new Promise((resolve) => {
279
+ updatingTimeout = timers.setTimeout(() => {
280
+ this._client._$onError(
281
+ C.TOPIC.RECORD,
282
+ C.EVENT.TIMEOUT,
283
+ Object.assign(new Error('sync updating timeout'), { data: { updating, timeout } })
284
+ )
285
+ resolve(null)
286
+ }, timeout ?? 10 * 60e3)
287
+ }),
288
+ signalPromise,
289
+ ]).finally(() => {
290
+ timers.clearTimeout(updatingTimeout)
291
+ })
292
+ }
293
+
294
+ let serverTimeout
295
+ const token = xuid()
296
+ await Promise.race([
297
+ await new Promise((resolve) => {
298
+ this._syncEmitter.once(token, resolve)
299
+ this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
300
+ }),
301
+ new Promise((resolve) => {
302
+ serverTimeout = timers.setTimeout(() => {
303
+ this._client._$onError(
304
+ C.TOPIC.RECORD,
305
+ C.EVENT.TIMEOUT,
306
+ Object.assign(new Error('sync server timeout'), { data: { token, timeout } })
307
+ )
308
+ resolve(null)
309
+ }, timeout ?? 10 * 60e3)
310
+ }),
311
+ signalPromise,
312
+ ]).finally(() => {
313
+ timers.clearTimeout(serverTimeout)
314
+ })
315
+ } finally {
316
+ if (onAbort) {
317
+ signal?.removeEventListener('abort', onAbort)
318
+ }
319
+ }
290
320
  }
291
321
 
292
322
  set(name, ...args) {