@nxtedition/deepstream.io-client-js 23.4.40 → 23.4.42

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.40",
3
+ "version": "23.4.42",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -164,6 +164,15 @@ class RecordHandler {
164
164
  return new Promise((resolve) => {
165
165
  let counter = 0
166
166
 
167
+ const doSync = () => {
168
+ const token = xuid()
169
+ this._syncEmitter.once(token, resolve)
170
+
171
+ if (this._connected) {
172
+ this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
173
+ }
174
+ }
175
+
167
176
  const onUpdate = (rec) => {
168
177
  if (rec.pending) {
169
178
  return
@@ -177,18 +186,17 @@ class RecordHandler {
177
186
  return
178
187
  }
179
188
 
180
- const token = xuid()
181
- this._syncEmitter.once(token, resolve)
182
-
183
- if (this._connected) {
184
- this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
185
- }
189
+ doSync()
186
190
  }
187
191
 
188
- for (const rec of this._pending) {
189
- rec.ref()
190
- rec.subscribe(onUpdate)
191
- counter += 1
192
+ if (this._pending.size > 0) {
193
+ for (const rec of this._pending) {
194
+ rec.ref()
195
+ rec.subscribe(onUpdate)
196
+ counter += 1
197
+ }
198
+ } else {
199
+ doSync()
192
200
  }
193
201
  })
194
202
  }
@@ -22,7 +22,8 @@ class Record {
22
22
  this._subscriptionsEmitting = false
23
23
  this._updating = null
24
24
  this._patches = null
25
- this._pending = false
25
+
26
+ this._handler._onPending(this)
26
27
 
27
28
  this._subscribe()
28
29
  }
@@ -48,7 +49,7 @@ class Record {
48
49
  }
49
50
 
50
51
  get pending() {
51
- return this._pending
52
+ return this._state < Record.STATE.SERVER
52
53
  }
53
54
 
54
55
  ref() {
@@ -231,8 +232,6 @@ class Record {
231
232
  }
232
233
 
233
234
  _$onConnectionStateChange() {
234
- const prevState = this._state
235
-
236
235
  const connection = this._handler._connection
237
236
  if (connection.connected) {
238
237
  if (this._refs > 0) {
@@ -246,11 +245,21 @@ class Record {
246
245
  }
247
246
  } else {
248
247
  this._subscribed = false
249
- this._state = Record.STATE.CLIENT
248
+ if (this._state > Record.STATE.CLIENT) {
249
+ this._state = Record.STATE.CLIENT
250
+ this._handler._onPending(this)
251
+ this._emitUpdate()
252
+ }
250
253
  }
254
+ }
251
255
 
252
- if (this._state !== prevState) {
253
- this._emitUpdate()
256
+ _subscribe() {
257
+ invariant(this._refs, this._name + ' missing refs')
258
+
259
+ const connection = this._handler._connection
260
+ if (!this._subscribed && connection.connected) {
261
+ connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, this._name)
262
+ this._subscribed = true
254
263
  }
255
264
  }
256
265
 
@@ -258,35 +267,17 @@ class Record {
258
267
  invariant(!this._refs, this._name + ' must not have refs')
259
268
  invariant(!this._patches, this._name + ' must not have patches')
260
269
 
261
- const prevState = this._state
262
-
263
270
  const connection = this._handler._connection
264
271
  if (this._subscribed && connection.connected) {
265
272
  connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.UNSUBSCRIBE, this._name)
266
273
  }
267
274
 
268
275
  this._subscribed = false
269
- this._state = Record.STATE.CLIENT
270
-
271
- if (this._state !== prevState) {
272
- this._emitUpdate()
273
- }
274
- }
275
-
276
- _subscribe() {
277
- invariant(this._refs, this._name + ' missing refs')
278
-
279
- if (!this._pending) {
280
- this._pending = true
276
+ if (this._state > Record.STATE.CLIENT) {
277
+ this._state = Record.STATE.CLIENT
281
278
  this._handler._onPending(this)
282
279
  this._emitUpdate()
283
280
  }
284
-
285
- const connection = this._handler._connection
286
- if (!this._subscribed && connection.connected) {
287
- connection.sendMsg1(C.TOPIC.RECORD, C.ACTIONS.SUBSCRIBE, this._name)
288
- this._subscribed = true
289
- }
290
281
  }
291
282
 
292
283
  _update(nextData) {
@@ -318,46 +309,36 @@ class Record {
318
309
  _onUpdate([, version, data]) {
319
310
  const prevData = this._data
320
311
  const prevVersion = this._version
321
- const prevState = this._state
322
- const prevPending = this._pending
323
312
 
324
313
  if (this._updating?.delete(version)) {
325
314
  this._handler._stats.updating -= 1
326
315
  }
327
316
 
328
- if (this._patches) {
329
- this._version = version
330
- this._data = jsonPath.parse(data)
331
-
332
- if (this._version.charAt(0) !== 'I') {
333
- let patchData = this._data
334
- for (let n = 0; n < this._patches.length; n += 2) {
335
- patchData = jsonPath.set(patchData, this._patches[n + 0], this._patches[n + 1], false)
336
- }
337
- this._update(patchData)
338
- }
339
-
340
- this._patches = null
341
- } else if (version.charAt(0) === 'I' || utils.compareRev(version, this._version) > 0) {
342
- this._version = version
317
+ if (
318
+ (version.charAt(0) === 'I' && this._version !== version) ||
319
+ utils.compareRev(version, this._version) > 0
320
+ ) {
343
321
  this._data = jsonPath.set(this._data, null, jsonPath.parse(data), true)
322
+ this._version = version
344
323
  }
345
324
 
346
- if (this._pending) {
347
- this._pending = false
348
- this._handler._onPending(this)
325
+ invariant(this._version, 'must have version')
326
+ invariant(this._data, 'must have data')
327
+
328
+ if (this._patches && this._version.charAt(0) !== 'I') {
329
+ let patchData = this._data
330
+ for (let n = 0; n < this._patches.length; n += 2) {
331
+ patchData = jsonPath.set(patchData, this._patches[n + 0], this._patches[n + 1], false)
332
+ }
333
+ this._update(patchData)
349
334
  }
335
+ this._patches = null
350
336
 
351
337
  if (this._state < Record.STATE.SERVER) {
352
338
  this._state = this._version.charAt(0) === 'I' ? Record.STATE.STALE : Record.STATE.SERVER
353
- }
354
-
355
- if (
356
- this._data !== prevData ||
357
- this._version !== prevVersion ||
358
- this._state !== prevState ||
359
- this._pending !== prevPending
360
- ) {
339
+ this._handler._onPending(this)
340
+ this._emitUpdate()
341
+ } else if (this._data !== prevData || this._version !== prevVersion) {
361
342
  this._emitUpdate()
362
343
  }
363
344
  }