@nxtedition/deepstream.io-client-js 31.2.3 → 31.2.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": "31.2.3",
3
+ "version": "31.2.4",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "type": "module",
@@ -9,6 +9,8 @@ import * as utils from '../utils/utils.js'
9
9
  import xuid from 'xuid'
10
10
  import * as timers from '../utils/timers.js'
11
11
 
12
+ /** @import {Timeout} from '../utils/timers.js' */
13
+
12
14
  function noop() {}
13
15
 
14
16
  const kEmpty = Symbol('kEmpty')
@@ -41,17 +43,24 @@ function onUpdate(record, subscription) {
41
43
  return
42
44
  }
43
45
 
44
- if (!subscription.synced) {
45
- return
46
- }
47
-
48
- if (subscription.state && subscription.record.state < subscription.state) {
46
+ if (!subscription.synced || subscription.record.state < subscription.state) {
47
+ if (subscription.timeoutValue > 0) {
48
+ if (!subscription.timeoutHandle) {
49
+ subscription.timeoutHandle = timers.setTimeout(
50
+ onTimeout,
51
+ subscription.timeoutValue,
52
+ subscription,
53
+ )
54
+ } else {
55
+ subscription.timeoutHandle.refresh()
56
+ }
57
+ }
49
58
  return
50
59
  }
51
60
 
52
- if (subscription.timeout) {
53
- timers.clearTimeout(subscription.timeout)
54
- subscription.timeout = null
61
+ if (subscription.timeoutHandle) {
62
+ timers.clearTimeout(subscription.timeoutHandle)
63
+ subscription.timeoutHandle = null
55
64
  }
56
65
 
57
66
  const data = subscription.path
@@ -542,11 +551,11 @@ class RecordHandler {
542
551
  _observe(defaults, name, ...args) {
543
552
  return new rxjs.Observable((subscriber) => {
544
553
  let path
545
- let state = defaults?.state
546
- let signal
547
- let timeout = defaults?.timeout
548
- let dataOnly = defaults?.dataOnly
549
- let sync = defaults?.sync
554
+ let state = defaults?.state ?? C.RECORD_STATE.CLIENT
555
+ let signal = null
556
+ let timeout = defaults?.timeout ?? 0
557
+ let dataOnly = defaults?.dataOnly ?? false
558
+ let sync = defaults?.sync ?? false
550
559
 
551
560
  let idx = 0
552
561
 
@@ -596,31 +605,61 @@ class RecordHandler {
596
605
  state = C.RECORD_STATE[state.toUpperCase()]
597
606
  }
598
607
 
608
+ // TODO (fix): Validate path?
609
+ // TODO (fix): Validate signal?
610
+
611
+ if (!Number.isInteger(state) || state < 0) {
612
+ throw new Error('invalid argument: state')
613
+ }
614
+
615
+ if (!Number.isInteger(timeout) || timeout < 0) {
616
+ throw new Error('invalid argument: timeout')
617
+ }
618
+
619
+ if (typeof dataOnly !== 'boolean') {
620
+ throw new Error('invalid argument: dataOnly')
621
+ }
622
+
623
+ if (typeof sync !== 'boolean') {
624
+ throw new Error('invalid argument: sync')
625
+ }
626
+
599
627
  // TODO (perf): Make a class
600
628
  const subscription = {
629
+ /** @readonly @type {unknown} */
601
630
  subscriber,
631
+ /** @type {Record|null} */
632
+ record: this.getRecord(name),
633
+ /** @readonly @type {unknown} */
602
634
  path,
635
+ /** @readonly @type {number} */
603
636
  state,
604
- synced: false,
637
+ /** @type {AbortSignal|null} */
605
638
  signal,
639
+ /** @readonly @type {boolean} */
606
640
  dataOnly,
607
- data: kEmpty,
608
- /** @type {NodeJS.Timeout|Timeout|null} */
609
- timeout: null,
610
- /** @type {Record?} */
611
- record: null,
641
+ /** @readonly @type {number} */
642
+ timeoutValue: timeout,
643
+
644
+ /** @type {Timeout|null} */
645
+ timeoutHandle: null,
612
646
  /** @type {Function?} */
613
647
  abort: null,
648
+ /** @type {object|Array} */
649
+ data: kEmpty,
650
+ /** @type {boolean} */
651
+ synced: false,
652
+
614
653
  unsubscribe() {
615
- if (this.timeout) {
616
- timers.clearTimeout(this.timeout)
617
- this.timeout = null
654
+ if (this.timeoutHandle) {
655
+ timers.clearTimeout(this.timeoutHandle)
656
+ this.timeoutHandle = null
618
657
  }
619
658
 
620
659
  if (this.signal) {
621
660
  utils.removeAbortListener(this.signal, this.abort)
622
- this.signal = null
623
661
  this.abort = null
662
+ this.signal = null
624
663
  }
625
664
 
626
665
  if (this.record) {
@@ -631,30 +670,22 @@ class RecordHandler {
631
670
  },
632
671
  }
633
672
 
634
- subscription.record = this.getRecord(name).subscribe(onUpdate, subscription)
635
-
636
- const record = subscription.record
637
-
638
- if (sync && record.state >= C.RECORD_STATE.SERVER) {
639
- this._sync(onSync, sync === true ? 'WEAK' : sync, subscription)
640
- } else {
641
- subscription.synced = true
642
- }
673
+ if (subscription.record) {
674
+ subscription.record.subscribe(onUpdate, subscription)
643
675
 
644
- if (timeout > 0 && state && record.state < state) {
645
- // TODO (perf): Avoid Timer allocation.
646
- subscription.timeout = timers.setTimeout(onTimeout, timeout, subscription)
676
+ if (sync && subscription.record.state >= C.RECORD_STATE.SERVER) {
677
+ this._sync(onSync, sync === true ? 'WEAK' : sync, subscription)
678
+ } else {
679
+ subscription.synced = true
680
+ }
647
681
  }
648
682
 
649
- if (signal) {
650
- // TODO (perf): Avoid abort closure allocation.
683
+ if (subscription.signal) {
651
684
  subscription.abort = () => subscriber.error(new utils.AbortError())
652
- utils.addAbortListener(signal, subscription.abort)
685
+ utils.addAbortListener(subscription.signal, subscription.abort)
653
686
  }
654
687
 
655
- if (record.version) {
656
- onUpdate(null, subscription)
657
- }
688
+ onUpdate(subscription.record, subscription)
658
689
 
659
690
  return subscription
660
691
  })
@@ -49,7 +49,7 @@ function refreshTimeout() {
49
49
  }
50
50
  }
51
51
 
52
- class Timeout {
52
+ class FastTimeout {
53
53
  constructor(callback, delay, opaque) {
54
54
  this.callback = callback
55
55
  this.delay = delay
@@ -84,14 +84,32 @@ class Timeout {
84
84
  }
85
85
  }
86
86
 
87
+ /**
88
+ * @typedef {{
89
+ * refresh: () => void,
90
+ * [Symbol.dispose]: () => void,
91
+ * }} Timeout
92
+ */
93
+
94
+ /**
95
+ * @param {(opaque?: any) => void} callback
96
+ * @param {number} delay
97
+ * @param {any} [opaque]
98
+ * @returns {Timeout}
99
+ */
87
100
  export function setTimeout(callback, delay, opaque) {
88
101
  return delay < fastNowInterval
89
- ? globalThis.setTimeout(callback, delay, opaque)
90
- : new Timeout(callback, delay, opaque)
102
+ ? opaque
103
+ ? globalThis.setTimeout(() => callback(opaque), delay)
104
+ : globalThis.setTimeout(callback, delay)
105
+ : new FastTimeout(callback, delay, opaque)
91
106
  }
92
107
 
108
+ /**
109
+ * @param {Timeout} timeout
110
+ */
93
111
  export function clearTimeout(timeout) {
94
- if (timeout instanceof Timeout) {
112
+ if (timeout instanceof FastTimeout) {
95
113
  timeout.clear()
96
114
  } else {
97
115
  globalThis.clearTimeout(timeout)
@@ -1,9 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(npm run test:types:*)"
5
- ],
6
- "deny": [],
7
- "ask": []
8
- }
9
- }