Skip to content

Commit

Permalink
working on timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
pvh committed Aug 4, 2023
1 parent 4dfb0b4 commit a169b06
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
39 changes: 28 additions & 11 deletions packages/automerge-repo/src/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ export class DocHandle<T> //
* Internally we use a state machine to orchestrate document loading and/or syncing, in order to
* avoid requesting data we already have, or surfacing intermediate values to the consumer.
*
* ┌─────────┐ ┌────────────┐
* ┌───────┐ ┌──FIND──┤ loading ├─REQUEST──►│ requesting ├─UPDATE──┐
* ┌─────────────────────┬─────────TIMEOUT────►┌────────┐
* ┌───┴─────┐ ┌───┴────────┐ │ failed │
* ┌───────┐ ┌──FIND──┤ loading ├─REQUEST──►│ requesting ├─UPDATE──┐ └────────┘
* │ idle ├──┤ └───┬─────┘ └────────────┘ │
* └───────┘ │ │ └─►┌────────
* │ └───────LOAD───────────────────────────────►│ ready │
* └──CREATE───────────────────────────────────────────────►└────────
* └───────┘ │ │ └─►┌────────┐
* │ └───────LOAD───────────────────────────────►│ ready │
* └──CREATE───────────────────────────────────────────────►└────────┘
*/
this.#machine = interpret(
createMachine<DocHandleContext<T>, DocHandleEvent<T>>(
Expand Down Expand Up @@ -77,6 +78,12 @@ export class DocHandle<T> //
REQUEST: { target: REQUESTING },
DELETE: { actions: "onDelete", target: DELETED },
},
after: [
{
delay: this.#timeoutDelay,
target: FAILED,
},
],
},
requesting: {
on: {
Expand All @@ -86,6 +93,12 @@ export class DocHandle<T> //
REQUEST_COMPLETE: { target: READY },
DELETE: { actions: "onDelete", target: DELETED },
},
after: [
{
delay: this.#timeoutDelay,
target: FAILED,
},
],
},
ready: {
on: {
Expand All @@ -94,8 +107,12 @@ export class DocHandle<T> //
DELETE: { actions: "onDelete", target: DELETED },
},
},
error: {},
deleted: {},
failed: {
type: "final",
},
deleted: {
type: "final",
},
},
},

Expand Down Expand Up @@ -196,8 +213,8 @@ export class DocHandle<T> //
* @returns true if the document has been marked as deleted
*/
isDeleted = () => this.inState([HandleState.DELETED])
inState = (awaitStates: HandleState[]) =>
awaitStates.some(state => this.#machine?.getSnapshot().matches(state))
inState = (states: HandleState[]) =>
states.some(state => this.#machine?.getSnapshot().matches(state))

/**
* Use this to block until the document handle has finished loading.
Expand Down Expand Up @@ -352,7 +369,7 @@ export const HandleState = {
LOADING: "loading",
REQUESTING: "requesting",
READY: "ready",
ERROR: "error",
FAILED: "failed",
DELETED: "deleted",
} as const
export type HandleState = (typeof HandleState)[keyof typeof HandleState]
Expand Down Expand Up @@ -425,7 +442,7 @@ type DocHandleXstateMachine<T> = Interpreter<

// CONSTANTS

export const { IDLE, LOADING, REQUESTING, READY, ERROR, DELETED } = HandleState
export const { IDLE, LOADING, REQUESTING, READY, FAILED, DELETED } = HandleState
const {
CREATE,
LOAD,
Expand Down
4 changes: 3 additions & 1 deletion packages/automerge-repo/test/DocHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe("DocHandle", () => {

it("should not time out if the document is updated in time", async () => {
// set docHandle time out after 5 ms
const handle = new DocHandle<TestDoc>(TEST_ID, { timeoutDelay: 5 })
const handle = new DocHandle<TestDoc>(TEST_ID, { timeoutDelay: 1 })

// simulate requesting from the network
handle.request()
Expand All @@ -259,6 +259,8 @@ describe("DocHandle", () => {
})

// now it should not time out
await pause(5)

const doc = await handle.doc()
assert.equal(doc.foo, "bar")
})
Expand Down
4 changes: 2 additions & 2 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Repo", () => {
assert.equal(handle.isReady(), false)

return assert.rejects(
rejectOnTimeout(handle.doc(), 100),
rejectOnTimeout(handle.doc(), 10),
"This document should not exist"
)
})
Expand Down Expand Up @@ -292,7 +292,7 @@ describe("Repo", () => {
assert.equal(handle.isReady(), false)

return assert.rejects(
rejectOnTimeout(handle.doc(), 100),
rejectOnTimeout(handle.doc(), 10),
"This document should not exist"
)
})
Expand Down

0 comments on commit a169b06

Please sign in to comment.