UNPKG

diffable-objects

Version:

A package for dynamic state tracking for Cloudflare's Durable Objects using SQLite

37 lines (36 loc) 1.36 kB
export * from "./decorator.js"; export type SnapshotPolicy = "never" | "every-change" | { changes: number; }; export type StateOptions = { /** * Diffable-objects will automatically snapshot the state perodically based on this policy to minimize the * number of diffs that must be applied to restore the state when the Durable Object is restarted. */ snapshotPolicy?: SnapshotPolicy; }; /** * Dynamically create a state object that persists changes to durable storage using Proxy and SQLite. * * @example * ``` * import { DurableObject } from "cloudflare:workers"; * import { state } from "diffable-objects"; * * class Counter extends DurableObject { * #state = state(this, "counter", { count: 0 }); * * async fetch(request) { * this.#state.count += 1; * return new Response(`Count: ${this.#state.count}`); * } * } * ``` * * @param ctx the DurableObject state. * @param name the name of the state, typically the name of the field. * @param initialState the initial state of this object, this must be the same every time the DO is created. * @param options options for configuring how the state is persisted. * @returns a copy of state that will persist changes. */ export declare function state<T extends object>(ctx: DurableObjectState, name: string, initialState: T, options?: StateOptions): T;