Skip to content

Commit 4e051da

Browse files
committed
chore: document public api
1 parent 6efc7b6 commit 4e051da

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"scripts": {
1717
"build": "tsc -p tsconfig.build.json",
18+
"build:watch": "tsc -p tsconfig.build.json -w",
1819
"format": "biome format ./src ./test --write",
1920
"check:lint": "biome lint ./src ./test",
2021
"check:format": "biome format ./src ./test",

src/decorator.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,76 @@ type DiffableArgs =
1616
| [_: any, { kind: string; name: string }];
1717

1818
type DiffableOpts = {
19+
/**
20+
* The name of the state, typically the name of the field.
21+
*/
1922
name?: string;
23+
/**
24+
* Diffable-objects will automatically snapshot the state perodically based on this policy to minimize
25+
* the number of diffs that must be applied to restore the state when the Durable Object is restarted.
26+
*/
2027
snapshotPolicy?: SnapshotPolicy;
2128
};
2229

30+
/**
31+
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
32+
*
33+
* ```
34+
* import { DurableObject } from "cloudflare:workers";
35+
* import { diffable } from "diffable-objects";
36+
*
37+
* class Counter extends DurableObject {
38+
* @diffable
39+
* #state = { count: 0 };
40+
*
41+
* async fetch(request) {
42+
* this.#state.count += 1;
43+
* return new Response(`Count: ${this.#state.count}`);
44+
* }
45+
* }
46+
* ```
47+
*/
2348
export function diffable(
2449
_: any,
2550
{ kind, name }: { kind: string; name: string },
2651
): FieldDecoratorReturn<any>;
52+
/**
53+
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
54+
*
55+
* ```
56+
* import { DurableObject } from "cloudflare:workers";
57+
* import { diffable } from "diffable-objects";
58+
*
59+
* class Counter extends DurableObject {
60+
* @diffable("counter")
61+
* #state = { count: 0 };
62+
*
63+
* async fetch(request) {
64+
* this.#state.count += 1;
65+
* return new Response(`Count: ${this.#state.count}`);
66+
* }
67+
* }
68+
* ```
69+
*/
2770
export function diffable(name?: string): FieldDecoratorFactoryReturn<any>;
71+
/**
72+
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
73+
*
74+
* ```
75+
* import { DurableObject } from "cloudflare:workers";
76+
* import { diffable } from "diffable-objects";
77+
*
78+
* class Counter extends DurableObject {
79+
* @diffable({ name: "counter", snapshotPolicy: "never" })
80+
* #state = { count: 0 };
81+
*
82+
* async fetch(request) {
83+
* this.#state.count += 1;
84+
* return new Response(`Count: ${this.#state.count}`);
85+
* }
86+
* }
87+
* ```
88+
*/
2889
export function diffable(
2990
options: DiffableOpts,
3091
): FieldDecoratorFactoryReturn<any>;

src/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,37 @@ export * from "./decorator.js";
77
export type SnapshotPolicy = "never" | "every-change" | { changes: number };
88

99
export type StateOptions = {
10+
/**
11+
* Diffable-objects will automatically snapshot the state perodically based on this policy to minimize the
12+
* number of diffs that must be applied to restore the state when the Durable Object is restarted.
13+
*/
1014
snapshotPolicy?: SnapshotPolicy;
1115
};
1216

17+
/**
18+
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
19+
*
20+
* @example
21+
* ```
22+
* import { DurableObject } from "cloudflare:workers";
23+
* import { state } from "diffable-objects";
24+
*
25+
* class Counter extends DurableObject {
26+
* #state = state(this, "counter", { count: 0 });
27+
*
28+
* async fetch(request) {
29+
* this.#state.count += 1;
30+
* return new Response(`Count: ${this.#state.count}`);
31+
* }
32+
* }
33+
* ```
34+
*
35+
* @param ctx the DurableObject state.
36+
* @param name the name of the state, typically the name of the field.
37+
* @param initialState the initial state of this object, this must be the same every time the DO is created.
38+
* @param options options for configuring how the state is persisted.
39+
* @returns a copy of state that will persist changes.
40+
*/
1341
export function state<T extends object>(
1442
ctx: DurableObjectState,
1543
name: string,

0 commit comments

Comments
 (0)