Documentation / @hybrid-compute/local / LocalCompute
Class: LocalCompute
Defined in: index.ts:47
LocalCompute is a compute backend that runs tasks on the calling thread: the main thread in a page, or whichever worker or Node.js thread creates it.
It keeps the registered tasks in memory and runs them by name. A task's synchronous work runs before runTask returns, so it suits small, cheap tasks; heavy work belongs in the worker backend.
Example
const local = new LocalCompute();
local.registerTask({
name: 'square',
run: (n: number) => n * n
});
const result = await local.runTask<number, number>('square', 5);
console.log(result); // 25Implements
Constructors
Constructor
new LocalCompute(): LocalCompute;Returns
LocalCompute
Methods
canRun()
canRun(taskName: string): boolean;Defined in: index.ts:114
Determines whether the backend has a task by this name.
Parameters
taskName
string
The task identifier.
Returns
boolean
true if the task is registered, otherwise false.
Implementation of
ComputeBackendInterface.canRun
registerTask()
registerTask<Input, Output>(task: ComputeTaskInterface<Input, Output>): void;Defined in: index.ts:65
Registers a task with this backend. The task is copied, so changing the object afterwards has no effect.
Type Parameters
Input
Input
The input type expected by the task.
Output
Output
The output type returned by the task.
Parameters
task
ComputeTaskInterface<Input, Output>
A task with a unique, non-empty name and a run function, which may return the output or a Promise of it.
Returns
void
Throws
If task isn't an object, its name isn't a non-empty string, or it has no run function.
Throws
If a task with the same name is already registered. Call LocalCompute.unregisterTask first to replace it.
runTask()
runTask<Input, Output>(
taskName: string,
input: Input,
options?: RunTaskOptionsInterface): Promise<Output>;Defined in: index.ts:139
Runs a registered task with the given input.
Type Parameters
Input
Input
The expected input type of the task.
Output
Output
The expected output type of the task.
Parameters
taskName
string
The name of the task to run.
input
Input
The input data to pass to the task.
options?
Options for this call. With a signal, the task gets the signal as run(input, { signal }), and runTask rejects with the signal's reason as soon as it aborts. An async task keeps running unless it stops itself; synchronous work can't be interrupted.
Returns
Promise<Output>
A Promise resolving to the task's output. It rejects with a NoBackendError (from @hybrid-compute/core) if no task has that name, with the signal's reason if it's aborted, and with whatever the task throws or rejects with.
Example
const result = await local.runTask('square', 4); // 16Implementation of
ComputeBackendInterface.runTask
unregisterTask()
unregisterTask(taskName: string): boolean;Defined in: index.ts:104
Removes a registered task.
Parameters
taskName
string
The name of the task to remove.
Returns
boolean
true if a task was removed, false if none had that name.