Quick Start
Actor processes for JavaScript, built on generator functions.
Install
npm install posipaki
Your first actor
import { defineActor, defineMessages } from "posipaki";
type MPoke = { type: "POKE" };
type MPing = { type: "PING"; step: number };
const counter = defineActor({
inMessages: defineMessages<MPoke | MPing>(),
setup: () => ({ count: 0 }),
handlers: {
POKE(msg) {
// msg: { type: "POKE" }
this.state.count++;
},
PING(msg) {
// msg: { type: "PING"; step: number }
this.state.count += msg.step;
},
},
});
const proc = await counter.spawn(null);
await proc.ready();
proc.send({ type: "POKE" }); // proc.state.count === 1
proc.send({ type: "PING", step: 5 }); // proc.state.count === 6
In-messages are a discriminated union on type. Hand it to
defineMessages<MPoke | MPing>() and each handler's msg is narrowed to the
matching variant.
A process receives messages, updates its own state, and sends messages back. It can also fork children, pause/resume, and exit on its own terms.
Live demo
Messages are processed one at a time — each type sleeps for a different duration. Press the buttons to queue waves through the tunnel; the active handler is highlighted as its message is processed.