@nxtedition/deepstream.io-client-js 23.1.20 → 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 +1 -1
- package/src/record/json-path.js +11 -0
- package/src/record/record.js +21 -37
package/package.json
CHANGED
package/src/record/json-path.js
CHANGED
|
@@ -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,
|
package/src/record/record.js
CHANGED
|
@@ -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,25 +98,15 @@ class Record {
|
|
|
100
98
|
throw new Error('invalid argument: path')
|
|
101
99
|
}
|
|
102
100
|
|
|
103
|
-
|
|
104
|
-
const nextData = jsonPath.set(this._data, path, data, false)
|
|
105
|
-
|
|
106
|
-
if (prevData === nextData) {
|
|
101
|
+
if (!this._update(path, data, false)) {
|
|
107
102
|
return
|
|
108
103
|
}
|
|
109
104
|
|
|
110
|
-
|
|
111
|
-
this._version = this._makeVersion(this._version ? parseInt(this._version) + 1 : 1)
|
|
112
|
-
this._data = nextData
|
|
105
|
+
this._emitUpdate()
|
|
113
106
|
|
|
114
|
-
|
|
115
|
-
this._patches.push(path, jsonPath.jsonClone(data))
|
|
107
|
+
if (this._state < Record.STATE.SERVER) {
|
|
116
108
|
this._handler._patch.add(this)
|
|
117
|
-
} else {
|
|
118
|
-
this._sendUpdate(nextData)
|
|
119
109
|
}
|
|
120
|
-
|
|
121
|
-
this._emitUpdate()
|
|
122
110
|
}
|
|
123
111
|
|
|
124
112
|
when(stateOrNull) {
|
|
@@ -235,7 +223,7 @@ class Record {
|
|
|
235
223
|
|
|
236
224
|
_unsubscribe() {
|
|
237
225
|
invariant(!this._refs, this._name + ' must not have refs')
|
|
238
|
-
invariant(
|
|
226
|
+
invariant(this._version, this._name + ' must have version')
|
|
239
227
|
|
|
240
228
|
const prevState = this._state
|
|
241
229
|
|
|
@@ -262,25 +250,29 @@ class Record {
|
|
|
262
250
|
}
|
|
263
251
|
}
|
|
264
252
|
|
|
265
|
-
|
|
266
|
-
invariant(this._version, this._name + ' missing version')
|
|
267
|
-
invariant(this._data, this._name + ' missing data')
|
|
268
|
-
|
|
269
|
-
const connection = this._handler._connection
|
|
270
|
-
|
|
253
|
+
_update(path, data, isPlainObject) {
|
|
271
254
|
const prevVersion = this._version
|
|
255
|
+
const prevData = this._data
|
|
256
|
+
|
|
272
257
|
const nextVersion = this._makeVersion(parseInt(prevVersion) + 1)
|
|
258
|
+
const nextData = jsonPath.set(this._data, path, data, isPlainObject)
|
|
259
|
+
|
|
260
|
+
if (nextData === prevData) {
|
|
261
|
+
return false
|
|
262
|
+
}
|
|
273
263
|
|
|
274
|
-
const update = [this._name, nextVersion,
|
|
264
|
+
const update = [this._name, nextVersion, jsonPath.stringify(data), prevVersion]
|
|
275
265
|
|
|
276
|
-
|
|
266
|
+
this._handler._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
|
|
277
267
|
|
|
278
268
|
this._version = nextVersion
|
|
279
|
-
this._data =
|
|
269
|
+
this._data = nextData
|
|
280
270
|
|
|
281
271
|
this._updating ??= new Map()
|
|
282
272
|
this._updating.set(nextVersion, update)
|
|
283
273
|
this._handler._stats.updating += 1
|
|
274
|
+
|
|
275
|
+
return true
|
|
284
276
|
}
|
|
285
277
|
|
|
286
278
|
_onUpdate([, version, data]) {
|
|
@@ -292,28 +284,20 @@ class Record {
|
|
|
292
284
|
this._handler._stats.updating -= 1
|
|
293
285
|
}
|
|
294
286
|
|
|
295
|
-
if (this.
|
|
287
|
+
if (!this._version) {
|
|
296
288
|
this._version = version
|
|
297
|
-
this._data = jsonPath.
|
|
289
|
+
this._data = jsonPath.parse(data)
|
|
298
290
|
|
|
299
291
|
if (this._version.charAt(0) !== 'I') {
|
|
300
|
-
|
|
301
|
-
this._sendUpdate(
|
|
302
|
-
jsonPath.set(this._data, this._patches[i + 0], this._patches[i + 1], true)
|
|
303
|
-
)
|
|
304
|
-
}
|
|
305
|
-
} else if (this._patches.length) {
|
|
306
|
-
this._error(C.EVENT.USER_ERROR, 'cannot patch provided value')
|
|
292
|
+
this._update(null, prevData, true)
|
|
307
293
|
}
|
|
308
|
-
|
|
309
|
-
this._patches = null
|
|
310
|
-
this._handler._patch.delete(this)
|
|
311
294
|
} else if (version.charAt(0) === 'I' || utils.compareRev(version, this._version) > 0) {
|
|
312
295
|
this._version = version
|
|
313
296
|
this._data = jsonPath.set(this._data, null, jsonPath.parse(data), true)
|
|
314
297
|
}
|
|
315
298
|
|
|
316
299
|
if (this._state < Record.STATE.SERVER) {
|
|
300
|
+
this._handler._patch.delete(this)
|
|
317
301
|
this._state = this._version.charAt(0) === 'I' ? Record.STATE.STALE : Record.STATE.SERVER
|
|
318
302
|
}
|
|
319
303
|
|