tragedy
v0.1.0-alpha.0
Published
A tiny TypeScript actor runtime for typed local actors.
Maintainers
Readme
tragedy
tragedy is a tiny TypeScript actor runtime experiment: define typed protocols, bind them to actor classes, and talk to local entities through typed refs.
Install
npm install tragedy@alphaExample
import { Actor, ActorSystem, entity, local, msg, type } from "tragedy";
const Counter = entity("Counter", {
key: type<string>(),
messages: {
inc: msg<{ by?: number }>().ack(),
get: msg().reply<number>(),
},
});
class CounterActor extends Actor.for(Counter) {
value = 0;
receive = this.handlers({
inc: (_ctx, { by }) => {
this.value += by ?? 1;
},
get: () => this.value,
});
}
const system = await ActorSystem.start({
name: "demo",
runtime: local(),
entities: [CounterActor],
});
const counter = system.entity(Counter, "global");
await counter.ask.inc({ by: 1 }, { deadline: "500ms" });
console.log(await counter.ask.get({ deadline: "500ms" }));Model
entity,actor, andservicedescribe protocols with typed messages.Actor.for(protocol)binds a concrete actor class to a protocol.ActorSystem.start({ runtime: local() })runs the current in-process runtime.askrequires a deadline and returns a typed reply.tellsends fire-and-forget messages.deliveris reserved for future durable delivery.
Scripts
npm run typecheck
npm run lint
npm run build
npm run example