@nxtedition/deepstream.io-client-js 24.1.1 → 24.1.3

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": "24.1.1",
3
+ "version": "24.1.3",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -318,11 +318,14 @@ Connection.prototype._setState = function (state) {
318
318
  return
319
319
  }
320
320
  this._state = state
321
+ this.emit(C.EVENT.CONNECTION_STATE_CHANGED, state)
321
322
  this._client.emit(C.EVENT.CONNECTION_STATE_CHANGED, state)
322
323
 
323
324
  if (state === C.CONNECTION_STATE.OPEN) {
325
+ this.emit(C.EVENT.CONNECTED, true)
324
326
  this._client.emit(C.EVENT.CONNECTED, true)
325
327
  } else if (state === C.CONNECTION_STATE.RECONNECTING || state === C.CONNECTION_STATE.CLOSED) {
328
+ this.emit(C.EVENT.CONNECTED, false)
326
329
  this._client.emit(C.EVENT.CONNECTED, false)
327
330
  }
328
331
  }
@@ -5,6 +5,7 @@ const messageParser = require('../message/message-parser')
5
5
  const xuid = require('xuid')
6
6
  const invariant = require('invariant')
7
7
  const cloneDeep = require('lodash.clonedeep')
8
+ const timers = require('../utils/timers')
8
9
 
