From 2c5e0b04bf467697cd9c469ab25e760bf90b7f3b Mon Sep 17 00:00:00 2001 From: Herb Caudill Date: Sun, 21 Jan 2024 22:16:25 +0100 Subject: [PATCH] refactor DocHandleOptions initialValue can only be set when isNew=true, and timeoutDelay is mutually exclusive from either --- packages/automerge-repo/src/DocHandle.ts | 46 +++++++++++++----------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/automerge-repo/src/DocHandle.ts b/packages/automerge-repo/src/DocHandle.ts index 90dc6a75a..74139fc35 100644 --- a/packages/automerge-repo/src/DocHandle.ts +++ b/packages/automerge-repo/src/DocHandle.ts @@ -39,7 +39,7 @@ export class DocHandle // #log: debug.Debugger #machine: DocHandleXstateMachine - #timeoutDelay: number + #timeoutDelay: number = 60_000 #remoteHeads: Record = {} /** The URL of this document @@ -54,28 +54,30 @@ export class DocHandle // /** @hidden */ constructor( public documentId: DocumentId, - { - isNew = false, - timeoutDelay = 60_000, - initialValue = {} as T, - }: DocHandleOptions = {} + options: DocHandleOptions = {} ) { super() - this.#timeoutDelay = timeoutDelay - this.#log = debug(`automerge-repo:dochandle:${this.documentId.slice(0, 5)}`) - let doc: T + this.documentId = documentId + + if ("timeoutDelay" in options && options.timeoutDelay) { + this.#timeoutDelay = options.timeoutDelay + } + let doc: T + const isNew = "isNew" in options && options.isNew if (isNew) { // T should really be constrained to extend `Record` (an automerge doc can't be // e.g. a primitive, an array, etc. - it must be an object). But adding that constraint creates // a bunch of other problems elsewhere so for now we'll just cast it here to make Automerge happy. - doc = A.from(initialValue as Record) as T + doc = A.from(options.initialValue as Record) as T doc = A.emptyChange(doc) } else { doc = A.init() } + this.#log = debug(`automerge-repo:dochandle:${this.documentId.slice(0, 5)}`) + /** * 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. @@ -459,16 +461,20 @@ export class DocHandle // // WRAPPER CLASS TYPES /** @hidden */ -export interface DocHandleOptions { - /** If we know this is a new document (because we're creating it) this should be set to true. */ - isNew?: boolean - - /** The number of milliseconds before we mark this document as unavailable if we don't have it and nobody shares it with us. */ - timeoutDelay?: number - - /** The initial value of the document. */ - initialValue?: T -} +export type DocHandleOptions = + // NEW DOCUMENTS + | { + /** If we know this is a new document (because we're creating it) this should be set to true. */ + isNew: true + + /** The initial value of the document. */ + initialValue?: T + } + // EXISTING DOCUMENTS + | { + /** The number of milliseconds before we mark this document as unavailable if we don't have it and nobody shares it with us. */ + timeoutDelay?: number + } export interface DocHandleMessagePayload { destinationId: PeerId