chore(vendor): import pi 1.3.0
Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: 71023aec1546d6d677a65669c5665c0927ce0dd0
This commit is contained in:
1 parent
719370c8f7
commit
6081e3e325
1743 files changed
+205459
-131704
No files matched your search
@@ -0,0 +1,37 @@
|
||||
// Expose a document through Chord.
|
||||
// Run from packages/durable:
|
||||
// node --conditions=source --experimental-strip-types test/examples/04-chord-state.ts
|
||||
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
||||
import { createSession, defineDoc, MemoryStorage } from "../../src/index.ts";
|
||||
|
||||
const context = BACKGROUND_CONTEXT;
|
||||
const session = createSession(new MemoryStorage());
|
||||
|
||||
const Notes = defineDoc<{ text: string }>({
|
||||
kind: "example.notes",
|
||||
version: 1,
|
||||
scope: "conversation",
|
||||
history: "rewindable",
|
||||
fork: "asOf",
|
||||
initial: () => ({ text: "" }),
|
||||
});
|
||||
const chat = await session.commit(async (tx) => {
|
||||
const conversation = await tx.createConversation({ ownership: { kind: "ownerless" } });
|
||||
(await tx.doc(Notes, conversation.id)).text = "first";
|
||||
return conversation;
|
||||
}, context);
|
||||
|
||||
// documentState() never creates a document. It returns a hydrated read-only
|
||||
// Chord state bound to the current concrete incarnation.
|
||||
const notesState = await session.documentState(Notes, chat.id, context);
|
||||
if (notesState === undefined) throw new Error("notes are absent");
|
||||
const stopNotes = notesState.subscribe((value, _deliveryContext, delivery) => {
|
||||
console.log("Chord notes:", delivery.kind, delivery.sequence, value);
|
||||
});
|
||||
await session.commit(async (tx) => {
|
||||
(await tx.doc(Notes, chat.id)).text = "published through Chord";
|
||||
}, context);
|
||||
stopNotes();
|
||||
notesState.dispose();
|
||||
|
||||
await session.close(context);
|
||||
Reference in new issue
Block a user