@nxtedition/deepstream.io-client-js 23.4.52 → 23.4.54

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.52",
3
+ "version": "23.4.54",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -21,7 +21,6 @@ const Connection = function (client, url, options) {
21
21
  this._tooManyAuthAttempts = false
22
22
  this._connectionAuthenticationTimeout = false
23
23
  this._challengeDenied = false
24
- this._sendQueue = new FixedQueue()
25
24
  this._message = {
26
25
  raw: null,
27
26
  topic: null,
@@ -35,8 +34,6 @@ const Connection = function (client, url, options) {
35
34
 
36
35
  this._processingRecv = false
37
36
  this._recvMessages = this._recvMessages.bind(this)
38
- this._processingSend = false
39
- this._sendMessages = this._sendMessages.bind(this)
40
37
 
41
38
  this._url = new URL(url)
42
39
 
@@ -82,22 +79,18 @@ Connection.prototype.authenticate = function (authParams, callback) {
82
79
  }
83
80
 
84
81
  Connection.prototype.sendMsg = function (topic, action, data) {
85
- this.send(messageBuilder.getMsg(topic, action, data))
82
+ return this.send(messageBuilder.getMsg(topic, action, data))
86
83
  }
87
84
 
88
85
  Connection.prototype.sendMsg1 = function (topic, action, p0) {
89
- this.send(messageBuilder.getMsg1(topic, action, p0))
86
+ return this.send(messageBuilder.getMsg1(topic, action, p0))
90
87
  }
91
88
 
92
89
  Connection.prototype.sendMsg2 = function (topic, action, p0, p1) {
93
- this.send(messageBuilder.getMsg2(topic, action, p0, p1))
90
+ return this.send(messageBuilder.getMsg2(topic, action, p0, p1))
94
91
  }
95
92
 
96
93
  Connection.prototype.close = function () {
97
- while (!this._sendQueue.isEmpty()) {
98
- this._submit(this._sendQueue.shift())
99
- }
100
- this._reset()
101
94
  this._deliberateClose = true
102
95
  this._endpoint?.close()
103
96
 
@@ -133,48 +126,10 @@ Connection.prototype.send = function (message) {
133
126
  err,
134
127
  message.split(C.MESSAGE_PART_SEPERATOR).map((x) => x.slice(0, 256))
135
128
  )
136
- return
137
- }
138
-
139
- if (this._processingSend) {
140
- this._sendQueue.push(message)
141
- } else if (
142
- this._state === C.CONNECTION_STATE.OPEN &&
143
- this._endpoint.readyState === this._endpoint.OPEN
144
- ) {
145
- this._submit(message)
146
- } else {
147
- this._sendQueue.push(message)
148
- this._processingSend = true
149
- this._sendMessages()
150
- }
151
- }
152
-
153
- Connection.prototype._sendMessages = function (deadline) {
154
- if (
155
- this._state !== C.CONNECTION_STATE.OPEN ||
156
- this._endpoint.readyState !== this._endpoint.OPEN
157
- ) {
158
- this._processingSend = false
159
- return
160
- }
161
-
162
- for (
163
- let n = 0;
164
- // eslint-disable-next-line no-unmodified-loop-condition
165
- n < this._batchSize && (!deadline || deadline.timeRemaining() || deadline.didTimeout);
166
- ++n
167
- ) {
168
- const message = this._sendQueue.shift()
169
- if (!message) {
170
- this._processingSend = false
171
- return
172
- }
173
-
174
- this._submit(message)
129
+ return false
175
130
  }
176
131
 
177
- this._schedule(this._sendMessages)
132
+ return this._submit(message)
178
133
  }
179
134
 
180
135
  Connection.prototype._submit = function (message) {
@@ -183,12 +138,18 @@ Connection.prototype._submit = function (message) {
183
138
  if (message.length > maxPacketSize) {
184
139
  const err = new Error(`Packet to big: ${message.length} > ${maxPacketSize}`)
185
140
  this._client._$onError(C.TOPIC.CONNECTION, C.EVENT.CONNECTION_ERROR, err)
186
- } else if (this._endpoint.readyState === this._endpoint.OPEN) {
141
+ return false
142
+ } else if (
143
+ this._state === C.CONNECTION_STATE.OPEN &&
144
+ this._endpoint.readyState === this._endpoint.OPEN
145
+ ) {
187
146
  this.emit('send', message)
188
147
  this._endpoint.send(message)
148
+ return true
189
149
  } else {
190
150
  const err = new Error('Tried to send message on a closed websocket connection')
191
151
  this._client._$onError(C.TOPIC.CONNECTION, C.EVENT.CONNECTION_ERROR, err)
152
+ return false
192
153
  }
193
154
  }
194
155
 
@@ -210,7 +171,7 @@ Connection.prototype._onOpen = function () {
210
171
  }
211
172
 
212
173
  Connection.prototype._onError = function (err) {
213
- this._reset()
174
+ this._recvMessages()
214
175
 
215
176
  if (err.error) {
216
177
  const { message, error } = err
@@ -232,7 +193,7 @@ Connection.prototype._onError = function (err) {
232
193
  }
233
194
 
234
195
  Connection.prototype._onClose = function () {
235
- this._reset()
196
+ this._recvMessages()
236
197
 
237
198
  if (this._deliberateClose === true) {
238
199
  this._setState(C.CONNECTION_STATE.CLOSED)
@@ -291,11 +252,6 @@ Connection.prototype._recvMessages = function (deadline) {
291
252
  this._schedule(this._recvMessages)
292
253
  }
293
254
 
294
- Connection.prototype._reset = function () {
295
- this._recvQueue = new FixedQueue()
296
- this._sendQueue = new FixedQueue()
297
- }
298
-
299
255
  Connection.prototype._handleConnectionResponse = function (message) {
300
256
  if (message.action === C.ACTIONS.PING) {
301
257
  this._submit(messageBuilder.getMsg(C.TOPIC.CONNECTION, C.ACTIONS.PONG))
@@ -363,7 +319,6 @@ Connection.prototype._setState = function (state) {
363
319
  this._client.emit(C.EVENT.CONNECTED, true)
364
320
  } else if (state === C.CONNECTION_STATE.RECONNECTING || state === C.CONNECTION_STATE.CLOSED) {
365
321
  this._client.emit(C.EVENT.CONNECTED, false)
366
- this._reset()
367
322
  }
368
323
  }
369
324
 
@@ -162,10 +162,7 @@ class RecordHandler {
162
162
 
163
163
  const token = xuid()
164
164
  this._syncEmitter.once(token, resolve)
165
-
166
- if (this._connected) {
167
- this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
168
- }
165
+ this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
169
166
  }
170
167
 
171
168
  for (const rec of this._pending) {
@@ -16,12 +16,15 @@ class Record {
16
16
  this._version = ''
17
17
  this._data = jsonPath.EMPTY
18
18
  this._state = Record.STATE.VOID
19
- this._refs = 0
19
+ this._refs = 1
20
20
  this._subscriptions = []
21
- this._subscriptionsEmitting = false
21
+ this._emitting = false
22
22
  this._updating = null
23
- this._patches = null
24
- this._subscribed = false
23
+ this._patching = null
24
+ this._pending = false
25
+ this._subscribed = this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
26
+
27
+ this._onPending(true)
25
28
  }
26
29
 
27
30
  get name() {
@@ -50,8 +53,8 @@ class Record {
50
53
  this._handler._onPruning(this, false)
51
54
  }
52
55
 
53
- if (this._refs === 1 && this._handler._connection.connected && !this._subscribed) {
54
- this._subscribe()
56
+ if (!this._subscribed) {
57
+ this._subscribed = this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
55
58
  }
56
59
 
57
60
  return this
@@ -59,7 +62,7 @@ class Record {
59
62
 
60
63
  unref() {
61
64
  invariant(this._refs > 0, 'missing refs')
62
- invariant(this._refs > 0 || !this._patches, 'must not have patches')
65
+ invariant(this._refs > 0 || !this._patching, 'must not have patches')
63
66
  invariant(this._refs > 0 || this._state >= Record.STATE.SERVER, 'must be ready')
64
67
 
65
68
  this._refs -= 1
@@ -71,9 +74,9 @@ class Record {
71
74
  }
72
75
 
73
76
  subscribe(fn) {
74
- if (this._subscriptionsEmitting) {
77
+ if (this._emitting) {
75
78
  this._subscriptions = this._subscriptions.slice()
76
- this._subscriptionsEmitting = false
79
+ this._emitting = false
77
80
  }
78
81
 
79
82
  this._subscriptions.push(fn)
@@ -82,9 +85,9 @@ class Record {
82
85
  }
83
86
 
84
87
  unsubscribe(fn) {
85
- if (this._subscriptionsEmitting) {
88
+ if (this._emitting) {
86
89
  this._subscriptions = this._subscriptions.slice()
87
- this._subscriptionsEmitting = false
90
+ this._emitting = false
88
91
  }
89
92
 
90
93
  const idx = this._subscriptions.indexOf(fn)
@@ -128,14 +131,14 @@ class Record {
128
131
  }
129
132
 
130
133
  if (this._state < Record.STATE.SERVER) {
131
- if (!this._patches) {
134
+ if (!this._patching) {
132
135
  this.ref()
133
- this._patches = []
136
+ this._patching = []
134
137
  } else if (path) {
135
- this._patches.splice(0)
138
+ this._patching.splice(0)
136
139
  }
137
140
 
138
- this._patches.push(path, cloneDeep(data))
141
+ this._patching.push(path, cloneDeep(data))
139
142
  } else {
140
143
  this._update(jsonPath.set(this._data, path, data, false))
141
144
  }
@@ -228,49 +231,37 @@ class Record {
228
231
  return true
229
232
  }
230
233
 
231
- _$onConnectionStateChange() {
232
- if (this._handler._connection.connected) {
233
- if (this._refs > 0) {
234
- this._subscribe()
235
- }
234
+ _$onConnectionStateChange(connected) {
235
+ if (connected) {
236
+ this._subscribed = this._refs > 0 && this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
236
237
 
237
238
  if (this._updating) {
238
239
  for (const update of this._updating.values()) {
239
- this._handler._connection.connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
240
+ this._sendMsg1(C.ACTIONS.UPDATE, update)
240
241
  }
241
242
  }
242
243
  } else {
243
244
  this._subscribed = false
244
245
  }
245
246
 
247
+ if (!this._pending) {
248
+ this._onPending(true)
249
+ }
250
+
246
251
  if (this._state > Record.STATE.CLIENT) {
247
252
  this._state = Record.STATE.CLIENT
248
253
  this._emitUpdate()
249
254
  }
250
255
  }
251
256
 
252
- _subscribe() {
253
- invariant(this._handler._connection.connected, 'must be connected')
254
-
255
- this._handler._connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, this._name)
256
- this._subscribed = true
257
-
258
- this.ref()
259
- this._handler._onPending(this, true)
260
- }
261
-
262
257
  _$dispose() {
263
258
  invariant(!this._refs, 'must not have refs')
264
- invariant(!this._patches, 'must not have patches')
259
+ invariant(!this._patching, 'must not have patches')
265
260
  invariant(!this._updating, 'must not have updates')
266
- invariant(this.state >= C.RECORD_STATE.SERVER, 'must not be pending')
267
- invariant(
268
- !this._subscribed || this._handler._connection.connected,
269
- 'must be unsubscribed or connected'
270
- )
261
+ invariant(!this._pending, 'must not be pending')
271
262
 
272
263
  if (this._subscribed) {
273
- this._handler._connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.UNSUBSCRIBE, this._name)
264
+ this._sendMsg1(C.ACTIONS.UNSUBSCRIBE, this._name)
274
265
  this._subscribed = false
275
266
  }
276
267
 
@@ -283,6 +274,8 @@ class Record {
283
274
  _update(nextData) {
284
275
  invariant(this._version, 'must have version')
285
276
 
277
+ const connection = this._handler._connection
278
+
286
279
  if (nextData === this._data) {
287
280
  return
288
281
  }
@@ -300,10 +293,7 @@ class Record {
300
293
  this._updating.set(nextVersion, update)
301
294
  this._handler._stats.updating += 1
302
295
 
303
- const connection = this._handler._connection
304
- if (connection.connected) {
305
- connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
306
- }
296
+ connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
307
297
 
308
298
  this._data = nextData
309
299
  this._version = nextVersion
@@ -334,23 +324,25 @@ class Record {
334
324
  invariant(this._version, 'must have version')
335
325
  invariant(this._data, 'must have data')
336
326
 
337
- if (this._patches) {
327
+ if (this._patching) {
338
328
  if (this._version.charAt(0) !== 'I') {
339
329
  let patchData = this._data
340
- for (let n = 0; n < this._patches.length; n += 2) {
341
- patchData = jsonPath.set(patchData, this._patches[n + 0], this._patches[n + 1], false)
330
+ for (let n = 0; n < this._patching.length; n += 2) {
331
+ patchData = jsonPath.set(patchData, this._patching[n + 0], this._patching[n + 1], false)
342
332
  }
343
333
  this._update(patchData)
344
334
  }
345
335
 
346
- this._patches = null
336
+ this._patching = null
347
337
  this.unref()
348
338
  }
349
339
 
350
340
  if (this._state < C.RECORD_STATE.SERVER) {
351
341
  this._state = this._version.charAt(0) === 'I' ? C.RECORD_STATE.STALE : C.RECORD_STATE.SERVER
352
- this._handler._onPending(this, false)
353
- this.unref()
342
+ }
343
+
344
+ if (this._pending) {
345
+ this._onPending(false)
354
346
  }
355
347
 
356
348
  if (this._state !== prevState || this._data !== prevData || this._version !== prevVersion) {
@@ -358,13 +350,30 @@ class Record {
358
350
  }
359
351
  }
360
352
 
353
+ _onPending(value) {
354
+ if (this._pending === value) {
355
+ return
356
+ }
357
+
358
+ this._pending = value
359
+ this._handler._onPending(this, value)
360
+
361
+ if (value) {
362
+ this.ref()
363
+ } else {
364
+ this.unref()
365
+ }
366
+ }
367
+
361
368
  _onSubscriptionHasProvider([, hasProvider]) {
369
+ if (this._state < C.RECORD_STATE.SERVER) {
370
+ return
371
+ }
372
+
362
373
  const prevState = this._state
363
374
 
364
375
  this._state =
365
- hasProvider &&
366
- messageParser.convertTyped(hasProvider, this._handler._client) &&
367
- this._state >= C.RECORD_STATE.SERVER
376
+ hasProvider && messageParser.convertTyped(hasProvider, this._handler._client)
368
377
  ? C.RECORD_STATE.PROVIDER
369
378
  : this._version.charAt(0) === 'I'
370
379
  ? C.RECORD_STATE.STALE
@@ -375,6 +384,10 @@ class Record {
375
384
  }
376
385
  }
377
386
 
387
+ _sendMsg1(action, data) {
388
+ return this._handler._connection.sendMsg1(C.TOPIC.RECORD, action, data)
389
+ }
390
+
378
391
  _error(event, msgOrError, data) {
379
392
  this._handler._client._$onError(C.TOPIC.RECORD, event, msgOrError, [
380
393
  ...(Array.isArray(data) ? data : []),
@@ -394,7 +407,7 @@ class Record {
394
407
  }
395
408
 
396
409
  _emitUpdate() {
397
- this._subscriptionsEmitting = true
410
+ this._emitting = true
398
411
  try {
399
412
  for (const fn of this._subscriptions) {
400
413
  try {
@@ -407,7 +420,7 @@ class Record {
407
420
  }
408
421
  }
409
422
  } finally {
410
- this._subscriptionsEmitting = false
423
+ this._emitting = false
411
424
  }
412
425
  }
413
426
  }