@mblaney/holster 1.0.19 → 1.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mblaney/holster",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "Holster is a service that allows synchronising data between devices using Node, Deno, Bun or the browser.",
5
5
  "type": "module",
6
6
  "main": "index.js",
package/src/wire.js CHANGED
@@ -264,33 +264,34 @@ const Wire = opt => {
264
264
  const put = async (msg, send) => {
265
265
  // Store updates returned from Ham.mix and defer updates if required.
266
266
  const update = await Ham.mix(msg.put, graph, opt.secure, listen)
267
- if (Object.keys(update.now).length) {
268
- if (!(await check(update.now, send))) return
269
-
270
- store.put(update.now, err => {
271
- send(
272
- JSON.stringify({
273
- "#": dup.track(utils.text.random(9)),
274
- "@": msg["#"],
275
- err: err,
276
- }),
267
+ if (Object.keys(update.now).length === 0) {
268
+ // No updates to store, check deferred.
269
+ if (Object.keys(update.defer).length !== 0) {
270
+ setTimeout(
271
+ () => put({put: update.defer, "#": msg["#"]}, send),
272
+ update.wait,
277
273
  )
278
- })
274
+ }
275
+ return
279
276
  }
280
277
 
281
- // Always broadcast original data to other peers even if no local update
282
- const sendResult = send(
283
- JSON.stringify({
284
- "#": dup.track(utils.text.random(9)),
285
- put: msg.put,
286
- }),
287
- )
288
- if (sendResult && sendResult.err) {
289
- console.log(`Error broadcasting put: ${sendResult.err}`)
290
- }
278
+ if (!(await check(update.now, send))) return
291
279
 
292
- if (Object.keys(update.defer).length) {
293
- setTimeout(() => put({put: update.defer}, send), update.wait)
280
+ store.put(update.now, err => {
281
+ send(
282
+ JSON.stringify({
283
+ "#": dup.track(utils.text.random(9)),
284
+ "@": msg["#"],
285
+ err: err,
286
+ }),
287
+ )
288
+ })
289
+
290
+ if (Object.keys(update.defer).length !== 0) {
291
+ setTimeout(
292
+ () => put({put: update.defer, "#": msg["#"]}, send),
293
+ update.wait,
294
+ )
294
295
  }
295
296
  }
296
297
 
@@ -365,29 +366,29 @@ const Wire = opt => {
365
366
  // used whereas wire spec needs to handle clock skew for updates
366
367
  // across the network.
367
368
  const update = await Ham.mix(data, graph, opt.secure, listen)
369
+ if (Object.keys(update.now).length === 0) {
370
+ // No updates, still respond to callback.
371
+ if (cb) cb(null)
372
+ return
373
+ }
368
374
 
369
- if (Object.keys(update.now).length !== 0) {
370
- if (!(await check(update.now, send, cb))) return
371
-
372
- // Seed pendingReferences with any new references from API calls
373
- for (const [soul, node] of Object.entries(update.now)) {
374
- if (node && typeof node === "object") {
375
- for (const [key, value] of Object.entries(node)) {
376
- const soulId = utils.rel.is(value)
377
- if (soulId) {
378
- // Add referenced soul to pending list
379
- pendingReferences.add(soulId)
380
- }
375
+ if (!(await check(update.now, send, cb))) return
376
+
377
+ // Seed pendingReferences with any new references from API calls
378
+ for (const [soul, node] of Object.entries(update.now)) {
379
+ if (node && typeof node === "object") {
380
+ for (const [key, value] of Object.entries(node)) {
381
+ const soulId = utils.rel.is(value)
382
+ if (soulId) {
383
+ // Add referenced soul to pending list
384
+ pendingReferences.add(soulId)
381
385
  }
382
386
  }
383
387
  }
384
-
385
- store.put(update.now, cb)
386
- } else {
387
- // No local update needed, but still callback
388
- if (cb) cb(null)
389
388
  }
390
389
 
390
+ store.put(update.now, cb)
391
+
391
392
  // Always put data on the wire spec even if no local update needed
392
393
  const sendResult = send(
393
394
  JSON.stringify({
@@ -773,12 +774,11 @@ const Wire = opt => {
773
774
  }
774
775
  // Store filtered data if any
775
776
  if (Object.keys(filteredPut).length > 0) {
776
- // Create filtered message for Ham.mix
777
- const filteredMsg = {put: filteredPut}
777
+ // Create filtered message for Ham.mix with original message ID
778
+ const filteredMsg = {put: filteredPut, "#": msg["#"]}
778
779
  put(filteredMsg, send)
779
780
  }
780
781
  }
781
- send(m.data)
782
782
 
783
783
  const id = msg["@"]
784
784
  const cb = queue[id]