Skip to content

Commit

Permalink
catch a couple weird errors that were being swallowed by async code
Browse files Browse the repository at this point in the history
  • Loading branch information
pvh committed Feb 2, 2025
1 parent 953ac6e commit 9e4214e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 26 deletions.
21 changes: 0 additions & 21 deletions packages/automerge-repo/src/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,27 +275,6 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> {
await withTimeout(this.#statePromise(awaitStates), this.#timeoutDelay)
}

/**
* @returns the current state of this handle's Automerge document.
*
* This is the recommended way to access a handle's document. Note that this waits for the handle
* to be ready if necessary. If loading (or synchronization) fails, this will never resolve.
*/
async legacyAsyncDoc(
/** states to wait for, such as "LOADING". mostly for internal use. */
awaitStates: HandleState[] = ["ready", "unavailable"]
) {
try {
// wait for the document to enter one of the desired states
await this.#statePromise(awaitStates)
} catch (error) {
// if we timed out, return undefined
return undefined
}
// Return the document
return !this.isUnavailable() ? this.#doc : undefined
}

/**
* Returns the current state of the Automerge document this handle manages.
*
Expand Down
10 changes: 7 additions & 3 deletions packages/automerge-repo/src/synchronizer/DocSynchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ export class DocSynchronizer extends Synchronizer {
/// PRIVATE

async #syncWithPeers() {
const doc = await this.#handle.legacyAsyncDoc() // XXX THIS ONE IS WEIRD
if (doc === undefined) return
this.#peers.forEach(peerId => this.#sendSyncMessage(peerId, doc))
try {
await this.#handle.whenReady()
const doc = this.#handle.doc() // XXX THIS ONE IS WEIRD
this.#peers.forEach(peerId => this.#sendSyncMessage(peerId, doc))
} catch (e) {
console.log("sync with peers threw an exception")
}
}

async #broadcastToPeers({
Expand Down
2 changes: 0 additions & 2 deletions packages/automerge-repo/test/DocHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ describe("DocHandle", () => {
const handle = new DocHandle<TestDoc>(TEST_ID)
assert.equal(handle.isReady(), false)

handle.legacyAsyncDoc()

assert(vi.getTimerCount() > timerCount)

// simulate loading from storage
Expand Down

0 comments on commit 9e4214e

Please sign in to comment.