Docs/State & events

State & events

Keep durable state with its owner and connect actors with typed events.

Scoped state

Declare actor state in .scope(). Collections can generate their own primary IDs. Actions read and mutate state through this.state.

src/todos/todos.ts
import { Actor, Event, State } from "taskwish";

export const { actor } = Actor("Todos").scope(
  Event("TodoCompleted", {
    id: "string",
    description: "string",
  }),

  State({
    items: State.List({
      id: "primary.uuidv4.random",
      description: "string",
      done: "boolean",
    }),
  }),
);

Signal an event

Events are qualified by their owning actor. Signal them from a step; subscribed actors receive a typed event payload.

taskwish.ts
Step("signalTodoCompleted", function () {
  return this.signal("Todos::TodoCompleted", {
    id: this.markComplete.id,
    description: this.markComplete.description,
  });
});
Note
Events decouple actors. For direct coordination, declare a service with .use() and call it through this.actions.