@nxtedition/deepstream.io-client-js 23.4.56 → 23.4.58

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.56",
3
+ "version": "23.4.58",
4
4
  "description": "the javascript client for deepstream.io",
5
5
  "homepage": "http://deepstream.io",
6
6
  "bugs": {
@@ -302,8 +302,6 @@ Connection.prototype._handleAuthResponse = function (message) {
302
302
  if (this._authCallback) {
303
303
  this._authCallback(true, this._getAuthData(message.data[0]))
304
304
  }
305
-
306
- this._sendMessages()
307
305
  }
308
306
  }
309
307
 
@@ -24,7 +24,7 @@ class RecordHandler {
24
24
  this._client = client
25
25
  this._records = new Map()
26
26
  this._listeners = new Map()
27
- this._pending = new Set()
27
+ this._pending = new Map()
28
28
  this._pruning = new Set()
29
29
 
30
30
  this._connected = 0
@@ -32,10 +32,14 @@ class RecordHandler {
32
32
  updating: 0,
33
33
  created: 0,
34
34
  destroyed: 0,
35
+ records: 0,
36
+ pending: 0,
37
+ pruning: 0,
38
+ patching: 0,
35
39
  }
36
40
 
37
41
  this._syncEmitter = new EventEmitter()
38
- this._pendingEmitter = new EventEmitter()
42
+ this._readyEmitter = new EventEmitter()
39
43
 
40
44
  this.set = this.set.bind(this)
41
45
  this.get = this.get.bind(this)
@@ -56,8 +60,10 @@ class RecordHandler {
56
60
 
57
61
  for (const rec of pruning) {
58
62
  rec._$dispose()
63
+
64
+ this._stats.records -= 1
65
+ this._stats.destroyed += 1
59
66
  this._records.delete(rec.name)
60
- this._stats.destroyed++
61
67
  }
62
68
 
63
69
  if (this._pruningTimeout) {
@@ -71,6 +77,12 @@ class RecordHandler {
71
77
  }
72
78
 
73
79
  _onPruning(rec, isPruning) {
80
+ if (isPruning) {
81
+ this._stats.pruning += 1
82
+ } else {
83
+ this._stats.pruning -= 1
84
+ }
85
+
74
86
  if (isPruning) {
75
87
  this._pruning.add(rec)
76
88
  } else {
@@ -78,13 +90,37 @@ class RecordHandler {
78
90
  }
79
91
  }
80
92
 
81
- _onPending(rec, isPending) {
82
- if (isPending) {
83
- this._pending.add(rec)
93
+ _onPending(rec, value) {
94
+ if (value) {
95
+ this._stats.pending += 1
84
96
  } else {
97
+ this._stats.pending -= 1
98
+ }
99
+
100
+ if (value) {
101
+ this._pending.set(rec, [])
102
+ } else {
103
+ for (const callback of this._pending.get(rec)) {
104
+ callback()
105
+ }
85
106
  this._pending.delete(rec)
86
107
  }
87
- this._pendingEmitter.emit(rec.name, isPending)
108
+ }
109
+
110
+ _onUpdating(rec, value) {
111
+ if (value) {
112
+ this._stats.updating += 1
113
+ } else {
114
+ this._stats.updating -= 1
115
+ }
116
+ }
117
+
118
+ _onPatching(rec, value) {
119
+ if (value) {
120
+ this._stats.patching += 1
121
+ } else {
122
+ this._stats.patching -= 1
123
+ }
88
124
  }
89
125
 
90
126
  get connected() {
@@ -92,32 +128,25 @@ class RecordHandler {
92
128
  }
93
129
 
94
130
  get stats() {
95
- return {
96
- ...this._stats,
97
- listeners: this._listeners.size,
98
- records: this._records.size,
99
- pruning: this._pruning.size,
100
- pending: this._pending.size,
101
- }
131
+ return this._stats
102
132
  }
103
133
 
104
134
  getRecord(name) {
105
135
  invariant(
106
- typeof name === 'string' && name.length > 0 && !name.includes('[object Object]'),
136
+ typeof name === 'string' && name.length > 0 && name !== '[object Object]',
107
137
  `invalid name ${name}`
108
138
  )
109
139
 
110
140
  let record = this._records.get(name)
111
141
 
112
142
  if (!record) {
113
- record = new Record(name, this).ref()
143
+ record = new Record(name, this)
144
+ this._stats.records += 1
145
+ this._stats.created += 1
114
146
  this._records.set(name, record)
115
- this._stats.created++
116
- } else {
117
- record.ref()
118
147
  }
119
148
 
120
- return record
149
+ return record.ref()
121
150
  }
122
151
 
123
152
  provide(pattern, callback, options) {
@@ -144,18 +173,24 @@ class RecordHandler {
144
173
  ? new UnicastListener(C.TOPIC.RECORD, pattern, callback, this, options)
145
174
  : new MulticastListener(C.TOPIC.RECORD, pattern, callback, this, options)
146
175
 
176
+ this._stats.listeners += 1
147
177
  this._listeners.set(pattern, listener)
178
+
148
179
  return () => {
149
180
  listener._$destroy()
181
+
182
+ this._stats.listeners -= 1
150
183
  this._listeners.delete(pattern)
151
184
  }
152
185
  }
153
186
 
154
187
  sync() {
188
+ // TODO (fix): Sync patching & updating?
155
189
  return new Promise((resolve) => {
156
- let counter = this._pending.size
190
+ let counter = this._pending.size + 1
157
191
 
158
192
  const maybeSync = () => {
193
+ counter -= 1
159
194
  if (counter > 0) {
160
195
  return
161
196
  }
@@ -165,12 +200,8 @@ class RecordHandler {
165
200
  this._connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.SYNC, [token])
166
201
  }
167
202
 
168
- for (const rec of this._pending) {
169
- this._pendingEmitter.once(rec.name, (isPending) => {
170
- invariant(!isPending, 'unexpected pending state')
171
- counter -= 1
172
- maybeSync()
173
- })
203
+ for (const callbacks of this._pending.values()) {
204
+ callbacks.push(maybeSync)
174
205
  }
175
206
 
176
207
  maybeSync()
@@ -15,8 +15,8 @@ class Record {
15
15
  this._name = name
16
16
  this._version = ''
17
17
  this._data = jsonPath.EMPTY
18
- this._state = Record.STATE.VOID
19
- this._refs = 1
18
+ this._state = C.RECORD_STATE.VOID
19
+ this._refs = 0
20
20
  this._subscriptions = []
21
21
  this._emitting = false
22
22
  this._updating = null
@@ -51,10 +51,7 @@ class Record {
51
51
  this._refs += 1
52
52
  if (this._refs === 1) {
53
53
  this._handler._onPruning(this, false)
54
- }
55
-
56
- if (!this._subscribed) {
57
- this._subscribed = this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
54
+ this._subscribed = this._subscribed || this._sendMsg1(C.ACTIONS.SUBSCRIBE, this._name)
58
55
  }
59
56
 
60
57
  return this
@@ -63,7 +60,7 @@ class Record {
63
60
  unref() {
64
61
  invariant(this._refs > 0, 'missing refs')
65
62
  invariant(this._refs > 0 || !this._patching, 'must not have patches')
66
- invariant(this._refs > 0 || this._state >= Record.STATE.SERVER, 'must be ready')
63
+ invariant(this._refs > 0 || this._state >= C.RECORD_STATE.SERVER, 'must be ready')
67
64
 
68
65
  this._refs -= 1
69
66
  if (this._refs === 0) {
@@ -130,10 +127,9 @@ class Record {
130
127
  throw new Error('invalid argument: path')
131
128
  }
132
129
 
133
- if (this._state < Record.STATE.SERVER) {
134
- if (!this._patching) {
135
- this.ref()
136
- this._patching = []
130
+ if (this._state < C.RECORD_STATE.SERVER) {
131
+ if (this._patching == null) {
132
+ this._onPatching(true)
137
133
  } else if (path) {
138
134
  this._patching.splice(0)
139
135
  }
@@ -152,7 +148,7 @@ class Record {
152
148
  when(stateOrNull) {
153
149
  invariant(this._refs > 0, 'missing refs')
154
150
 
155
- const state = stateOrNull == null ? Record.STATE.SERVER : stateOrNull
151
+ const state = stateOrNull == null ? C.RECORD_STATE.SERVER : stateOrNull
156
152
 
157
153
  if (!Number.isFinite(state) || state < 0) {
158
154
  throw new Error('invalid argument: state')
@@ -208,7 +204,7 @@ class Record {
208
204
  }
209
205
 
210
206
  this.ref()
211
- return this.when(Record.STATE.SERVER)
207
+ return this.when(C.RECORD_STATE.SERVER)
212
208
  .then(() => {
213
209
  const prev = this.get(path)
214
210
  const next = updater(prev, this._version)
@@ -248,8 +244,8 @@ class Record {
248
244
  this._onPending(true)
249
245
  }
250
246
 
251
- if (this._state > Record.STATE.CLIENT) {
252
- this._state = Record.STATE.CLIENT
247
+ if (this._state > C.RECORD_STATE.CLIENT) {
248
+ this._state = C.RECORD_STATE.CLIENT
253
249
  this._emitUpdate()
254
250
  }
255
251
  }
@@ -286,12 +282,10 @@ class Record {
286
282
  const update = [this._name, nextVersion, jsonPath.stringify(nextData), prevVersion]
287
283
 
288
284
  if (!this._updating) {
289
- this._updating = new Map()
290
- this.ref()
285
+ this._onUpdating(true)
291
286
  }
292
287
 
293
288
  this._updating.set(nextVersion, update)
294
- this._handler._stats.updating += 1
295
289
 
296
290
  connection.sendMsg(C.TOPIC.RECORD, C.ACTIONS.UPDATE, update)
297
291
 
@@ -304,12 +298,8 @@ class Record {
304
298
  const prevVersion = this._version
305
299
  const prevState = this._state
306
300
 
307
- if (this._updating?.delete(version)) {
308
- this._handler._stats.updating -= 1
309
- if (this._updating.size === 0) {
310
- this._updating = null
311
- this.unref()
312
- }
301
+ if (this._updating && this._updating.delete(version) && this._updating.size === 0) {
302
+ this._onUpdating(false)
313
303
  }
314
304
 
315
305
  if (
@@ -324,7 +314,7 @@ class Record {
324
314
  invariant(this._version, 'must have version')
325
315
  invariant(this._data, 'must have data')
326
316
 
327
- if (this._patching) {
317
+ if (this._patching != null) {
328
318
  if (this._version.charAt(0) !== 'I') {
329
319
  let patchData = this._data
330
320
  for (let n = 0; n < this._patching.length; n += 2) {
@@ -333,8 +323,7 @@ class Record {
333
323
  this._update(patchData)
334
324
  }
335
325
 
336
- this._patching = null
337
- this.unref()
326
+ this._onPatching(false)
338
327
  }
339
328
 
340
329
  if (this._state < C.RECORD_STATE.SERVER) {
@@ -350,19 +339,44 @@ class Record {
350
339
  }
351
340
  }
352
341
 
353
- _onPending(value) {
354
- if (this._pending === value) {
355
- return
342
+ _onPatching(value) {
343
+ invariant(this._refs > 0, 'missing refs')
344
+
345
+ if (value) {
346
+ this._patching = []
347
+ this._refs += 1
348
+ } else {
349
+ this._patching = null
350
+ this._refs -= 1
356
351
  }
357
352
 
358
- this._pending = value
359
353
  this._handler._onPending(this, value)
354
+ }
355
+
356
+ _onUpdating(value) {
357
+ invariant(this._refs > 0, 'missing refs')
360
358
 
361
359
  if (value) {
362
- this.ref()
360
+ this._updating = new Map()
361
+ this._refs += 1
363
362
  } else {
364
- this.unref()
363
+ this._updating = null
364
+ this._refs -= 1
365
365
  }
366
+
367
+ this._handler._onUpdating(this, value)
368
+ }
369
+
370
+ _onPending(value) {
371
+ if (value) {
372
+ this._pending = true
373
+ this._refs += 1
374
+ } else {
375
+ this._pending = false
376
+ this._refs -= 1
377
+ }
378
+
379
+ this._handler._onPending(this, value)
366
380
  }
367
381
 
368
382
  _onSubscriptionHasProvider([, hasProvider]) {
@@ -448,7 +462,7 @@ Object.defineProperty(Record.prototype, 'empty', {
448
462
  // TODO (fix): Remove
449
463
  Object.defineProperty(Record.prototype, 'ready', {
450
464
  get: function ready() {
451
- return this._state >= Record.STATE.SERVER
465
+ return this._state >= C.RECORD_STATE.SERVER
452
466
  },
453
467
  })
454
468
 
@@ -476,7 +490,7 @@ Object.defineProperty(Record.prototype, 'stale', {
476
490
  // TODO (fix): Remove
477
491
  Object.defineProperty(Record.prototype, 'isReady', {
478
492
  get: function isReady() {
479
- return this._state >= Record.STATE.SERVER
493
+ return this._state >= C.RECORD_STATE.SERVER
480
494
  },
481
495
  })
482
496