@nxtedition/deepstream.io-client-js 30.0.0 → 31.0.0

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.
@@ -1,59 +1,30 @@
1
+ import * as rxjs from 'rxjs'
1
2
  import * as C from '../constants/constants.js'
2
- import { h64ToString } from '../utils/utils.js'
3
-
4
- class Observer {
5
- #name
6
- #key
7
- #listener
8
- #version = ''
9
-
10
- constructor(name, listener) {
11
- this.#name = name
12
- this.#key = h64ToString(name)
13
- this.#listener = listener
14
- }
3
+ import { h64ToString, findBigIntPaths } from '../utils/utils.js'
15
4
 
16
- next(value) {
17
- let data
18
- if (value && typeof value === 'string') {
5
+ const PIPE = rxjs.pipe(
6
+ rxjs.map((value) => {
7
+ if (typeof value === 'string') {
19
8
  if (value.charAt(0) !== '{' && value.charAt(0) !== '[') {
20
9
  throw new Error(`invalid value: ${value}`)
21
10
  }
22
- data = value
23
- } else if (value && typeof value === 'object') {
24
- data = JSON.stringify(value)
25
- } else if (data != null) {
26
- throw new Error(`invalid value: ${value}`)
27
- }
28
-
29
- const version = data ? `INF-${h64ToString(data)}` : ''
30
- if (this.#version === version) {
31
- return
32
- }
33
-
34
- if (version) {
35
- this.#listener._connection.sendMsg(this.#listener._topic, C.ACTIONS.UPDATE, [
36
- this.#key,
37
- version,
38
- data,
39
- ])
11
+ return value
12
+ } else if (value != null && typeof value === 'object') {
13
+ try {
14
+ return JSON.stringify(value)
15
+ } catch (err) {
16
+ const bigIntPaths = /BigInt/.test(err.message) ? findBigIntPaths(value) : undefined
17
+ throw Object.assign(new Error(`invalid value: ${value}`), {
18
+ cause: err,
19
+ data: { bigIntPaths },
20
+ })
21
+ }
40
22
  } else {
41
- this.#listener._connection.sendMsg(this.#listener._topic, C.ACTIONS.LISTEN_REJECT, [
42
- this.#listener._pattern,
43
- this.#key,
44
- ])
23
+ throw new Error(`invalid value: ${value}`)
45
24
  }
46
-
47
- this.#version = version
48
- }
49
- error(err) {
50
- this.#listener._error(this.#name, err)
51
- this.#listener._connection.sendMsg(this.#listener._topic, C.ACTIONS.LISTEN_REJECT, [
52
- this.#listener._pattern,
53
- this.#key,
54
- ])
55
- }
56
- }
25
+ }),
26
+ rxjs.distinctUntilChanged(),
27
+ )
57
28
 
58
29
  export default class Listener {
59
30
  constructor(topic, pattern, callback, handler, opts) {
@@ -99,11 +70,24 @@ export default class Listener {
99
70
  try {
100
71
  value$ = this._callback(name)
101
72
  } catch (err) {
102
- this._error(name, err)
73
+ value$ = rxjs.throwError(() => err)
103
74
  }
104
75
 
105
76
  if (value$) {
106
- this._subscriptions.set(name, value$.subscribe(new Observer(name, this)))
77
+ const subscription = value$.pipe(PIPE).subscribe({
78
+ next: (data) => {
79
+ const version = `INF-${h64ToString(data)}`
80
+ this._connection.sendMsg(this._topic, C.ACTIONS.UPDATE, [name, version, data])
81
+ },
82
+ error: (err) => {
83
+ this._error(name, err)
84
+
85
+ this._subscriptions.delete(name)
86
+ this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
87
+ },
88
+ })
89
+
90
+ this._subscriptions.set(name, subscription)
107
91
  } else {
108
92
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
109
93
  }
@@ -177,8 +177,27 @@ export function removeAbortListener(signal, handler) {
177
177
  }
178
178
  }
179
179
 
180
- const HASHER = await xxhash()
180
+ // This is a hack to avoid top-level await
181
+ // const HASHER = await xxhash()
182
+ let HASHER
183
+ xxhash().then((hasher) => (HASHER = hasher))
181
184
 
182
185
  export function h64ToString(str) {
183
186
  return HASHER.h64ToString(str)
184
187
  }
188
+
189
+ export function findBigIntPaths(obj, path = "") {
190
+ const paths = []
191
+
192
+ if (typeof obj === "bigint") {
193
+ return [path]
194
+ }
195
+
196
+ if (typeof obj === "object" && obj !== null) {
197
+ for (const key of Object.keys(obj)) {
198
+ paths.push(...findBigIntPaths(obj[key], path ? `${path}.${key}` : key))
199
+ }
200
+ }
201
+
202
+ return paths
203
+ }