Receive and replay a thread in JavaScript
This receiver gets a vote before it gets the proposal. It also gets a second copy of the vote. The example shows the verdict and the tally at each stage.
- Install
@anuna/cbcl. - Save the definition from Write a poll dialect as
lunch-vote.cbcl. - Create
receive.mjsin the same directory. - Copy the complete receiver into the file.
On this page
Complete receiver
import { readFile } from 'node:fs/promises';
import { actRecord, createRuntime, makeHlc, openerText, ThreadStore } from '@anuna/cbcl/object';
const source = await readFile(new URL('./lunch-vote.cbcl', import.meta.url), 'utf8');
const runtime = createRuntime(source);
const thread = 'friday';
const open = actRecord(openerText({
dialect: runtime.info.name, verb: 'propose', room: '@team', thread, from: '@alice',
fields: { question: 'Lunch?', options: ['Sushi', 'Pizza'] },
}), '@alice');
const clock = makeHlc({ nowMs: 1 });
const intent = runtime.intend(thread, [open], '@bob', 'vote', { choice: 'Sushi', clock });
if (!intent.ok) throw new Error(intent.reason);
const vote = actRecord(intent.canonical, '@bob');
const inbox = new ThreadStore();
inbox.append(vote);
console.log(runtime.verdicts(thread, inbox.messages(thread)).get(vote.cid));
inbox.append(open);
console.log(runtime.verdicts(thread, inbox.messages(thread)).get(vote.cid));
console.log(inbox.append(vote));
console.log(JSON.stringify(runtime.read(thread, inbox.messages(thread)).tally));
const edit = runtime.intend(thread, inbox.messages(thread), '@bob', 'vote', {
choice: 'Pizza', clock: makeHlc({ previous: clock, nowMs: 2 }),
});
if (!edit.ok) throw new Error(edit.reason);
const revised = actRecord(edit.canonical, '@bob');
inbox.append(revised);
console.log(JSON.stringify(runtime.read(thread, inbox.messages(thread)).tally));
Open the poll
The example supplies a test identity to actRecord. In a network application, authenticate the signer before you call this function. A sender can put a different name in the :from field.
Bind a vote on the sender
Bob has seen Alice’s opener. The binder uses that history to supply the causal reference and produces canonical act text. The example uses a fixed clock value. A live host issues timestamps for each signer. It keeps the previous timestamp across restarts.
Deliver in reverse order
Run node receive.mjs. The inbox stores received records. The runtime retries pending records as the accepted set grows, then folds the accepted records.
pending
accepted
false
{"Sushi":1}
{"Pizza":1}
false means the store has a record at that address. A vote at a new address is a different act. The effect on the tally depends on the current ballot for that signer.
Change Bob’s ballot
The final call to runtime.intend uses the updated history. The binder can identify the vote that Bob wants to replace.
The last line is {"Pizza":1}. The history keeps the two vote records for replay. The projected ballot for Bob has changed. See Diagnose a missing vote for how to trace each stage independently.