Skip to main content

@math.gl/tasks

@math.gl/tasks provides generic task infrastructure for browsers and Node.js. It defines the task contract and provides a runner for local or worker execution. The runner manages worker startup, messages, and cleanup.

Applications and other math.gl packages import @math.gl/tasks to define and run their own operations. Each package owns its task implementations. The task runtime has no dependencies on other math.gl packages.

Package entry pointsโ€‹

@math.gl/tasks exposes the task API through its root import.

ExportUse it to
TaskRunner and InitializedTaskRun tasks and manage reusable sessions.
preloadTasksPrepare tasks before first use.
Task, TaskTypes, and TaskContextDefine a calculation and its input, result, and progress types.
createTypedTaskDispatcherRoute typed operations through a persistent task.
initializeTaskWorkerStart a custom task implementation inside a worker.

Import ready-made tasks from their owning package. For example, @math.gl/geospatial/tasks exports ProjectCoordinatesTask.

Run a taskโ€‹

This example combines the generic runner with a projection task implemented by @math.gl/geospatial.

import {TaskRunner} from '@math.gl/tasks';
import {ProjectCoordinatesTask} from '@math.gl/geospatial/tasks';

const runner = new TaskRunner();
try {
const {coordinates} = await runner.runTask(ProjectCoordinatesTask, {
coordinates: new Float64Array([0, 0, 10, 20]),
projection: {from: 'EPSG:4326', to: 'EPSG:3857'}
});
console.log(coordinates);
} finally {
await runner.close();
}

ProjectCoordinatesTask includes a worker implementation. You send coordinates and a projection, then await the converted coordinates. Inputs are copied by default, so your application can keep using the original arrays.

Start with the getting-started guide for setup and examples, or write a task for your own computation.

Featuresโ€‹

FeatureSupportDescription
Browser and Node.js workersโœ…Move computations off the main thread
Typed inputs and resultsโœ…Describe a task with ordinary TypeScript objects
Multiple inputs and streamsโœ…Process a dataset in manageable pieces
Reusable setupโœ…Keep a prepared projection, index, or other state for later calls
Cancellation and progressโœ…Stop worker work and report progress to your application
Buffer transfer and shared memoryโœ…Choose how large arrays move between threads
Local executionโœ…Run small operations on the calling thread when a worker would add overhead

Workers are useful when computation would otherwise block your application. Small operations may run faster locally because starting a worker and sending its data takes time. See choosing where tasks run.

Learn moreโ€‹