Skip to content

Commit

Permalink
refactor DocHandleOptions
Browse files Browse the repository at this point in the history
initialValue can only be set when isNew=true, and timeoutDelay is mutually exclusive from either
  • Loading branch information
HerbCaudill committed Jan 21, 2024
1 parent d8dce05 commit 2c5e0b0
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions packages/automerge-repo/src/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class DocHandle<T> //
#log: debug.Debugger

#machine: DocHandleXstateMachine<T>
#timeoutDelay: number
#timeoutDelay: number = 60_000
#remoteHeads: Record<StorageId, A.Heads> = {}

/** The URL of this document
Expand All @@ -54,28 +54,30 @@ export class DocHandle<T> //
/** @hidden */
constructor(
public documentId: DocumentId,
{
isNew = false,
timeoutDelay = 60_000,
initialValue = {} as T,
}: DocHandleOptions<T> = {}
options: DocHandleOptions<T> = {}
) {
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<string, unknown>` (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<string, unknown>) as T
doc = A.from(options.initialValue as Record<string, unknown>) as T
doc = A.emptyChange<T>(doc)
} else {
doc = A.init<T>()
}

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.
Expand Down Expand Up @@ -459,16 +461,20 @@ export class DocHandle<T> //
// WRAPPER CLASS TYPES

/** @hidden */
export interface DocHandleOptions<T> {
/** 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<T> =
// 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
Expand Down

0 comments on commit 2c5e0b0

Please sign in to comment.