@nxtedition/deepstream.io-client-js 23.4.60 → 24.0.0

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": "23.4.60",
3
+ "version": "24.0.0",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -13,6 +13,46 @@ const timers = require('../utils/timers')
13
13
 
14
14
  const kEmpty = Symbol('kEmpty')
15
15
 
16
+ function noop() {}
17
+
18
+ function onUpdate(record, subscription) {
19
+ if (subscription.state && record.state < subscription.state) {
20
+ return
21
+ }
22
+
23
+ if (subscription.timeout) {
24
+ timers.clearTimeout(subscription.timeout)
25
+ subscription.timeout = null
26
+ }
27
+
28
+ const data = subscription.path ? record.get(subscription.path) : record.data
29
+
30
+ if (subscription.dataOnly) {
31
+ if (data !== subscription.data) {
32
+ subscription.data = data
33
+ subscription.subscriber.next(data)
34
+ }
35
+ } else {
36
+ subscription.subscriber.next({
37
+ name: record.name,
38
+ version: record.version,
39
+ state: record.state,
40
+ data,
41
+ })
42
+ }
43
+ }
44
+
45
+ function onTimeout(subscription) {
46
+ const expected = C.RECORD_STATE_NAME[subscription.state]
47
+ const current = C.RECORD_STATE_NAME[subscription.record.state]
48
+
49
+ subscription.subscriber.error(
50
+ Object.assign(new Error(`timeout ${subscription.record.name} [${current}<${expected}]`), {
51
+ code: 'ETIMEDOUT',
52
+ })
53
+ )
54
+ }
55
+
16
56
  class RecordHandler {
17
57
  constructor(options, connection, client) {
18
58
  this.JSON = jsonPath
@@ -261,7 +301,7 @@ class RecordHandler {
261
301
  let path
262
302
  let state = defaults ? defaults.state : undefined
263
303
  let signal
264
- let timeoutValue = defaults ? defaults.timeout : undefined
304
+ let timeout = defaults ? defaults.timeout : undefined
265
305
  let dataOnly = defaults ? defaults.dataOnly : undefined
266
306
 
267
307
  let idx = 0
@@ -282,7 +322,7 @@ class RecordHandler {
282
322
  }
283
323
 
284
324
  if (options.timeout != null) {
285
- timeoutValue = options.timeout
325
+ timeout = options.timeout
286
326
  }
287
327
 
288
328
  if (options.path != null) {
@@ -302,84 +342,52 @@ class RecordHandler {
302
342
  state = C.RECORD_STATE[state.toUpperCase()]
303
343
  }
304
344
 
305
- if (!name) {
306
- const data = path ? undefined : jsonPath.EMPTY
307
- return rxjs.of(
308
- dataOnly
309
- ? data
310
- : utils.deepFreeze({
311
- name,
312
- version: '0-00000000000000',
313
- data,
314
- state: Number.isFinite(state) ? state : C.RECORD_STATE.SERVER,
315
- })
316
- )
317
- }
318
-
319
- if (signal?.aborted) {
320
- return rxjs.throwError(() => new utils.AbortError())
321
- }
322
-
323
- return new rxjs.Observable((o) => {
324
- let timeoutHandle
325
- let prevData = kEmpty
326
-
327
- const onUpdate = (record) => {
328
- if (state && record.state < state) {
329
- return
330
- }
331
-
332
- if (timeoutHandle) {
333
- timers.clearTimeout(timeoutHandle)
334
- timeoutHandle = null
335
- }
345
+ return new rxjs.Observable((subscriber) => {
346
+ const subscription = {
347
+ subscriber,
348
+ path,
349
+ state,
350
+ signal,
351
+ dataOnly,
352
+ data: kEmpty,
353
+ timeout: null,
354
+ abort: noop,
355
+ unsubscribe() {
356
+ if (this.timeout) {
357
+ timers.clearTimeout(this.timeout)
358
+ this.timeout = null
359
+ }
336
360
 
337
- const nextData = path ? record.get(path) : record.data
361
+ if (this.ignal) {
362
+ utils.removeAbortListener(this.signal, this.abort)
363
+ this.signal = null
364
+ this.abort = noop
365
+ }
338
366
 
339
- if (dataOnly) {
340
- if (nextData !== prevData) {
341
- prevData = nextData
342
- o.next(nextData)
367
+ if (this.record) {
368
+ this.record.unsubscribe(onUpdate, this)
369
+ this.record.unref()
370
+ this.record = null
343
371
  }
344
- } else {
345
- o.next({
346
- name: record.name,
347
- version: record.version,
348
- data: nextData,
349
- state: record.state,
350
- })
351
- }
372
+ },
352
373
  }
353
374
 
354
- const record = this.getRecord(name).subscribe(onUpdate)
355
-
356
- if (timeoutValue && state && record.state < state) {
357
- timeoutHandle = timers.setTimeout(() => {
358
- const expected = C.RECORD_STATE_NAME[state]
359
- const current = C.RECORD_STATE_NAME[record.state]
360
- o.error(
361
- Object.assign(
362
- new Error(
363
- `timeout after ${timeoutValue / 1e3}s: ${record.name} [${current}<${expected}]`
364
- ),
365
- { code: 'ETIMEDOUT' }
366
- )
367
- )
368
- }, timeoutValue)
375
+ const record = this.getRecord(name).subscribe(onUpdate, subscription)
376
+
377
+ if (timeout && subscription.state && record.state < subscription.state) {
378
+ subscription.timeout = timers.setTimeout(onTimeout, timeout, subscription)
369
379
  }
370
380
 
371
381
  if (record.version) {
372
- onUpdate(record)
382
+ onUpdate(record, subscription)
373
383
  }
374
384
 
375
- const abort = signal ? () => o.error(new utils.AbortError()) : null
376
-
377
- utils.addAbortListener(signal, abort)
378
-
379
- return () => {
380
- record.unsubscribe(onUpdate).unref()
381
- utils.removeAbortListener(signal, abort)
385
+ if (subscription.signal) {
386
+ subscription.abort = () => subscription.subscriber.error(new utils.AbortError())
387
+ utils.addAbortListener(subscription.signal, subscription.abort)
382
388
  }
389
+
390
+ return subscription
383
391
  })
384
392
  }
385
393
 
@@ -53,43 +53,47 @@ class Record {
53
53
  this._handler._onPruning(this, false)
54
54
  this._subscribed = this._subscribed || this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
55
55
  }
56
-
57
56
  return this
58
57
  }
59
58
 
60
59
  unref() {
61
- invariant(this._refs > 0, 'missing refs')
62
- invariant(this._refs > 0 || !this._patching, 'must not have patches')
63
- invariant(this._refs > 0 || this._state >= C.RECORD_STATE.SERVER, 'must be ready')
64
-
65
60
  this._refs -= 1
66
61
  if (this._refs === 0) {
67
62
  this._handler._onPruning(this, true)
68
63
  }
69
-
70
64
  return this
71
65
  }
72
66
 
73
- subscribe(fn) {
67
+ subscribe(fn, opaque = null) {
74
68
  if (this._emitting) {
75
69
  this._subscriptions = this._subscriptions.slice()
76
70
  this._emitting = false
77
71
  }
78
72
 
79
- this._subscriptions.push(fn)
73
+ this._subscriptions.push(fn, opaque)
80
74
 
81
75
  return this
82
76
  }
83
77
 
84
- unsubscribe(fn) {
78
+ unsubscribe(fn, opaque = null) {
85
79
  if (this._emitting) {
86
80
  this._subscriptions = this._subscriptions.slice()
87
81
  this._emitting = false
88
82
  }
89
83
 
90
- const idx = this._subscriptions.indexOf(fn)
84
+ let idx = -1
85
+
86
+ const arr = this._subscriptions
87
+ const len = arr.length
88
+ for (let n = 0; n < len; n += 2) {
89
+ if (arr[n + 0] === fn && arr[n + 1] === opaque) {
90
+ idx = n
91
+ break
92
+ }
93
+ }
94
+
91
95
  if (idx !== -1) {
92
- this._subscriptions.splice(idx, 1)
96
+ this._subscriptions.splice(idx, 2)
93
97
  }
94
98
 
95
99
  return this
@@ -344,10 +348,10 @@ class Record {
344
348
 
345
349
  if (value) {
346
350
  this._patching = []
347
- this._refs += 1
351
+ this.ref()
348
352
  } else {
349
353
  this._patching = null
350
- this._refs -= 1
354
+ this.unref()
351
355
  }
352
356
 
353
357
  this._handler._onPatching(this, value)
@@ -358,10 +362,10 @@ class Record {
358
362
 
359
363
  if (value) {
360
364
  this._updating = new Map()
361
- this._refs += 1
365
+ this.ref()
362
366
  } else {
363
367
  this._updating = null
364
- this._refs -= 1
368
+ this.unref()
365
369
  }
366
370
 
367
371
  this._handler._onUpdating(this, value)
@@ -370,10 +374,10 @@ class Record {
370
374
  _onPending(value) {
371
375
  if (value) {
372
376
  this._pending = true
373
- this._refs += 1
377
+ this.ref()
374
378
  } else {
375
379
  this._pending = false
376
- this._refs -= 1
380
+ this.unref()
377
381
  }
378
382
 
379
383
  this._handler._onPending(this, value)
@@ -423,9 +427,12 @@ class Record {
423
427
  _emitUpdate() {
424
428
  this._emitting = true
425
429
  try {
426
- for (const fn of this._subscriptions) {
430
+ const arr = this._subscriptions
431
+ const len = arr.length
432
+
433
+ for (let n = 0; n < len; n += 2) {
427
434
  try {
428
- fn(this)
435
+ arr[n + 0](this, arr[n + 1])
429
436
  } catch (err) {
430
437
  this._error(
431
438
  C.EVENT.USER_ERROR,
@@ -143,13 +143,17 @@ module.exports.addAbortListener = function addAbortListener(signal, handler) {
143
143
  return
144
144
  }
145
145
 
146
- let handlers = abortSignals.get(signal)
147
- if (!handlers) {
148
- handlers = []
149
- abortSignals.set(signal, handlers)
150
- signal.addEventListener('abort', onAbort)
146
+ if (signal.aborted) {
147
+ queueMicrotask(handler)
148
+ } else {
149
+ let handlers = abortSignals.get(signal)
150
+ if (!handlers) {
151
+ handlers = []
152
+ abortSignals.set(signal, handlers)
153
+ signal.addEventListener('abort', onAbort)
154
+ }
155
+ handlers.push(handler)
151
156
  }
152
- handlers.push(handler)
153
157
  }
154
158
 
155
159
  module.exports.removeAbortListener = function removeAbortListener(signal, handler) {