Write optional causal logic in generator.workspec.js. Register one or more synchronous lifecycle callbacks.

Direct registration

JavaScript
WorkSpec.onStart((context) => {
  context.set("queue", "arrivals", 0);
});

WorkSpec.onUpdate((context) => {
  if (context.random() < 0.1) {
    context.change("queue", "arrivals", 1);
  }
});

WorkSpec.onStart and WorkSpec.onUpdate require functions. You can register multiple callbacks for each lifecycle stage.

Object registration

JavaScript
WorkSpec.generator({
  onStart: ({ set }) => set("room", "temperature", 20),
  onUpdate: ({ get, set }) => {
    set("room", "temperature", get("room", "temperature") + 0.1);
  }
});

WorkSpec.generator accepts an object with optional onStart and onUpdate functions.

Callback contract

Callbacks must return synchronously. Generator compilation uses ordinary JavaScript source evaluation.

The runtime catches thrown callback errors and reports generator.execution.failed. A source evaluation failure reports generator.compile.failed.

The shared in-process runtime cannot stop a callback that never returns. Read the trusted-code contract before executing third-party source.

Checked JavaScript

JavaScript
// @ts-check
/// <reference types="workspec/workspec-generator" />

The package publishes the declaration as workspec/workspec-generator.d.ts. Shared conventions live in executable source files.