Skip to content
Sheetwrite

@sheetwrite/core/history

HistoryArchiveclass

Report a problem

On this page

Opt-in archive of named and automatic versions for one document. It stores content-addressed checkpoints plus the committed transactions since each one, through a host HistoryStorageAdapter. A grid that never imports this module has no history state.

Calls that change the archive run one at a time. Each publishes atomically, so a failure or abort leaves the last durable version readable.

Members

Methods

deleteVersion: (id: string, signal?: AbortSignal) => Promise<void>;

Delete one version. Records it shares with other versions stay.

diffVersions: (request: HistoryDiffRequest) => Promise<HistoryDiffPage>;

One page of the changes between two retained versions. It lists the cells that changed, with before and after and who wrote them; sheets added, removed or changed outside their cells; and workbook fields that changed.

Both versions are rebuilt in private engine instances, or taken from the last readVersion or restore, and compared in bounded windows by the history diff kernel, a separate WASM module that loads on the first diff. Rows and columns are aligned through the structural operations of the log, so an inserted row does not make the rows below it changed. Only the checkpoint pages that can differ are read and rebuilt (see selectPages); the result is the same as rebuilding both versions whole. A diff keeps nothing between pages: pass cursor to continue, and each page rebuilds what it needs again.

initialize: (snapshot: WorkbookSnapshot, time: number, signal?: AbortSignal) => Promise<void>;

Store the first checkpoint: a complete, committed snapshot of the document at time.

listVersions: (options?: ListVersionsOptions) => Promise<HistoryListPage>;

One bounded page of version metadata, newest first.

prune: (options?: PruneOptions) => Promise<void>;

Remove old automatic versions to free space. It removes those made at or before beforeTime (default: the age ceiling) and, oldest first, more while the archive is above targetBytes. Named versions and every record they need stay.

readVersion: (id: string, signal?: AbortSignal) => Promise<WorkbookSnapshot>;

Rebuild the document of one version. A reader lease keeps its records alive while a concurrent prune runs; the lease is released on every exit. The archive keeps the rebuilt version for the calls that follow, until releaseMemory.

record: (commit: CommittedTransaction, capture?: HistorySnapshotProvider, signal?: AbortSignal) => Promise<void>;

Append one committed transaction. capture runs only when a checkpoint is due and must return the committed document at commit.revision. An automatic version is made after automaticIntervalMs of active time.

releaseMemory: () => void;

Free the version that the archive keeps rebuilt between calls. The version history panel calls it when it closes and when its grid trims memory. A call that still uses the version frees it when it returns.

restoreVersion: (grid: Grid, id: string, scope: HistoryRestoreScope, options?: HistoryRestoreOptions) => Promise<ApplyTransactionResult>;

Restore one version, or one sheet or range of it, on grid as one undoable transaction. The version is rebuilt in a private engine instance, or taken from the last readVersion or restore, and compared with the grid's current document; the difference is committed as one atomic restore, which SyncCoordinator sends as one transaction that is never rebased.

With options.sync, the coordinator must be online with no pending or conflicted commits. When its server version is the archive head, the grid holds the head, so only the checkpoint pages where the version and the head differ are read and written again (see selectRestorePages).

A change to the grid while the version is read, or a scope that cuts a merged cell or hyperlink, rejects before anything changes. See the restore section of the persistence guide for what each scope covers.

saveVersion: (name: string, authorId: string, time: number, signal?: AbortSignal) => Promise<HistoryVersion>;

Save the current version under a name. The host flushes pending edits first, so the name covers them.

storageUsage: (signal?: AbortSignal) => Promise<HistoryStorageUsage>;

Retained bytes and counts, read from the manifest without fetching any payload.

Declaration

View full TypeScript declaration
class HistoryArchive {
constructor(
limits: Readonly<HistoryLimits>,
manifest: HistoryManifest,
);
deleteVersion: (id: string, signal?: AbortSignal) => Promise<void>;
diffVersions: (request: HistoryDiffRequest) => Promise<HistoryDiffPage>;
initialize: (
snapshot: WorkbookSnapshot,
time: number,
signal?: AbortSignal,
) => Promise<void>;
listVersions: (options?: ListVersionsOptions) => Promise<HistoryListPage>;
prune: (options?: PruneOptions) => Promise<void>;
readVersion: (
id: string,
signal?: AbortSignal,
) => Promise<WorkbookSnapshot>;
record: (
signal?: AbortSignal,
) => Promise<void>;
releaseMemory: () => void;
restoreVersion: (
grid: Grid,
id: string,
) => Promise<ApplyTransactionResult>;
saveVersion: (
name: string,
authorId: string,
time: number,
signal?: AbortSignal,
) => Promise<HistoryVersion>;
storageUsage: (signal?: AbortSignal) => Promise<HistoryStorageUsage>;
static open: (
signal?: AbortSignal,
) => Promise<HistoryArchive>;
}