@nxtedition/deepstream.io-client-js 23.4.0 → 23.4.2

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.0",
3
+ "version": "23.4.2",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -48,7 +48,17 @@ class RecordHandler {
48
48
 
49
49
  this._client.on(C.EVENT.CONNECTED, this._onConnectionStateChange.bind(this))
50
50
 
51
+ const _prune = () => {
52
+ if (!this._pruning) {
53
+ this._pruning = true
54
+ this._schedule(prune)
55
+ }
56
+ }
57
+
51
58
  const prune = () => {
59
+ this._now = Date.now()
60
+ this._pruning = false
61
+
52
62
  let counter = 0
53
63
  for (const [rec, timestamp] of this._prune) {
54
64
  if (this._now - timestamp < 1e3) {
@@ -65,22 +75,13 @@ class RecordHandler {
65
75
  this._prune.delete(rec)
66
76
 
67
77
  if (counter++ > 1024) {
68
- this._schedule(prune)
69
- return
78
+ _prune()
70
79
  }
71
80
  }
72
-
73
- this._pruning = false
74
81
  }
75
82
 
76
- const pruneInterval = setInterval(() => {
77
- this._now = Date.now()
78
- if (!this._pruning) {
79
- this._pruning = true
80
- this._schedule(prune)
81
- }
82
- }, 1e3)
83
- pruneInterval.unref?.()
83
+ this._pruneInterval = setInterval(_prune, 1e3)
84
+ this._pruneInterval.unref?.()
84
85
  }
85
86
 
86
87
  _onRef(rec) {
@@ -154,13 +155,19 @@ class RecordHandler {
154
155
  }
155
156
 
156
157
  sync(options) {
157
- return new Promise((resolve) => {
158
+ return new Promise((resolve, reject) => {
159
+ const timeoutValue = options?.timeout ?? 2 * 60e3
160
+ const signal = options?.signal
161
+
162
+ if (signal?.aborted) {
163
+ reject(new utils.AbortError())
164
+ return
165
+ }
166
+
158
167
  let done = false
159
168
  let token
160
- let timeout
169
+ let timeoutHandle
161
170
 
162
- const timeoutValue = 2 * 60e3
163
- const signal = options?.signal
164
171
  const records = [...this._patch]
165
172
 
166
173
  const onDone = (val) => {
@@ -172,9 +179,9 @@ class RecordHandler {
172
179
 
173
180
  signal?.removeEventListener('abort', onAbort)
174
181
 
175
- if (timeout) {
176
- clearTimeout(timeout)
177
- timeout = null
182
+ if (timeoutHandle) {
183
+ clearTimeout(timeoutHandle)
184
+ timeoutHandle = null
178
185
  }
179
186
 
180
187
  if (token) {
@@ -200,8 +207,8 @@ class RecordHandler {
200
207
  const onTimeout = () => {
201
208
  const elapsed = Date.now() - this._connected
202
209
  if (elapsed < timeoutValue) {
203
- timeout = setTimeout(onTimeout, timeoutValue - elapsed)
204
- timeout.unref?.()
210
+ timeoutHandle = setTimeout(onTimeout, timeoutValue - elapsed)
211
+ timeoutHandle.unref?.()
205
212
  } else {
206
213
  for (const rec of records.filter((rec) => !rec.isReady)) {
207
214
  this._client._$onError(C.TOPIC.RECORD, C.EVENT.TIMEOUT, 'record timeout', [
@@ -222,8 +229,10 @@ class RecordHandler {
222
229
  rec.ref()
223
230
  }
224
231
 
225
- timeout = setTimeout(onTimeout, 2 * 60e3)
226
- timeout.unref?.()
232
+ if (timeoutValue) {
233
+ timeoutHandle = setTimeout(onTimeout, timeoutValue)
234
+ timeoutHandle.unref?.()
235
+ }
227
236
 
228
237
  signal?.addEventListener('abort', onAbort)
229
238
 
@@ -297,7 +306,7 @@ class RecordHandler {
297
306
  let path
298
307
  let state = defaults ? defaults.state : undefined
299
308
  let signal
300
- let timeout = defaults ? defaults.timeout : undefined
309
+ let timeoutValue = defaults ? defaults.timeout : undefined
301
310
  let dataOnly = defaults ? defaults.dataOnly : undefined
302
311
 
303
312
  let idx = 0
@@ -316,7 +325,7 @@ class RecordHandler {
316
325
  signal = options.signal
317
326
 
318
327
  if (options.timeout != null) {
319
- timeout = options.timeout
328
+ timeoutValue = options.timeout
320
329
  }
321
330
 
322
331
  if (options.path != null) {
@@ -350,7 +359,11 @@ class RecordHandler {
350
359
  )
351
360
  }
352
361
 
353
- let x$ = new rxjs.Observable((o) => {
362
+ if (signal?.aborted) {
363
+ return rxjs.throwError(() => new utils.AbortError())
364
+ }
365
+
366
+ return new rxjs.Observable((o) => {
354
367
  let timeoutHandle
355
368
  let prevData = kEmpty
356
369
 
@@ -364,8 +377,9 @@ class RecordHandler {
364
377
  timeoutHandle = null
365
378
  }
366
379
 
380
+ const nextData = path ? record.get(path) : record.data
381
+
367
382
  if (dataOnly) {
368
- const nextData = record.get(path)
369
383
  if (nextData !== prevData) {
370
384
  prevData = nextData
371
385
  o.next(nextData)
@@ -374,28 +388,25 @@ class RecordHandler {
374
388
  o.next({
375
389
  name: record.name,
376
390
  version: record.version,
377
- data: record.get(path),
391
+ data: nextData,
378
392
  state: record.state,
379
393
  })
380
394
  }
381
395
  }
382
396
 
383
- const record = this.getRecord(name)
397
+ const record = this.getRecord(name).subscribe(onUpdate)
384
398
 
385
- record.subscribe(onUpdate)
386
- record.unref()
387
-
388
- if (timeout && state && record.state < state) {
399
+ if (timeoutValue && state && record.state < state) {
389
400
  timeoutHandle = setTimeout(() => {
390
401
  const expected = C.RECORD_STATE_NAME[state]
391
402
  const current = C.RECORD_STATE_NAME[record.state]
392
403
  o.error(
393
404
  Object.assign(
394
- new Error(`timeout after ${timeout / 1e3}s: ${name} [${current}<${expected}]`),
405
+ new Error(`timeout after ${timeoutValue / 1e3}s: ${name} [${current}<${expected}]`),
395
406
  { code: 'ETIMEDOUT' }
396
407
  )
397
408
  )
398
- }, timeout)
409
+ }, timeoutValue)
399
410
  timeoutHandle.unref?.()
400
411
  }
401
412
 
@@ -403,18 +414,17 @@ class RecordHandler {
403
414
  onUpdate(record)
404
415
  }
405
416
 
406
- return () => {
407
- record.unsubscribe(onUpdate)
417
+ const abort = () => {
418
+ o.error(new utils.AbortError())
408
419
  }
409
- })
410
420
 
411
- if (signal != null) {
412
- // TODO (perf): This a slow way to implement.
413
- x$ = signal.aborted ? rxjs.EMPTY : x$.pipe(rx.takeUntil(rxjs.fromEvent(signal, 'abort')))
414
- x$ = x$.pipe(rx.throwIfEmpty(() => new utils.AbortError()))
415
- }
421
+ signal?.addEventListener('abort', abort)
416
422
 
417
- return x$
423
+ return () => {
424
+ record.unsubscribe(onUpdate).unref()
425
+ signal?.removeEventListener('abort', abort)
426
+ }
427
+ })
418
428
  }
419
429
 
420
430
  _$handle(message) {
@@ -46,34 +46,42 @@ class Record {
46
46
 
47
47
  ref() {
48
48
  this._refs += 1
49
- this._handler._onRef(this)
50
49
  if (this._refs === 1) {
51
50
  this._subscribe()
52
51
  }
52
+
53
+ this._handler._onRef(this)
54
+
55
+ return this
53
56
  }
54
57
 
55
58
  unref() {
56
59
  invariant(this._refs > 0, this._name + ' missing refs')
57
60
 
58
61
  this._refs -= 1
62
+
59
63
  this._handler._onRef(this)
64
+
65
+ return this
60
66
  }
61
67
 
62
68
  subscribe(fn) {
63
69
  this._subscriptions.push(fn)
64
- this.ref()
70
+
71
+ return this
65
72
  }
66
73
 
67
74
  unsubscribe(fn) {
68
75
  const idx = this._subscriptions.indexOf(fn)
69
76
  if (idx !== -1) {
70
77
  this._subscriptions.splice(idx, 1)
71
- this.unref()
72
78
  }
79
+
80
+ return this
73
81
  }
74
82
 
75
83
  get(path) {
76
- return jsonPath.get(this._data, path)
84
+ return path ? jsonPath.get(this._data, path) : this._data
77
85
  }
78
86
 
79
87
  set(pathOrData, dataOrNil) {
@@ -112,6 +120,7 @@ class Record {
112
120
  }
113
121
  }
114
122
 
123
+ // TODO (fix): timeout + signal
115
124
  when(stateOrNull) {
116
125
  invariant(this._refs > 0, this._name + ' missing refs')
117
126
 
@@ -132,11 +141,13 @@ class Record {
132
141
  return
133
142
  }
134
143
 
144
+ this.unref()
135
145
  this.unsubscribe(onUpdate)
136
146
 
137
147
  resolve(null)
138
148
  }
139
149
 
150
+ this.ref()
140
151
  this.subscribe(onUpdate)
141
152
  })
142
153
  }