statebus 7.0.16 → 7.0.18

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
@@ -107,12 +107,15 @@
107
107
  patches: the_put.patches
108
108
  }
109
109
  ).then(function (res) {
110
- if (res.status !== 200)
110
+ if (res.status === 200) {
111
+ console.log('PUT succeeded!')
112
+ puts.delete(id)
113
+ }
114
+ else
111
115
  console.error(
112
116
  'Server gave error on PUT:', e,
113
117
  'for', {body: the_put.body, patches: the_put.patches}
114
118
  )
115
- puts.delete(id)
116
119
  }).catch(function (e) {
117
120
  console.error(e, 'Error on PUT, waiting...', the_put.url)
118
121
  the_put.status = 'waiting'
@@ -198,6 +201,7 @@
198
201
  }
199
202
 
200
203
  // Else, reconnect!
204
+ console.log('Reconnecting due to', e)
201
205
  setTimeout(() => subscribe(key, t),
202
206
  reconnect_attempts > 0 ? 5000 : 1500)
203
207
  subscriptions[key].status = 'reconnecting'
@@ -229,10 +233,17 @@
229
233
  }
230
234
 
231
235
  // Return the update
232
- t.return({
233
- key: key,
234
- val: add_prefixes(JSON.parse(new_version.body))
235
- })
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
+ )})
236
247
  },
237
248
  reconnect
238
249
  )).catch(reconnect)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "7.0.16",
3
+ "version": "7.0.18",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/server-library.js CHANGED
@@ -236,8 +236,7 @@ function import_server (bus, make_statebus, options)
236
236
  var key = req.url.substr(1)
237
237
  cbus.get_once(key, (obj) => {
238
238
  cbus.set(key, {patch: statebus_patches})
239
- res.statusCode = 200
240
- res.end
239
+ res.sendStatus(200)
241
240
  console.log('We just processed a patch!')
242
241
  })
243
242
  })
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!
@@ -1771,7 +1791,7 @@
1771
1791
  : parseInt(numstr)
1772
1792
  }
1773
1793
 
1774
- console.log('Getting path', path, 'in obj', obj)
1794
+ console.log('Getting path', JSON.stringify(path), 'in obj', obj)
1775
1795
 
1776
1796
  while (true) {
1777
1797
  var match = path_segment.exec(path),
@@ -1790,11 +1810,19 @@
1790
1810
 
1791
1811
  // If it's the final item, set it
1792
1812
  if (path.length == subpath.length) {
1793
- if (field) // Object
1794
- curr_obj[field] = new_stuff
1813
+ if (!subpath) return new_stuff
1814
+ else if (field) // Object
1815
+ if (new_stuff === undefined)
1816
+ delete curr_obj[field] // - Delete a field in object
1817
+ else
1818
+ curr_obj[field] = new_stuff // - Set a field in object
1819
+
1795
1820
  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}
1821
+ console.assert(typeof new_stuff === 'string')
1822
+ if (!slice_start) {
1823
+ slice_start = slice_end
1824
+ slice_end = slice_end + 1
1825
+ }
1798
1826
  if (last_obj) {
1799
1827
  var s = last_obj[last_field]
1800
1828
  last_obj[last_field] = (s.slice(0, slice_start)