Appearance
Browser workers
Use new Simulation({ graph, facilities, scenario, execution: 'worker' }) to run off the UI thread. Local and worker execution share run() and produce the same results. The main package is safe to import in Node; browser globals are used only when starting a browser worker.
Bundle the worker
For Vite, import the public worker asset with ?worker and include vite/client in TypeScript types. Other bundlers must emit a module worker and provide a factory.
ts
import { Scenario, Simulation, type SimulationInput } from '@grabm/abm'
import SimulationWorker from '@grabm/abm/worker-entry?worker'
// Vite emits the public worker entry as a separate module-worker asset.
export function createWorkerSimulation(input: SimulationInput) {
return new Simulation({
graph: input.graph,
facilities: input.facilities,
scenario: new Scenario(input.scenario),
execution: 'worker',
createWorker: () => new SimulationWorker(),
})
}
export async function simulateInWorker(input: SimulationInput, signal?: AbortSignal) {
const simulation = createWorkerSimulation(input)
try {
return await simulation.run({
...input.run,
...(signal ? { signal } : {}),
onProgress: (progress) => console.log(progress.phaseLabel, progress.progress),
})
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
console.log('Simulation cancelled.')
}
throw error // The host handles cancellation and displays other failures.
} finally {
simulation.dispose()
}
}The default factory uses a URL relative to the packaged worker client. The explicit Vite factory above is covered by production-build consumer tests. Never import the worker entry as main-thread application code: it installs a worker message handler.
Run, cancel, and clean up
Pass an AbortController's signal to run(). Call abort() to cancel. The promise rejects with an error named AbortError; handle that separately from execution failures. A progress callback receives a phase label and fractions from zero to one. Throwing from that callback fails the run.
Each run owns a fresh worker. Completion, failure, and cancellation terminate it and remove handlers. Workers are created lazily. dispose() cancels active work, prevents new runs, and is safe to call repeatedly. Call it when the host unmounts.
Only one run may be active per simulation. A second call rejects without cancelling the first. Use separate simulation instances for independent concurrent runs. Local execution cancels cooperatively between phases and event batches. Worker cancellation terminates the worker immediately; neither mode returns a partial result.
The browser fixture verifies the emitted worker asset, completion, cancellation, failures, local/worker parity, playback, and cleanup.
Several independent runs
Use the batch API for a worker count or conservative auto setting. Use Node dataset preparation to keep large execution/results on a backend.
Event processing reports progress and yields after 2,048 events or when at least 250 milliseconds have elapsed since the last event yield, whichever is reached first at an event boundary. This also covers small regional runs with expensive routes. It does not interrupt an individual synchronous routing operation; one event may still take longer than this interval. Wall-clock timing controls only reporting and yielding, never event order, random draws, or simulated time.