import type * as A from '@automerge/automerge'; import { type AutomergeUrl, type DocHandle, type DocHandleChangePayload, type DocumentId, type DocumentProgress, type Heads, type UrlHeads } from '@automerge/automerge-repo'; import { type HandleQueryState } from './handle-state.ts'; /** One loaded document as the repo holds it now. */ export type DocumentPair = { query: DocumentProgress; handle: DocHandle; }; /** * How a lease reaches its document: `open` faults it in if it is not loaded, `peek` never does — * teardown has to be able to detach a listener without resurrecting an evicted document. */ export type DocumentResolver = { open: () => DocumentPair; peek: () => DocumentPair | undefined; }; /** What a subscriber learns about a leased document — never the handle. */ export type DocumentLeaseState = { state: HandleQueryState; }; /** * Events a holder can observe on the leased document. */ export type DocumentLeaseEvents = { 'change': DocHandleChangePayload; 'heads-changed': { doc: A.Doc; heads: Heads; }; }; /** * A borrowed reference to one loaded document, held for as long as the holder needs it. * * The repo caches a document forever once anything faults it in, so residency has to be decided by * the host rather than by the repo: a lease records that decision. The underlying `DocHandle` is * never handed out — a holder that captured one would keep reading (and writing) a document the host * has since evicted, and no reference count could see it — so every operation goes through the lease * and stops working the moment it is disposed. * * Disposal is idempotent and, being `Symbol.dispose`, is usually written as `using lease = ...`. * A holder that outlives its scope (a subscription, a space root) keeps the lease in a field and * disposes it in its own teardown. */ export declare class DocumentLease implements Disposable { #private; private readonly _documentId; /** * The pair is resolved through the registry rather than captured, because an eviction of the same * document may still be draining: a lease taken during that window must operate on the pair the * repo holds after it, not the pair being torn down. */ constructor(_documentId: DocumentId, resolve: DocumentResolver, release: () => void); /** Readable after disposal: an id is not a claim on the document. */ get documentId(): DocumentId; /** Derived from the id, so it reads the same whether or not the document is loaded. */ get url(): AutomergeUrl; /** Readiness of the document — the `DocHandle`'s own predicates lie, see `getHandleState`. */ get state(): HandleQueryState; /** Ready with the stored copy merged in — see {@link isLoaded}; what {@link waitUntilReady} waits for. */ get loaded(): boolean; get disposed(): boolean; /** * The document, or the empty initial document while it is still loading — check {@link state} * (or await {@link waitUntilReady}) when the difference matters. */ doc(): A.Doc; /** Heads of the document as loaded now — an empty document while it is still loading. */ heads(): UrlHeads; /** Mutates the document; the change is saved and synced by the repo's own listeners. */ change(callback: A.ChangeFn, options?: A.ChangeOptions): void; /** Mutates the document as of `heads`, returning the resulting heads, or undefined if none applied. */ changeAt(heads: UrlHeads, callback: A.ChangeFn, options?: A.ChangeOptions): UrlHeads | undefined; /** * Resolves once the document is loaded, rejects if the query fails. * * `'unavailable'` is transient rather than terminal — the query routinely reports it while no peer * serves the document yet — so this waits through it, unlike `DocumentQuery.whenReady`. */ waitUntilReady(): Promise; /** Registers a listener on the document as loaded now; a later eviction detaches it. */ on>(event: K, listener: (payload: DocumentLeaseEvents[K]) => void): void; /** * Removes a listener from the handle it was registered on, not from whatever the document resolves * to now: an eviction and re-acquisition swaps the handle, so detaching from the current one would * strip a listener another lease registered. It also never faults a document in, so teardown after * an eviction (or after the repo closed) cannot resurrect one. */ off>(event: K, listener: (payload: DocumentLeaseEvents[K]) => void): void; /** Registers a one-shot listener; see {@link on} for what an eviction does to it. */ once>(event: K, listener: (payload: DocumentLeaseEvents[K]) => void): void; /** * Observes readiness and sync transitions. Returns an unsubscribe function. * * Only the state is handed out: the query's own payload carries the `DocHandle`, which a * subscriber could keep past disposal. */ subscribe(callback: (state: DocumentLeaseState) => void): () => void; [Symbol.dispose](): void; } export type DocumentLeaseRegistryParams = { /** * Opens (or returns the cached) query and its attached handle for a document — the one place the * repo is consulted. */ open: (documentId: DocumentId) => DocumentPair; /** * Drops the document from the repo cache once nothing holds it. Runs after the last lease is * disposed. Eviction is asynchronous, so a lease can be taken while it runs: `isCancelled` must be * consulted after every await, and the document left alone once it reads true. * * Resolves `false` when the document was left resident (still loading, or the drop failed), which * puts it back in the queue for a later attempt. */ evict: (documentId: DocumentId, isCancelled: () => boolean) => Promise; /** * How long a document stays resident after its last lease is disposed. Faulting a document back in * costs a load from storage, so a document released between two passes of the same workload has to * survive the gap rather than be evicted into it. */ evictionDelay?: number; /** * How many released documents stay resident regardless of age — the most recently released ones. * A floor keeps the hot working set loaded on a host whose whole workload is shorter than the * delay, and bounds nothing else: leased documents are never counted against it. */ minResidentDocuments?: number; /** How many released documents may wait out the delay. Past it the oldest are evicted at once. */ maxIdleDocuments?: number; }; /** * Reference counts the documents the host holds loaded, and evicts each one once its last lease has * been disposed for {@link DocumentLeaseRegistryParams.evictionDelay}, oldest release first, keeping * the {@link DocumentLeaseRegistryParams.minResidentDocuments} most recently released. * * Eviction is never immediate: it has to drain the document's pending save, which is asynchronous * while disposal is not, and evicting on the last dispose would thrash a workload that releases and * re-reads the same document across passes. */ export declare class DocumentLeaseRegistry { #private; constructor(params: DocumentLeaseRegistryParams); /** Documents currently leased — the host's document residency. */ get size(): number; get leasedDocumentIds(): DocumentId[]; /** Released documents still resident, waiting out the delay or held by the floor. */ get idleCount(): number; /** Whether a lease is outstanding — an idle-but-resident document reads `false`, see {@link idleCount}. */ isLeased(documentId: DocumentId): boolean; acquire(documentId: DocumentId): DocumentLease; /** * Drops all bookkeeping for a document without evicting it — for a caller that removed the * document itself, where a later eviction would re-create the query it just deleted. */ forget(documentId: DocumentId): void; /** * Stops evicting and forgets every count, without disposing the outstanding leases: the repo is * being shut down, so unloading one document at a time would be wasted work. */ close(): void; /** * Closes, then waits for an eviction that is already draining — it holds the repo's storage and * would otherwise flush into an adapter the caller is about to shut down. */ closeAndSettle(): Promise; /** * Evicts every released document now, ignoring the delay and the floor — for a caller that needs * residency settled before it measures it. */ drain(): Promise; } //# sourceMappingURL=document-lease.d.ts.map