Runs actors: many charts, many images, from a handful of actors to millions, on the runtime's event loop. Inputs (spawn, send, ...) apply at once and schedule a turn; in its turns the host runs the actors and calls your handlers with what they sent out. There is no run(): await host.idle() waits until there is nothing left to do.
The application API
constructor(charts: Charts, options: HostOptions);
A host of system options.system running charts (each image added in order, a segment each: addImage), with the handlers and choices of options. Throws TinyactorsError (TA_ERROR_INVALID) for a config the host does not take, TypeError for options it does not take.
readonly system: string;
The system id, options.system.
readonly closed: boolean;
True once close() ran; every input of a closed host throws.
readonly nextDeadline: number | undefined;
Milliseconds from the last idle turn to the next deadline (a delayed send, a timeout, or the telemetry encoder's flush once records are staged); undefined for none. An input given since that turn has not run yet, so its deadlines are not in it.
spawn(chart: string | ChartRef, options?: SpawnOptions): Actor;
Starts an actor of chart, a chart's name (its current version) or a ChartRef (a version), and returns it. It runs in a later turn. Throws TA_ERROR_INVALID for a chart the host does not have.
actor(which: Session | Actor | string | Address): Actor;
The live actor which names: a session, an Actor, the name it was spawned with (newest segment first), or an address of this system (text or an Address). Throws StaleError for no live actor, InvalidError for another system's address.
actors(options?: { chart?: string }): Promise<Actor[]>;
Every live actor of the host (actorList, a page at a time), or the actors of the chart named options.chart.
send(target: Session | Actor | string | Address, event: string, data?: Value, options?: SendOptions): void;
send(message: SendMessage): void;
Puts event, with data, in the queue of the actor target names (a session or an address). Throws TA_ERROR_STALE for an actor that ended.
The same, from a SendMessage (the transport's send.message).
terminate(session: Session | Actor): void;
Ends the actor. Throws TA_ERROR_STALE for one that already ended.
idle(): Promise<void>;
Resolves at the next turn that finds nothing to do while no promise a handler returned is pending (an I/O processor's, a service's or its receive's, a wake, telemetry, log, migration or typed-method handler's): the same promise to every caller until then. A handler's promise that never settles keeps it pending, and so does a handler that awaits idle() itself. Rejects with the turn's first error (a handler's, a typed method's without onError), or with TA_ERROR_STATE when the host is closed first; on a closed host it returns a promise rejected so. It does not wait for promises your handlers start and do not return.
now(): number;
The host's clock in milliseconds, read now: a SimulatedClock's time, or the runtime's monotonic clock, on the scale the host's deadlines and snapshot() use, so a restored host's goes on from its snapshot's. A journal of inputs records it beside each input, and a replay on a restored host moves its clock to each reading. It reads and advances nothing; a closed host throws TA_ERROR_STATE.
actorState(params: SessionParams, onResult: OnResult<ActorStateResult>, onError?: OnError | null): void;
actorState(params: SessionParams): Promise<ActorStateResult>;
An actor's active states, { value, history }: a state id, or { parent: child } for compound states. Params { session }. const { value } = await host.actorState({ session }).
actorState given no handler: a promise of { value, history }.
actorContext(params: SessionParams, onResult: OnResult<ActorContextResult>, onError?: OnError | null): void;
actorContext(params: SessionParams): Promise<ActorContextResult>;
An actor's variables, { variables }. Params { session }; refused for a hibernated actor. A variable that holds undefined reads undefined, apart from null; before the actor's first turn every variable is undefined, its spawn params included.
actorContext given no handler: a promise of { variables }.
close(): void;
Frees the host: the last telemetry requests go to the handler, the timer is cleared, pending typed methods and running services are dropped without being called. From a handler it waits for the end of the turn. Again: nothing.
[Symbol.dispose](): void;
close().
[Symbol.asyncDispose](): Promise<void>;
close(), then the promises the telemetry handler returned at it.
Operating a host
static restore(snapshot: Uint8Array | ArrayBuffer, charts: Charts, options: HostOptions): Host;
A new host running a snapshot host.snapshot() gave, with the system it was taken with (checked) and the options new Host takes, its datamodel the snapshot's; of charts, the images the snapshot does not hold (compared by their bytes) are added after its own. Every session stays the same, and its clock goes on from the snapshot's time, whatever clock reads it. Invocations that were running are announced again to services, with invocation.restored true. A snapshot of a build whose layout differs throws TA_ERROR_INVALID.
readonly ticksPerSecond: number;
The host's clock rate, system.ticksPerSecond.
readonly quickjsHeapBytes: number | undefined;
The quickjs engine's heap in bytes, every actor's together, read on each access (cheap): what quickjs.memoryLimit bounds. undefined on a native host and once closed.
addImage(image: Uint8Array | ArrayBuffer): number;
Loads a compiled image, which it copies, and returns its segment, a number. A chart whose name the host already has gets a new version: new actors are of it, running ones stay on theirs until migrate. Throws TA_ERROR_INVALID for bytes that are not an image.
chart(name: string): ChartRef;
The current version of chart name, for spawn. Throws TA_ERROR_INVALID when the host has no chart of that name.
boot(segment: number): void;
Spawns the boot actors of the image of segment: a system manifest's <actor>s, or an actor of compileCharts' first chart.
actorList(params: ActorListParams | undefined, onResult: OnResult<ActorListResult>, onError?: OnError | null): void;
actorList(params?: ActorListParams): Promise<ActorListResult>;
The host's sessions a page at a time, { sessions, cursor }, cursor null after the last page. Params { cursor?, limit? } (1 to 512, default 256).
actorList given no handler: a promise of a page, { sessions, cursor }.
actorGet(params: SessionParams, onResult: OnResult<ActorGetResult>, onError?: OnError | null): void;
actorGet(params: SessionParams): Promise<ActorGetResult>;
One actor, { session, segment, chart, parent, mailbox, invocations, timers, timersPending }. Params { session }.
actorGet given no handler: a promise of the actor.
hostStats(params: NoParams, onResult: OnResult<HostStatsResult>, onError?: OnError | null): void;
hostStats(params?: NoParams): Promise<HostStatsResult>;
The host's counters, { host, pools, charts }: pools has one entry per live segment with each pool's capacity, used and highWater. No params: await host.hostStats().
hostStats given no handler: a promise of the counters.
hostConfig(params: NoParams, onResult: OnResult<EffectiveHostConfig>, onError?: OnError | null): void;
hostConfig(params?: NoParams): Promise<EffectiveHostConfig>;
The config the host runs with, every default filled in. No params.
hostConfig given no handler: a promise of the effective config.
traceWatch(params: TraceWatchParams, onResult: OnResult<TraceWatchResult>, onError?: OnError | null): void;
traceWatch(params: TraceWatchParams): Promise<TraceWatchResult>;
Adds trace rules for a while: { rules, seconds } (default 900); answers { id }.
traceWatch given no handler: a promise of { id }.
traceUnwatch(params: TraceUnwatchParams, onResult: OnResult<EmptyResult>, onError?: OnError | null): void;
traceUnwatch(params: TraceUnwatchParams): Promise<EmptyResult>;
Ends a watch early: { id }.
traceUnwatch given no handler: a promise of {}.
traceEnd(params: NoParams, onResult: OnResult<EmptyResult>, onError?: OnError | null): void;
traceEnd(params?: NoParams): Promise<EmptyResult>;
Ends every open span now, marked incomplete, for the telemetry handler. Refused without telemetry in the config.
traceEnd given no handler: a promise of {}.
traceSpansDecode(params: TraceSpansDecodeParams, onResult: OnResult<TraceSpansDecodeResult>, onError?: OnError | null): void;
traceSpansDecode(params: TraceSpansDecodeParams): Promise<TraceSpansDecodeResult>;
The spans of a traces request: { traces } (the bytes) answers { spans }, each with trace, span, parent, name, start, end, attributes, ...
traceSpansDecode given no handler: a promise of { spans }.
metrics(): Uint8Array;
The host's counters now as an OTLP metrics request (a Uint8Array, for a collector's /v1/metrics). TA_ERROR_STATE without telemetry in the config.
sweep(maxScan: number, limit: number): bigint[];
Finds cold actors: at most limit sessions that did not run since the previous sweep and can be hibernated now, looking at most at maxScan actors (each an integer from 0 to 2^32-1). The first sweep after an actor ran skips it.
hibernate(session: Session | Actor): Uint8Array;
Takes the actor out of memory and returns its blob (a Uint8Array) for your store. TA_ERROR_STATE when it is not at rest (events waiting, a step not over, running an invoked child or service) and when it is not resident (already hibernated, its wake asked for and not given): the status does not tell the two apart. TA_ERROR_STALE for a session that ended or never was, TA_ERROR_UNSUPPORTED when a variable holds what the datamodel cannot store (the message names it); it stays where it is then.
wake(session: Session | Actor, blob: Uint8Array | ArrayBuffer): void;
Puts a hibernated actor back from the blob hibernate gave; its waiting events run at the next turn. Only its latest blob: one of an earlier hibernation (a store that fell behind) throws TA_ERROR_STALE, another session's TA_ERROR_INVALID, and the actor stays hibernated. On a restored host a blob of a hibernation made after the snapshot is taken too.
migrate(options?: MigrateOptions): number;
Starts moving running actors (every chart's, chart's, or one session) to the current version of their chart, in later turns; each gets error.platform.upgrade with its old variables. Returns the migration's id, which migrated and migrationDone name. Throws TA_ERROR_INVALID for a chart the host does not have and TA_ERROR_STALE for a session that ended.
snapshot(): Uint8Array;
The whole host as one Uint8Array: every segment, the images, the system id, the host's time (its clocks advanced first: the time of the call, as now() reads it) and the datamodel's state, for Host.restore on the same build of the package. Only when quiescent: from a handler, with an effect or a reply pending, or during a migration it throws TA_ERROR_STATE; a value outside the value table throws TA_ERROR_UNSUPPORTED.
Wire format
fail(sender: Session | Actor | string | Address, sendid?: string): void;
Tells a sender that its send could not be delivered: the actor sender (a session or its address, the send's origin) gets error.communication with sendid.
sendMany(items: readonly SendMessage[]): number;
Sends each of items in order, and returns how many were taken: the rest, from the first one refused, were not sent.