@nxtedition/deepstream.io-client-js 32.0.2 → 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.2",
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} */
@@ -167,13 +169,23 @@ class RecordHandler {
167
169
  this._pruning = new Set()
168
170
 
169
171
  for (const rec of pruning) {
170
- rec._$dispose()
171
- this._records.delete(rec.name)
172
+ try {
173
+ rec._$dispose()
174
+ if (!this._records.delete(rec.name)) {
175
+ this._client._$onError(
176
+ C.TOPIC.RECORD,
177
+ C.EVENT.INTERNAL_ERROR,
178
+ `failed to delete pruned record: ${rec.name}`,
179
+ )
180
+ }
181
+ } catch (err) {
182
+ this._client._$onError(C.TOPIC.RECORD, C.EVENT.INTERNAL_ERROR, err)
183
+ }
172
184
  }
173
185
 
174
- this._stats.pruning -= pruning.size
175
- this._stats.records -= pruning.size
176
186
  this._stats.destroyed += pruning.size
187
+ this._stats.pruning = this._pruning.size
188
+ this._stats.records = this._records.size
177
189
 
178
190
  this._pruningTimeout.refresh()
179
191
  }
@@ -183,12 +195,11 @@ class RecordHandler {
183
195
 
184
196
  _onPruning(rec, value) {
185
197
  if (value) {
186
- this._stats.pruning += 1
187
198
  this._pruning.add(rec)
188
199
  } else {
189
- this._stats.pruning -= 1
190
200
  this._pruning.delete(rec)
191
201
  }
202
+ this._stats.pruning = this._pruning.size
192
203
  }
193
204
 
194
205
  _onUpdating(rec, value) {
@@ -211,17 +222,15 @@ class RecordHandler {
211
222
 
212
223
  _onPatching(rec, value) {
213
224
  if (value) {
214
- this._stats.patching += 1
215
225
  this._patching.set(rec, [])
216
226
  } else {
217
- this._stats.patching -= 1
218
-
219
227
  const callbacks = this._patching.get(rec)
220
228
  this._patching.delete(rec)
221
229
  for (const callback of callbacks) {
222
230
  callback()
223
231
  }
224
232
  }
233
+ this._stats.patching = this._patching.size
225
234
  }
226
235
 
227
236
  get connected() {
@@ -261,9 +270,9 @@ class RecordHandler {
261
270
  let record = this._records.get(name)
262
271
  if (!record) {
263
272
  record = new Record(name, this)
264
- this._stats.records += 1
265
- this._stats.created += 1
266
273
  this._records.set(name, record)
274
+ this._stats.created += 1
275
+ this._stats.records = this._records.size
267
276
  } else {
268
277
  record.ref()
269
278
  }
@@ -590,6 +599,7 @@ class RecordHandler {
590
599
  let timeout = defaults?.timeout ?? 0
591
600
  let dataOnly = defaults?.dataOnly ?? false
592
601
  let sync = defaults?.sync ?? false
602
+ let key
593
603
 
594
604
  let idx = 0
595
605
 
@@ -633,6 +643,10 @@ class RecordHandler {
633
643
  if (options.sync !== undefined) {
634
644
  sync = options.sync
635
645
  }
646
+
647
+ if (options.key !== undefined) {
648
+ key = options.key
649
+ }
636
650
  }
637
651
 
638
652
  if (typeof state === 'string') {
@@ -663,6 +677,7 @@ class RecordHandler {
663
677
 
664
678
  const subscription = new Subscription()
665
679
 
680
+ subscription.key = key
666
681
  subscription.subscriber = subscriber
667
682
  subscription.path = path
668
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