@nxtedition/deepstream.io-client-js 23.1.19 → 23.1.21

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.1.19",
3
+ "version": "23.1.21",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -36,6 +36,16 @@ function parse(value) {
36
36
  }
37
37
  }
38
38
 
39
+ function stringify(value) {
40
+ if (value === EMPTY_OBJ) {
41
+ return '{}'
42
+ } else if (value === EMPTY_ARR) {
43
+ return '[]'
44
+ } else {
45
+ return JSON.stringify(value)
46
+ }
47
+ }
48
+
39
49
  function set(data, path, value, isPlainJSON = false) {
40
50
  data = data || EMPTY
41
51
 
@@ -174,6 +184,7 @@ module.exports = {
174
184
  EMPTY_OBJ,
175
185
  EMPTY_ARR,
176
186
  parse,
187
+ stringify,
177
188
  get,
178
189
  set,
179
190
  jsonClone,
@@ -18,9 +18,7 @@ class Record {
18
18
  this._refs = 1
19
19
  this._subscribed = false
20
20
  this._subscriptions = []
21
-
22
21
  this._updating = null
23
- this._patches = null
24
22
 
25
23
  this._subscribe()
26
24
  }
@@ -100,23 +98,14 @@ class Record {
100
98
  throw new Error('invalid argument: path')
101
99
  }
102
100
 
103
- const prevData = this._data
104
- const prevVersion = this._version
105
- const prevState = this._state
101
+ if (!this._update(path, data, false)) {
102
+ return
103
+ }
104
+
105
+ this._emitUpdate()
106
106
 
107
107
  if (this._state < Record.STATE.SERVER) {
108
- this._patches = this._patches && path ? this._patches : []
109
- this._patches.push(path, jsonPath.jsonClone(data))
110
108
  this._handler._patch.add(this)
111
-
112
- this._version = this._makeVersion(this._version ? parseInt(this._version) + 1 : 1)
113
- this._data = jsonPath.set(this._data, path, data, true)
114
- } else {
115
- this._update(path, jsonPath.jsonClone(data), false)
116
- }
117
-
118
- if (this._data !== prevData || this._version !== prevVersion || this._state !== prevState) {
119
- this._emitUpdate()
120
109
  }
121
110
  }
122
111
 
@@ -234,7 +223,7 @@ class Record {
234
223
 
235
224
  _unsubscribe() {
236
225
  invariant(!this._refs, this._name + ' must not have refs')
237
- invariant(!this._patches, this._name + ' must not have patch queue')
226
+ invariant(this._version, this._name + ' must have version')
238
227
 
239
228
  const prevState = this._state
240
229
 
@@ -261,30 +250,29 @@ class Record {
261
250
  }
262
251
  }
263
252
 
264
- _update(path, data, force) {
265
- invariant(this._version, this._name + ' missing version')
266
- invariant(this._data, this._name + ' missing data')
253
+ _update(path, data, isPlainObject) {
254
+ const prevVersion = this._version
255
+ const prevData = this._data
267
256
 
268
- const connection = this._handler._connection
257
+ const nextVersion = this._makeVersion(parseInt(prevVersion) + 1)
258
+ const nextData = jsonPath.set(this._data, path, data, isPlainObject)
269
259
 
270
- const prevData = this._data
271
- const nextData = jsonPath.set(prevData, path, data, true)
260
+ if (nextData === prevData) {
261
+ return false
262
+ }
272
263
 
273
- if (force || nextData !== prevData) {
274
- const prevVersion = this._version
275
- const nextVersion = this._makeVersion(parseInt(prevVersion) + 1)
264
+ const update = [this._name, nextVersion, jsonPath.stringify(data), prevVersion]
276
265
 
277
- const update = [this._name, nextVersion, JSON.stringify(nextData), prevVersion]
266
+ this._handler._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
278
267
 
279
- connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
268
+ this._version = nextVersion
269
+ this._data = nextData
280
270
 
281
- this._version = nextVersion
282
- this._data = nextData
271
+ this._updating ??= new Map()
272
+ this._updating.set(nextVersion, update)
273
+ this._handler._stats.updating += 1
283
274
 
284
- this._updating ??= new Map()
285
- this._updating.set(nextVersion, update)
286
- this._handler._stats.updating += 1
287
- }
275
+ return true
288
276
  }
289
277
 
290
278
  _onUpdate([, version, data]) {
@@ -296,26 +284,20 @@ class Record {
296
284
  this._handler._stats.updating -= 1
297
285
  }
298
286
 
299
- if (this._patches) {
300
- this._data = jsonPath.set(this._data, null, jsonPath.parse(data), true)
287
+ if (!this._version) {
301
288
  this._version = version
289
+ this._data = jsonPath.parse(data)
302
290
 
303
291
  if (this._version.charAt(0) !== 'I') {
304
- for (let i = 0; i < this._patches.length; i += 2) {
305
- this._update(this._patches[i + 0], this._patches[i + 1], true)
306
- }
307
- } else if (this._patches.length) {
308
- this._error(C.EVENT.USER_ERROR, 'cannot patch provided value')
292
+ this._update(null, prevData, true)
309
293
  }
310
-
311
- this._patches = null
312
- this._handler._patch.delete(this)
313
294
  } else if (version.charAt(0) === 'I' || utils.compareRev(version, this._version) > 0) {
314
- this._data = jsonPath.set(this._data, null, jsonPath.parse(data), true)
315
295
  this._version = version
296
+ this._data = jsonPath.set(this._data, null, jsonPath.parse(data), true)
316
297
  }
317
298
 
318
299
  if (this._state < Record.STATE.SERVER) {
300
+ this._handler._patch.delete(this)
319
301
  this._state = this._version.charAt(0) === 'I' ? Record.STATE.STALE : Record.STATE.SERVER
320
302
  }
321
303