@nxtedition/deepstream.io-client-js 32.0.25 → 32.0.27

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.25",
3
+ "version": "32.0.27",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -103,11 +103,15 @@ Connection.prototype._createEndpoint = function () {
103
103
  this._endpoint = new BrowserWebSocket(this._url)
104
104
  this._endpoint.binaryType = 'arraybuffer'
105
105
  this._endpoint.onmessage = ({ data }) => {
106
- let buf = new Uint8Array(data)
107
- if (buf[0] > 128) {
108
- buf = buf.subarray(buf[0] - 128)
106
+ if (typeof data === 'string') {
107
+ this._onMessage(data)
108
+ } else {
109
+ let buf = new Uint8Array(data)
110
+ if (buf[0] > 128) {
111
+ buf = buf.subarray(buf[0] - 128)
112
+ }
113
+ this._onMessage(decoder.decode(buf))
109
114
  }
110
- this._onMessage(decoder.decode(buf))
111
115
  }
112
116
  }
113
117
 
@@ -147,10 +151,11 @@ Connection.prototype.send = function (message) {
147
151
  }
148
152
 
149
153
  if (this._endpoint._socket && !this._corked) {
150
- this._endpoint._socket.cork()
154
+ const socket = this._endpoint._socket
151
155
  this._corked = true
156
+ socket.cork()
152
157
  setTimeout(() => {
153
- this._endpoint._socket.uncork()
158
+ socket.uncork()
154
159
  this._corked = false
155
160
  }, 1)
156
161
  }
@@ -43,17 +43,10 @@ function onUpdate(record, subscription) {
43
43
  }
44
44
 
45
45
  if (!subscription.synced || subscription.record.state < subscription.state) {
46
- if (subscription.timeoutValue > 0) {
47
- if (!subscription.timeout) {
48
- subscription.timeout = timers.setTimeout(onTimeout, subscription.timeoutValue, subscription)
49
- } else {
50
- subscription.timeout.refresh()
51
- }
52
- }
53
46
  return
54
47
  }
55
48
 
56
- if (subscription.timeout) {
49
+ if (subscription.timeout != null) {
57
50
  timers.clearTimeout(subscription.timeout)
58
51
  subscription.timeout = null
59
52
  }
@@ -78,6 +71,10 @@ function onUpdate(record, subscription) {
78
71
  }
79
72
 
80
73
  function onTimeout(subscription) {
74
+ if (!subscription.record) {
75
+ return
76
+ }
77
+
81
78
  const expected = C.RECORD_STATE_NAME[subscription.state]
82
79
  const current = C.RECORD_STATE_NAME[subscription.record.state]
83
80
 
@@ -429,7 +426,7 @@ class RecordHandler {
429
426
  throw new Error('invalid argument: data')
430
427
  }
431
428
 
432
- if (parent != null && (typeof version !== 'string' || !/^\d+-/.test(version))) {
429
+ if (parent != null && (typeof parent !== 'string' || !/^\d+-/.test(parent))) {
433
430
  throw new Error('invalid argument: parent')
434
431
  }
435
432
 
@@ -623,6 +620,11 @@ class RecordHandler {
623
620
  }
624
621
 
625
622
  return new rxjs.Observable((subscriber) => {
623
+ if (signal?.aborted) {
624
+ subscriber.error(new utils.AbortError())
625
+ return
626
+ }
627
+
626
628
  // TODO (perf): Make a class
627
629
  const subscription = {
628
630
  /** @readonly @type {unknown} */
@@ -685,6 +687,10 @@ class RecordHandler {
685
687
  } else {
686
688
  onSync(subscription)
687
689
  }
690
+
691
+ if (timeout > 0 && (!subscription.synced || subscription.record.state < subscription.state)) {
692
+ subscription.timeout = timers.setTimeout(onTimeout, timeout, subscription)
693
+ }
688
694
  })
689
695
  }
690
696
 
@@ -19,14 +19,16 @@ class Record {
19
19
  this._data = jsonPath.EMPTY
20
20
  this._state = C.RECORD_STATE.VOID
21
21
  this._refs = 0
22
- this._subscriptions = []
22
+ /** @type {Array|null} */
23
+ this._subscriptions = null
23
24
 
24
25
  /** @type {Array|null} */
25
26
  this._emittingArr = null
26
27
  /** @type {number} */
27
28
  this._emittingIndex = -1
28
29
 
29
- this._observers = []
30
+ /** @type {Array|null} */
31
+ this._observers = null
30
32
 
31
33
  /** @type Map? */ this._updating = null
32
34
  /** @type Array? */ this._patching = null
@@ -94,6 +96,8 @@ class Record {
94
96
  * @returns {Record}
95
97
  */
96
98
  subscribe(fn, opaque = null) {
99
+ this._subscriptions ??= []
100
+
97
101
  if (this._emittingArr == this._subscriptions) {
98
102
  this._subscriptions = this._subscriptions.slice()
99
103
  }
@@ -110,6 +114,10 @@ class Record {
110
114
  * @returns {Record}
111
115
  */
112
116
  unsubscribe(fn, opaque = null) {
117
+ if (!this._subscriptions) {
118
+ return this
119
+ }
120
+
113
121
  if (this._emittingArr == this._subscriptions) {
114
122
  this._subscriptions = this._subscriptions.slice()
115
123
  }
@@ -137,6 +145,8 @@ class Record {
137
145
  * @param {{ index: number, onUpdate: (Record) => void}} subscription
138
146
  */
139
147
  _observe(subscription) {
148
+ this._observers ??= []
149
+
140
150
  if (subscription.index != null && subscription.index !== -1) {
141
151
  throw new Error('already observing')
142
152
  }
@@ -148,6 +158,10 @@ class Record {
148
158
  * @param {{ index: number, onUpdate: (Record) => void}} subscription
149
159
  */
150
160
  _unobserve(subscription) {
161
+ if (!this._observers) {
162
+ return this
163
+ }
164
+
151
165
  if (subscription.index == null || subscription.index === -1) {
152
166
  throw new Error('not observing')
153
167
  }
@@ -229,7 +243,7 @@ class Record {
229
243
  when(stateOrNil, optionsOrNil) {
230
244
  invariant(this._refs > 0, 'missing refs')
231
245
 
232
- if (stateOrNil != null && stateOrNil === 'object') {
246
+ if (stateOrNil != null && typeof stateOrNil === 'object') {
233
247
  optionsOrNil = stateOrNil
234
248
  stateOrNil = optionsOrNil?.state
235
249
  }
@@ -252,9 +266,26 @@ class Record {
252
266
  return
253
267
  }
254
268
 
255
- let timeoutHandle
269
+ const context = {
270
+ name: 'when',
271
+ /** @type {timers.Timeout|NodeJS.Timeout|null} */
272
+ timeout: null,
273
+ /** @type {AbortSignal|null} */
274
+ signal: null,
275
+ done: false,
276
+ }
277
+
278
+ const onAbort = (e) => {
279
+ onDone(signal.reason ?? new utils.AbortError())
280
+ }
256
281
 
257
282
  const onDone = (err) => {
283
+ if (context.done) {
284
+ return
285
+ }
286
+
287
+ context.done = true
288
+
258
289
  if (err) {
259
290
  reject(err)
260
291
  } else {
@@ -262,14 +293,17 @@ class Record {
262
293
  }
263
294
 
264
295
  this.unref()
265
- this.unsubscribe(onUpdate)
296
+ this.unsubscribe(onUpdate, context)
266
297
 
267
- if (timeoutHandle) {
268
- timers.clearTimeout(timeoutHandle)
269
- timeoutHandle = null
298
+ if (context.timeout != null) {
299
+ timers.clearTimeout(context.timeout)
300
+ context.timeout = null
270
301
  }
271
302
 
272
- signal?.removeEventListener('abort', onAbort)
303
+ if (context.signal != null) {
304
+ context.signal.removeEventListener('abort', onAbort)
305
+ context.signal = null
306
+ }
273
307
  }
274
308
 
275
309
  const onUpdate = () => {
@@ -278,14 +312,8 @@ class Record {
278
312
  }
279
313
  }
280
314
 
281
- const onAbort = signal
282
- ? () => {
283
- onDone(signal.reason ?? new utils.AbortError())
284
- }
285
- : null
286
-
287
315
  if (timeout > 0) {
288
- timeoutHandle = timers.setTimeout(() => {
316
+ context.timeout = timers.setTimeout(() => {
289
317
  const expected = C.RECORD_STATE_NAME[state]
290
318
  const current = C.RECORD_STATE_NAME[this._state]
291
319
 
@@ -297,10 +325,13 @@ class Record {
297
325
  }, timeout)
298
326
  }
299
327
 
300
- signal?.addEventListener('abort', onAbort)
328
+ if (signal) {
329
+ context.signal = signal
330
+ signal?.addEventListener('abort', onAbort)
331
+ }
301
332
 
302
333
  this.ref()
303
- this.subscribe(onUpdate)
334
+ this.subscribe(onUpdate, context)
304
335
  })
305
336
  }
306
337
 
@@ -558,34 +589,38 @@ class Record {
558
589
  throw new Error('cannot reenter emitUpdate')
559
590
  }
560
591
 
561
- try {
562
- const arr = this._subscriptions
563
- const len = arr.length
564
-
565
- this._emittingArr = arr
566
- for (let n = 0; n < len; n += 2) {
567
- this._emittingIndex = n
568
- // TODO (fix): What if this throws?
569
- arr[n + 0](this, arr[n + 1])
592
+ if (this._subscriptions != null) {
593
+ try {
594
+ const arr = this._subscriptions
595
+ const len = arr.length
596
+
597
+ this._emittingArr = arr
598
+ for (let n = 0; n < len; n += 2) {
599
+ this._emittingIndex = n
600
+ // TODO (fix): What if this throws?
601
+ arr[n + 0](this, arr[n + 1])
602
+ }
603
+ } finally {
604
+ this._emittingArr = null
605
+ this._emittingIndex = -1
570
606
  }
571
- } finally {
572
- this._emittingArr = null
573
- this._emittingIndex = -1
574
607
  }
575
608
 
576
- try {
577
- const arr = this._observers
578
- const len = arr.length
579
-
580
- this._emittingArr = arr
581
- for (let n = 0; n < len; n++) {
582
- this._emittingIndex = n
583
- // TODO (fix): What if this throws?
584
- arr[n].onUpdate(this, arr[n])
609
+ if (this._observers != null) {
610
+ try {
611
+ const arr = this._observers
612
+ const len = arr.length
613
+
614
+ this._emittingArr = arr
615
+ for (let n = 0; n < len; n++) {
616
+ this._emittingIndex = n
617
+ // TODO (fix): What if this throws?
618
+ arr[n].onUpdate(this, arr[n])
619
+ }
620
+ } finally {
621
+ this._emittingArr = null
622
+ this._emittingIndex = -1
585
623
  }
586
- } finally {
587
- this._emittingArr = null
588
- this._emittingIndex = -1
589
624
  }
590
625
 
591
626
  this._handler._client.emit('recordUpdated', this)
@@ -168,10 +168,11 @@ RpcHandler.prototype._onConnectionStateChange = function (connected) {
168
168
  }
169
169
  } else {
170
170
  const err = Object.assign(new Error('socket hang up'), { code: 'ECONNRESET' })
171
- for (const rpc of this._rpcs.values()) {
171
+ const rpcs = this._rpcs
172
+ this._rpcs = new Map()
173
+ for (const rpc of rpcs.values()) {
172
174
  rpc.callback(err)
173
175
  }
174
- this._rpcs.clear()
175
176
  }
176
177
  }
177
178
 
@@ -157,6 +157,7 @@ export default class Listener {
157
157
  } catch (err) {
158
158
  const bigIntPaths = /BigInt/.test(err.message) ? findBigIntPaths(value) : undefined
159
159
  this._error(
160
+ provider.name,
160
161
  Object.assign(new Error(`invalid value: ${value}`), {
161
162
  cause: err,
162
163
  data: { name: provider.name, bigIntPaths },
@@ -157,6 +157,7 @@ export default class Listener {
157
157
  } catch (err) {
158
158
  const bigIntPaths = /BigInt/.test(err.message) ? findBigIntPaths(value) : undefined
159
159
  this._error(
160
+ provider.name,
160
161
  Object.assign(new Error(`invalid value: ${value}`), {
161
162
  cause: err,
162
163
  data: { name: provider.name, bigIntPaths },