statebus 7.0.28 → 7.0.30

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
@@ -147,14 +147,8 @@
147
147
  if (t.parents) put.parents = t.parents
148
148
 
149
149
  // Do we have patches?
150
- if (t.patch) {
151
- put.patches = t.patch.map(patch => {
152
- var match = patch.match(/(.*) = (.*)/),
153
- range = match[1],
154
- content = match[2]
155
- return {unit: 'json', range, content}
156
- })
157
- }
150
+ if (t.patches) put.patches = t.patches
151
+
158
152
  // Then we just have a simple body.
159
153
  else put.body = JSON.stringify(obj.val)
160
154
 
@@ -222,7 +216,7 @@
222
216
  // credentials: 'include'
223
217
  }
224
218
  ).then(res => res.subscribe(
225
- new_version => {
219
+ update => {
226
220
  // New update received!
227
221
  if (subscriptions[key].status === 'connecting') {
228
222
  console.log('%c[*] opened ' + key,
@@ -233,17 +227,15 @@
233
227
  }
234
228
 
235
229
  // Return the update
236
- if (new_version.body)
230
+ if (update.body)
237
231
  // As a snapshot body
238
232
  t.return({
239
233
  key: key,
240
- val: add_prefixes(JSON.parse(new_version.body))
234
+ val: add_prefixes(JSON.parse(update.body))
241
235
  })
242
- else if (new_version.patches)
236
+ else if (update.patches)
243
237
  // As a patch
244
- bus.set.fire(key, {patch: new_version.patches.map(
245
- patch => patch.range + ' = ' + patch.content
246
- )})
238
+ bus.set.fire(key, {patches: update.patches})
247
239
  },
248
240
  reconnect
249
241
  )).catch(reconnect)
package/extras/tests.js CHANGED
@@ -107,6 +107,15 @@ test(function applying_patches (done) {
107
107
  [[1,2,3], '[-1:-0] = [0, 0, 0]', [1, 2, 0, 0, 0]]
108
108
  ]
109
109
 
110
+ // Convert to braid patch format
111
+ for (var i=0; i<tests.length; i++) {
112
+ var match = tests[i][1].match(/(.*) = (.*)/)
113
+ tests[i][1] = {unit: 'json',
114
+ range: match[1],
115
+ content: match[2]}
116
+ }
117
+
118
+ // Test them
110
119
  for (var i=0; i<tests.length; i++) {
111
120
  var x = bus.apply_patch(tests[i][0], tests[i][1])
112
121
  assert(bus.deep_equals(x, tests[i][2]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "7.0.28",
3
+ "version": "7.0.30",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/server-library.js CHANGED
@@ -229,13 +229,10 @@ function import_server (bus, make_statebus, options)
229
229
 
230
230
  var patches = req.patches().then(patches => {
231
231
  console.log("...and we see these patches:", patches)
232
- var statebus_patches = patches.map(patch =>
233
- patch.range + ' = ' + patch.content
234
- )
235
232
  var cbus = bus.bus_for_http_client(req, res)
236
233
  var key = req.url.substr(1)
237
234
  cbus.get_once(key, (obj) => {
238
- cbus.set(key, {patch: statebus_patches})
235
+ cbus.set(key, {patches})
239
236
  res.sendStatus(200)
240
237
  console.log('We just processed a patch!')
241
238
  })
@@ -463,8 +460,8 @@ function import_server (bus, make_statebus, options)
463
460
  var msg = {set: obj}
464
461
  if (t.version) msg.version = t.version
465
462
  if (t.parents) msg.parents = t.parents
466
- if (t.patch) msg.patch = t.patch
467
- if (t.patch) msg.set = msg.set.key
463
+ if (t.patches) msg.patches = t.patches
464
+ if (t.patches) msg.set = msg.set.key
468
465
  msg = JSON.stringify(msg)
469
466
 
470
467
  if (master.simulate_network_delay) {
@@ -489,7 +486,7 @@ function import_server (bus, make_statebus, options)
489
486
  ||
490
487
  (method === 'set'
491
488
  && master.validate(message, {set: '*',
492
- '?parents': 'array', '?version': 'string', '?patch': 'array'})
489
+ '?parents': 'array', '?version': 'string', '?patches': 'array'})
493
490
  && (typeof(message.set) === 'string'
494
491
  || (typeof(message.set) === 'object'
495
492
  && typeof(message.set.key === 'string'))))
@@ -522,10 +519,10 @@ function import_server (bus, make_statebus, options)
522
519
  break
523
520
  case 'set':
524
521
  message.version = message.version || client.new_version()
525
- if (message.patch) {
522
+ if (message.patches) {
526
523
  var o = bus.cache[message.set] || {key: message.set}
527
524
  try {
528
- message.set = bus.apply_patch(o.val, message.patch[0])
525
+ message.set = bus.apply_patch(o.val, message.patches[0])
529
526
  } catch (e) {
530
527
  console.error('Received bad sockjs message from '
531
528
  + conn.remoteAddress +': ', message, e)
@@ -535,7 +532,7 @@ function import_server (bus, make_statebus, options)
535
532
  client.set(message.set,
536
533
  {version: message.version,
537
534
  parents: message.parents,
538
- patch: message.patch})
535
+ patches: message.patches})
539
536
  if (our_gets_in[message.set.key]) { // Store what we've seen if we
540
537
  // might have to publish it later
541
538
  client.log('Adding', message.set.key+'#'+message.version,
package/statebus.js CHANGED
@@ -95,22 +95,22 @@
95
95
  function set (obj, t) {
96
96
  // First let's handle patches. We receive them as:
97
97
  //
98
- // set("foo", {patch: ['.foo.bar = "blah"]})
98
+ // set("foo", {patches: [{...}, ...]})
99
99
  //
100
100
  // Note the `obj` will actually be a string, set to the key in this case.
101
101
  //
102
- if (typeof obj === 'string' && t && t.patch) {
103
- if (typeof t.patch == 'string') t.patch = [t.patch]
102
+ if (typeof obj === 'string' && t && t.patches) {
103
+ if (!Array.isArray(t.patches)) t.patches = [t.patches]
104
104
  // Apply the patch locally
105
105
  var key = obj,
106
106
  obj = bus.cache[key]
107
107
 
108
- console.log('set: applying the patch!', {
108
+ console.log('set: applying the patches!', {
109
109
  key: key,
110
110
  obj: obj,
111
- patch: t.patch[0]
111
+ patches: t.patches[0]
112
112
  })
113
- obj.val = apply_patch(obj.val, t.patch[0])
113
+ obj.val = apply_patch(obj.val, t.patches[0])
114
114
  }
115
115
 
116
116
  if (!('key' in obj) || typeof obj.key !== 'string') {
@@ -188,22 +188,22 @@
188
188
 
189
189
  // Handle patches. We receive them as:
190
190
  //
191
- // set.fire("foo", {patch: ['.foo.bar = "blah"]})
191
+ // set.fire("foo", {patches: [{...}, ...]})
192
192
  //
193
193
  // Note the `obj` will actually be a string, set to the key in this case.
194
194
  //
195
195
  if (typeof obj === 'string' && t && t.patch) {
196
- if (typeof t.patch == 'string') t.patch = [t.patch]
197
- // Apply the patch locally
196
+ if (!Array.isArray(t.patches)) t.patches = [t.patches]
197
+ // Apply the patches locally
198
198
  var key = obj,
199
199
  obj = bus.cache[key]
200
200
 
201
- // console.log('set.fire: applying the patch!', {
201
+ // console.log('set.fire: applying the patches!', {
202
202
  // key: key,
203
203
  // obj: obj,
204
- // patch: t.patch[0]
204
+ // patch: t.patches[0]
205
205
  // })
206
- obj.val = apply_patch(obj.val, t.patch[0])
206
+ obj.val = apply_patch(obj.val, t.patches[0])
207
207
  }
208
208
 
209
209
  // Print a statelog entry
@@ -1357,8 +1357,10 @@
1357
1357
  var new_path = path + '[' + JSON.stringify(k) + ']'
1358
1358
  bus.set(
1359
1359
  base,
1360
- // Forward the patch too
1361
- {patch: [new_path + ' = ' + JSON.stringify(v)]}
1360
+ // Forward the patches too
1361
+ {patches: [{unit: 'json',
1362
+ range: new_path,
1363
+ content: JSON.stringify(v)}]}
1362
1364
  )
1363
1365
  return true
1364
1366
  },
@@ -1378,8 +1380,10 @@
1378
1380
  delete o[escape_field_to_bus(escape_field_to_nelson(k))]
1379
1381
  bus.set(
1380
1382
  base,
1381
- // Forward the patch too
1382
- {patch: [new_path + ' = ']}
1383
+ // Forward the patches too
1384
+ {patches: [{unit: 'json',
1385
+ range: new_path,
1386
+ content: undefined}]}
1383
1387
  )
1384
1388
  return true // Report success to Proxy
1385
1389
  }
@@ -1517,8 +1521,8 @@
1517
1521
  var x = {set: obj}
1518
1522
  if (t.version) x.version = t.version
1519
1523
  if (t.parents) x.parents = t.parents
1520
- if (t.patch) x.patch = t.patch
1521
- if (t.patch) x.set = rem_prefix(x.set.key)
1524
+ if (t.patches) x.patches = t.patches
1525
+ if (t.patches) x.set = rem_prefix(x.set.key)
1522
1526
  send(x)
1523
1527
  }
1524
1528
  bus(prefix).getter = function (key) { send({get: key}),
@@ -1619,24 +1623,24 @@
1619
1623
  bus.log('net client received', message)
1620
1624
  var t = {version: message.version,
1621
1625
  parents: message.parents,
1622
- patch: message.patch}
1626
+ patches: message.patches}
1623
1627
 
1624
1628
  var obj
1625
1629
 
1626
- // Are we receiving a patch?
1627
- if (message.patch && typeof message.set === 'string') {
1630
+ // Are we receiving a patches?
1631
+ if (message.patches && typeof message.set === 'string') {
1628
1632
  // Then message.set is a key, and we are applying a
1629
1633
  // patch to the data at that key
1630
1634
  var key = message.set
1631
1635
  obj = apply_patch(bus.cache[key] && bus.cache[key].val,
1632
- message.patch[0])
1636
+ message.patches[0])
1633
1637
  }
1634
1638
 
1635
1639
  // Then we're receiving the full state as an object
1636
1640
  else
1637
1641
  obj = message.set
1638
1642
 
1639
- if (!(t.version||t.parents||t.patch))
1643
+ if (!(t.version||t.parents||t.patches))
1640
1644
  t = undefined
1641
1645
 
1642
1646
  bus.set.fire(add_prefixes(message.set), t)
@@ -1796,9 +1800,9 @@
1796
1800
  // The final object can be a slice
1797
1801
  // Set the value in the final object
1798
1802
 
1799
- var match = patch.match(/(.*) = (.*)/),
1800
- path = match[1],
1801
- new_stuff = match[2].length ? JSON.parse(match[2]) : undefined
1803
+ console.assert(patch.unit === 'json', "Can't apply non-json patches")
1804
+
1805
+ var path = patch.range, new_stuff = JSON.parse(patch.content)
1802
1806
 
1803
1807
  var path_segment = /^(\.?([^\.\[]+))|(\[((-?\d+):)?(-?\d+)\])|\[("(\\"|[^"])*")\]/
1804
1808
  var curr_obj = obj,