@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/deepstream.io-client-js",
3
- "version": "28.1.16",
3
+ "version": "28.1.18",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -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 || 2 * 60e3
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
- let patchingTimeout
287
- const patching = [...this._patching.values()]
288
+ const promises = []
288
289
 
289
- await Promise.race([
290
- Promise.all(
291
- patching.map((callbacks) => new Promise((resolve) => callbacks.push(resolve))),
292
- ),
293
- new Promise((resolve) => {
294
- patchingTimeout = timers.setTimeout(() => {
295
- this._client._$onError(
296
- C.TOPIC.RECORD,
297
- C.EVENT.TIMEOUT,
298
- new Error('sync patching timeout'),
299
- )
300
- resolve(null)
301
- }, timeout)
302
- }),
303
- signalPromise,
304
- ]).finally(() => {
305
- timers.clearTimeout(patchingTimeout)
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
- let updatingTimeout
311
- const updating = [...this._updating.values()]
322
+ const promises = []
312
323
 
313
- await Promise.race([
314
- Promise.all(
315
- updating.map((callbacks) => new Promise((resolve) => callbacks.push(resolve))),
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
- updatingTimeout = timers.setTimeout(() => {
319
- this._client._$onError(
320
- C.TOPIC.RECORD,
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
- signalPromise,
328
- ]).finally(() => {
329
- timers.clearTimeout(updatingTimeout)
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
- let serverTimeout
334
- const token = xuid()
373
+ if (signalPromise) {
374
+ promises.push(signalPromise)
375
+ }
335
376
 
336
- return await Promise.race([
337
- new Promise((resolve) => {
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
- if (onAbort) {
357
- signal?.removeEventListener('abort', onAbort)
380
+ for (const disposer of disposers) {
381
+ disposer()
358
382
  }
359
383
  }
360
384
  }
@@ -78,6 +78,10 @@ class Timeout {
78
78
  clear() {
79
79
  this.state = -1
80
80
  }
81
+
82
+ [Symbol.dispose]() {
83
+ this.state = -1
84
+ }
81
85
  }
82
86
 
83
87
  export function setTimeout(callback, delay, opaque) {