@nxtedition/deepstream.io-client-js 32.0.3 → 32.0.4

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": "32.0.3",
3
+ "version": "32.0.4",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -95,6 +95,8 @@ function onTimeout(subscription) {
95
95
  }
96
96
 
97
97
  class Subscription {
98
+ /** @type {unknown} */
99
+ key = null
98
100
  /** @type {unknown} */
99
101
  subscriber = null
100
102
  /** @type {unknown} */
@@ -597,6 +599,7 @@ class RecordHandler {
597
599
  let timeout = defaults?.timeout ?? 0
598
600
  let dataOnly = defaults?.dataOnly ?? false
599
601
  let sync = defaults?.sync ?? false
602
+ let key
600
603
 
601
604
  let idx = 0
602
605
 
@@ -640,6 +643,10 @@ class RecordHandler {
640
643
  if (options.sync !== undefined) {
641
644
  sync = options.sync
642
645
  }
646
+
647
+ if (options.key !== undefined) {
648
+ key = options.key
649
+ }
643
650
  }
644
651
 
645
652
  if (typeof state === 'string') {
@@ -670,6 +677,7 @@ class RecordHandler {
670
677
 
671
678
  const subscription = new Subscription()
672
679
 
680
+ subscription.key = key
673
681
  subscription.subscriber = subscriber
674
682
  subscription.path = path
675
683
  subscription.state = state
@@ -19,14 +19,17 @@ class Record {
19
19
  this._data = jsonPath.EMPTY_OBJ
20
20
  this._state = C.RECORD_STATE.VOID
21
21
  this._refs = 1
22
- this._subscriptions = []
22
+
23
+ /** @type {Array|null} */
24
+ this._subscriptions = null
23
25
 
24
26
  /** @type {Array|null} */
25
27
  this._emittingArr = null
26
28
  /** @type {number} */
27
29
  this._emittingIndex = -1
28
30
 
29
- this._observers = []
31
+ /** @type {Array|null} */
32
+ this._observers = null
30
33
 
31
34
  /** @type Map? */ this._updating = null
32
35
  /** @type Array? */ this._patching = null
@@ -94,6 +97,8 @@ class Record {
94
97
  * @returns {Record}
95
98
  */
96
99
  subscribe(fn, opaque = null) {
100
+ this._subscriptions ??= []
101
+
97
102
  if (this._emittingArr == this._subscriptions) {
98
103
  this._subscriptions = this._subscriptions.slice()
99
104
  }
@@ -110,6 +115,10 @@ class Record {
110
115
  * @returns {Record}
111
116
  */
112
117
  unsubscribe(fn, opaque = null) {
118
+ if (!this._subscriptions) {
119
+ return this
120
+ }
121
+
113
122
  if (this._emittingArr == this._subscriptions) {
114
123
  this._subscriptions = this._subscriptions.slice()
115
124
  }
@@ -137,6 +146,8 @@ class Record {
137
146
  * @param {{ index: number, onUpdate: (Record) => void}} subscription
138
147
  */
139
148
  _observe(subscription) {
149
+ this._observers ??= []
150
+
140
151
  if (subscription.index != null && subscription.index !== -1) {
141
152
  throw new Error('already observing')
142
153
  }
@@ -153,6 +164,10 @@ class Record {
153
164
  * @param {{ index: number, onUpdate: (Record) => void}} subscription
154
165
  */
155
166
  _unobserve(subscription) {
167
+ if (!this._observers) {
168
+ return this
169
+ }
170
+
156
171
  if (subscription.index == null || subscription.index === -1) {
157
172
  throw new Error('not observing')
158
173
  }
@@ -242,6 +257,7 @@ class Record {
242
257
  const signal = optionsOrNil?.signal
243
258
  const state = stateOrNil ?? C.RECORD_STATE.SERVER
244
259
  const timeout = optionsOrNil?.timeout ?? 2 * 60e3
260
+ const key = optionsOrNil?.key ?? 'when'
245
261
 
246
262
  if (signal?.aborted) {
247
263
  return Promise.reject(signal.reason || new utils.AbortError())
@@ -257,9 +273,34 @@ class Record {
257
273
  return
258
274
  }
259
275
 
260
- let timeoutHandle
276
+ const subscription = {
277
+ key,
278
+ /** @type {timers.Timeout|NodeJS.Timeout|null} */
279
+ timeout: null,
280
+ /** @type {AbortSignal|null} */
281
+ signal: null,
282
+ done: false,
283
+
284
+ state,
285
+ index: -1,
286
+ onUpdate(record, subscription) {
287
+ if (record._state >= subscription.state) {
288
+ onDone(null)
289
+ }
290
+ },
291
+ }
292
+
293
+ const onAbort = (e) => {
294
+ onDone(signal.reason ?? new utils.AbortError())
295
+ }
261
296
 
262
297
  const onDone = (err) => {
298
+ if (subscription.done) {
299
+ return
300
+ }
301
+
302
+ subscription.done = true
303
+
263
304
  if (err) {
264
305
  reject(err)
265
306
  } else {
@@ -267,30 +308,21 @@ class Record {
267
308
  }
268
309
 
269
310
  this.unref()
270
- this.unsubscribe(onUpdate)
311
+ this._unobserve(subscription)
271
312
 
272
- if (timeoutHandle) {
273
- timers.clearTimeout(timeoutHandle)
274
- timeoutHandle = null
313
+ if (subscription.timeout != null) {
314
+ timers.clearTimeout(subscription.timeout)
315
+ subscription.timeout = null
275
316
  }
276
317
 
277
- signal?.removeEventListener('abort', onAbort)
278
- }
279
-
280
- const onUpdate = () => {
281
- if (this._state >= state) {
282
- onDone(null)
318
+ if (subscription.signal != null) {
319
+ subscription.signal.removeEventListener('abort', onAbort)
320
+ subscription.signal = null
283
321
  }
284
322
  }
285
323
 
286
- const onAbort = signal
287
- ? () => {
288
- onDone(signal.reason ?? new utils.AbortError())
289
- }
290
- : null
291
-
292
324
  if (timeout > 0) {
293
- timeoutHandle = timers.setTimeout(() => {
325
+ subscription.timeout = timers.setTimeout(() => {
294
326
  const expected = C.RECORD_STATE_NAME[state]
295
327
  const current = C.RECORD_STATE_NAME[this._state]
296
328
 
@@ -302,10 +334,13 @@ class Record {
302
334
  }, timeout)
303
335
  }
304
336
 
305
- signal?.addEventListener('abort', onAbort)
337
+ if (signal) {
338
+ subscription.signal = signal
339
+ signal?.addEventListener('abort', onAbort)
340
+ }
306
341
 
307
342
  this.ref()
308
- this.subscribe(onUpdate)
343
+ this._observe(subscription)
309
344
  })
310
345
  }
311
346