diff --git a/ipc/sidecar.js b/ipc/sidecar.js index 5f9178df..213bbf75 100644 --- a/ipc/sidecar.js +++ b/ipc/sidecar.js @@ -13,6 +13,7 @@ const parse = require('../lib/parse') const { discoveryKey } = require('hypercore-crypto') const { isWindows, isMac } = require('which-runtime') const { SWAP, INTERNAL_UNSAFE, SPINDOWN_TIMEOUT, DESKTOP_RUNTIME, SOCKET_PATH, PLATFORM_DIR } = require('../lib/constants') +const safetyCatch = require('safety-catch') const { constructor: AGF } = async function * () {} @@ -30,7 +31,8 @@ module.exports = class IPC { constructor ({ updater, drive, corestore }) { this.updater = updater if (this.updater) { - this.updater.on('update', (checkout) => this.updateNotify(checkout)) + // TODO: await and debounce? + this.updater.on('update', (checkout) => this.updateNotify(checkout).catch(safetyCatch)) } this.server = Pipe.createServer() diff --git a/lib/bundle.js b/lib/bundle.js index 70393af9..b29b29f7 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -9,6 +9,7 @@ const Tracer = require('./tracer') const Replicator = require('./replicator') const { SWAP } = require('./constants') const releaseWatcher = require('./release-watcher') +const safetyCatch = require('safety-catch') const noop = Function.prototype module.exports = class Bundle { @@ -59,7 +60,11 @@ module.exports = class Bundle { } else { this.replicator = null } - if (typeof updateNotify === 'function') this.#updates(updateNotify) + + // TODO: why is this called here? this.#updates awaits this.ready() + // so we end up running the ready logic before the constructor fully ran + // which crashes because this.initializing is not yet set + if (typeof updateNotify === 'function') this.#updates(updateNotify).catch(safetyCatch) this.release = null @@ -86,12 +91,12 @@ module.exports = class Bundle { if (this.updatesDiff) { this.watchingUpdates = watch(this.drive) for await (const { key, length, fork, diff } of this.watchingUpdates) { - updateNotify({ key, length, fork }, { link: this.link, diff }) + await updateNotify({ key, length, fork }, { link: this.link, diff }) } } else { this.watchingUpdates = releaseWatcher(this.drive.version || 0, this.drive) for await (const upd of this.watchingUpdates) { - updateNotify( + await updateNotify( { key: this.hexKey, length: upd.length, fork: upd.fork }, { link: this.link, diff: null } ) diff --git a/lib/engine.js b/lib/engine.js index a0437e49..3029c93a 100644 --- a/lib/engine.js +++ b/lib/engine.js @@ -243,7 +243,7 @@ class Engine extends ReadyResource { try { await this.engine.updater.wait(checkout) - this.engine.sidecar.updateNotify(checkout) + await this.engine.sidecar.updateNotify(checkout) return true } catch (err) { this.report({ err })