statebus 7.0.17 → 7.0.19

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/client-library.js CHANGED
@@ -201,6 +201,7 @@
201
201
  }
202
202
 
203
203
  // Else, reconnect!
204
+ console.log('Reconnecting due to', e)
204
205
  setTimeout(() => subscribe(key, t),
205
206
  reconnect_attempts > 0 ? 5000 : 1500)
206
207
  subscriptions[key].status = 'reconnecting'
@@ -232,10 +233,17 @@
232
233
  }
233
234
 
234
235
  // Return the update
235
- t.return({
236
- key: key,
237
- val: add_prefixes(JSON.parse(new_version.body))
238
- })
236
+ if (new_version.body)
237
+ // As a snapshot body
238
+ t.return({
239
+ key: key,
240
+ val: add_prefixes(JSON.parse(new_version.body))
241
+ })
242
+ else if (new_version.patches)
243
+ // As a patch
244
+ bus.set.fire(key, {patch: new_version.patches.map(
245
+ patch => patch.range + ' = ' + patch.content
246
+ )})
239
247
  },
240
248
  reconnect
241
249
  )).catch(reconnect)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "7.0.17",
3
+ "version": "7.0.19",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/statebus.js CHANGED
@@ -186,6 +186,26 @@
186
186
  || executing_funk && executing_funk.latest_reaction_at
187
187
  || new_version()
188
188
 
189
+ // Handle patches. We receive them as:
190
+ //
191
+ // set.fire("foo", {patch: ['.foo.bar = "blah"]})
192
+ //
193
+ // Note the `obj` will actually be a string, set to the key in this case.
194
+ //
195
+ if (typeof obj === 'string' && t && t.patch) {
196
+ if (typeof t.patch == 'string') t.patch = [t.patch]
197
+ // Apply the patch locally
198
+ var key = obj,
199
+ obj = bus.cache[key]
200
+
201
+ // console.log('set.fire: applying the patch!', {
202
+ // key: key,
203
+ // obj: obj,
204
+ // patch: t.patch[0]
205
+ // })
206
+ obj.val = apply_patch(obj.val, t.patch[0])
207
+ }
208
+
189
209
  // Print a statelog entry
190
210
  if (obj.key && honking_at(obj.key)) {
191
211
  // Warning: Changes to *nested* objects will *not* be printed out!
@@ -1340,7 +1360,13 @@
1340
1360
  return o.hasOwnProperty(proxied_2_keyed(k))
1341
1361
  },
1342
1362
  deleteProperty: function del (o, k) {
1363
+ var new_path = path + '[' + JSON.stringify(k) + ']'
1343
1364
  delete o[proxied_2_keyed(k)]
1365
+ bus.set(
1366
+ base,
1367
+ // Forward the patch too
1368
+ {patch: [new_path + ' = ']}
1369
+ )
1344
1370
  return true // Report success to Proxy
1345
1371
  }
1346
1372
  // For function proxies:
@@ -1771,7 +1797,7 @@
1771
1797
  : parseInt(numstr)
1772
1798
  }
1773
1799
 
1774
- console.log('Getting path', path, 'in obj', obj)
1800
+ // console.log('Getting path', JSON.stringify(path), 'in obj', obj)
1775
1801
 
1776
1802
  while (true) {
1777
1803
  var match = path_segment.exec(path),
@@ -1790,11 +1816,19 @@
1790
1816
 
1791
1817
  // If it's the final item, set it
1792
1818
  if (path.length == subpath.length) {
1793
- if (field) // Object
1794
- curr_obj[field] = new_stuff
1819
+ if (!subpath) return new_stuff
1820
+ else if (field) // Object
1821
+ if (new_stuff === undefined)
1822
+ delete curr_obj[field] // - Delete a field in object
1823
+ else
1824
+ curr_obj[field] = new_stuff // - Set a field in object
1825
+
1795
1826
  else if (typeof curr_obj == 'string') { // String
1796
- console.assert(typeof new_stuff == 'string')
1797
- if (!slice_start) {slice_start = slice_end; slice_end = slice_end+1}
1827
+ console.assert(typeof new_stuff === 'string')
1828
+ if (!slice_start) {
1829
+ slice_start = slice_end
1830
+ slice_end = slice_end + 1
1831
+ }
1798
1832
  if (last_obj) {
1799
1833
  var s = last_obj[last_field]
1800
1834
  last_obj[last_field] = (s.slice(0, slice_start)