9
10
  class Record {
10
11
  static STATE = C.RECORD_STATE
@@ -185,17 +186,33 @@ class Record {
185
186
 
186
187
  const signal = optionsOrNil?.signal
187
188
  const state = stateOrNil ?? C.RECORD_STATE.SERVER
189
+ const timeout = optionsOrNil?.timeout ?? 2 * 60e3
188
190
 
189
191
  if (!Number.isFinite(state) || state < 0) {
190
192
  throw new Error('invalid argument: state')
191
193
  }
192
194
 
193
- await new Promise((resolve) => {
195
+ await new Promise((resolve, reject) => {
194
196
  if (this._state >= state) {
195
197
  resolve(null)
196
198
  return
197
199
  }
198
200
 
201
+ let timeoutHandle
202
+
203
+ if (timeout > 0) {
204
+ timeoutHandle = timers.setTimeout(() => {
205
+ const expected = C.RECORD_STATE_NAME[state]
206
+ const current = C.RECORD_STATE_NAME[this._state]
207
+
208
+ reject(
209
+ Object.assign(new Error(`timeout ${this.name} [${current}<${expected}]`), {
210
+ code: 'ETIMEDOUT',
211
+ })
212
+ )
213
+ }, timeout)
214
+ }
215
+
199
216
  const onUpdate = () => {
200
217
  if (this._state < state) {
201
218
  return
@@ -204,6 +221,11 @@ class Record {
204
221
  this.unref()
205
222
  this.unsubscribe(onUpdate)
206
223
 
224
+ if (timeoutHandle) {
225
+ timers.clearTimeout(timeoutHandle)
226
+ timeoutHandle = null
227
+ }
228
+
207
229
  resolve(null)
208
230
  }
209
231
 
@@ -13,11 +13,11 @@ class Listener {
13
13
  this._recursive = recursive
14
14
  this._stringify = stringify || JSON.stringify
15
15
 
16
- this._$onConnectionStateChange()
16
+ this._$onConnectionStateChange(this._connection.connected)
17
17
  }
18
18
 
19
19
  get connected() {
20
- return this._client.getConnectionState() === C.CONNECTION_STATE.OPEN
20
+ return this._connection.connected
21
21
  }
22
22
 
23
23
  get stats() {
@@ -209,8 +209,8 @@ class Listener {
209
209
  return true
210
210
  }
211
211
 
212
- _$onConnectionStateChange() {
213
- if (this.connected) {
212
+ _$onConnectionStateChange(connected) {
213
+ if (connected) {
214
214
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN, [this._pattern])
215
215
  } else {
216
216
  this._reset()
@@ -2,42 +2,47 @@ const C = require('../constants/constants')
2
2
  const rx = require('rxjs/operators')
3
3
  const rxjs = require('rxjs')
4
4
 
5
+ const PIPE = rxjs.pipe(
6
+ rx.map((value) => {
7
+ let data
8
+ if (value && typeof value === 'string') {
9
+ if (value.charAt(0) !== '{' && value.charAt(0) !== '[') {
10
+ throw new Error(`invalid value: ${value}`)
11
+ }
12
+ data = value
13
+ } else if (value && typeof value === 'object') {
14
+ data = JSON.stringify(value)
15
+ } else if (data != null) {
16
+ throw new Error(`invalid value: ${value}`)
17
+ }
18
+
19
+ return data
20
+ }),
21
+ rx.distinctUntilChanged()
22
+ )
23
+
5
24
  class Listener {
6
- constructor(topic, pattern, callback, handler, { stringify = null, recursive = false } = {}) {
25
+ constructor(topic, pattern, callback, handler, opts) {
26
+ if (opts.recursive) {
27
+ throw new Error('invalid argument: recursive')
28
+ }
29
+ if (opts.stringify) {
30
+ throw new Error('invalid argument: stringify')
31
+ }
32
+
7
33
  this._topic = topic
8
34
  this._pattern = pattern
9
35
  this._callback = callback
10
36
  this._handler = handler
11
37
  this._client = this._handler._client
12
38
  this._connection = this._handler._connection
13
- this._connected = false
14
39
  this._subscriptions = new Map()
15
- this._stringify = stringify || JSON.stringify
16
-
17
- this._pipe = rxjs.pipe(
18
- rx.map((value) => {
19
- let data
20
- if (value && typeof value === 'string') {
21
- if (value.charAt(0) !== '{' && value.charAt(0) !== '[') {
22
- throw new Error(`invalid value: ${value}`)
23
- }
24
- data = value
25
- } else if (value && typeof value === 'object') {
26
- data = this._stringify(value)
27
- } else if (data != null) {
28
- throw new Error(`invalid value: ${value}`)
29
- }
30
-
31
- return data
32
- }),
33
- rx.distinctUntilChanged()
34
- )
35
-
36
- this._$onConnectionStateChange()
37
-
38
- if (recursive) {
39
- throw new Error('invalid argument: recursive')
40
- }
40
+
41
+ this._$onConnectionStateChange(this._connection.connected)
42
+ }
43
+
44
+ get connected() {
45
+ return this._connection.connected
41
46
  }
42
47
 
43
48
  get stats() {
@@ -49,13 +54,13 @@ class Listener {
49
54
  _$destroy() {
50
55
  this._reset()
51
56
 
52
- if (this._connected) {
57
+ if (this.connected) {
53
58
  this._connection.sendMsg(this._topic, C.ACTIONS.UNLISTEN, [this._pattern])
54
59
  }
55
60
  }
56
61
 
57
62
  _$onMessage(message) {
58
- if (!this._connected) {
63
+ if (!this.connected) {
59
64
  this._client._$onError(
60
65
  C.TOPIC.RECORD,
61
66
  C.EVENT.NOT_CONNECTED,
@@ -81,7 +86,7 @@ class Listener {
81
86
  }
82
87
 
83
88
  if (value$) {
84
- const subscription = value$.pipe(this._pipe).subscribe({
89
+ const subscription = value$.pipe(PIPE).subscribe({
85
90
  next: (data) => {
86
91
  if (data == null) {
87
92
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN_REJECT, [this._pattern, name])
@@ -120,8 +125,6 @@ class Listener {
120
125
  }
121
126
 
122
127
  _$onConnectionStateChange(connected) {
123
- this._connected = connected
124
-
125
128
  if (connected) {
126
129
  this._connection.sendMsg(this._topic, C.ACTIONS.LISTEN, [this._pattern, 'U'])
127
130
  } else {