Tasks
Run async work from a Task's arguments, show where it got to
through its state, stop it when its arguments change or its item
goes, and have the checker type it from its run.
After reading this page, you will be able to declare a
Task that runs async work when its arguments change, show where it got to, run it when a button is pressed, and know what stops it.A
Task, from Ljos.Core, holds one job: a run function, the args it is called with, and where the last run got to. When an element of args changes, the Task runs again. Its state says where the run is, and a binding that reads it follows it:run takes the elements of args, then an AbortSignal. It may be async or plain: what it gives is the Task's value, and what it throws is its error. The Task starts its first run once it is complete.state is one of four, told apart by status:A
switch on state.status (or an if that compares it) narrows state, so in the "done" case state.value is a string, with no undefined to test for. A pending or failed state keeps the last run's value, so a page can keep showing the old forecast while the new one loads. match takes state apart the same way: see Pattern matching.status, value (the value or undefined) and error are the same parts, read one at a time, for a binding that wants only one: visible: weather.status === "pending". The four change together, and each notifies once a step, so a binding that reads two of them is evaluated once.When
args changes, element by element with !==, the Task aborts the run in progress through its signal and starts another. What a superseded run gives is dropped, even when it comes back after the new one starts, so a slow reply for Oslo never shows under Bergen. Give the signal to fetch and the superseded request stops; without it the request runs on, and only its result is dropped. args made again with the same elements runs nothing.When the item the Task is declared in goes (a
Loader unloads it, or a delegate is taken out of its view), the run in progress is aborted. A Task is a Controller of that item, so a delegate scrolled out of a list stops its request.A Task waiting on its run costs nothing: no timer and no frame. The reply wakes the application, which sets the Task's state and the bindings that read it, and sleeps again.
With
autoRun: false, a Task runs only when start() is called: with args' own, or, given arguments, with those: every one before the signal, optional ones too. abort() stops the run in progress, and the Task fails at once with the signal's reason, an AbortError. The signals done(value) and failed(error) are for what a binding cannot say, such as adding a row:A reply that is not
ok and a request that could not be made both fail the Task, and the page going aborts it. error is unknown, as a catch binding is: what a run throws need not be an Error, so narrow it before reading its message.The checker types a Task from its
run. args, written as an array, is held to the run's parameters before its signal, in number and element by element, as is what start(…) is given, which is none of them or all of them; value and done carry what the run's promise gives. A Task whose run takes a name: string and whose args is [root.count], an int, is error: `name` is a string; this gives it a number [ts-assign-type], and reading weather.value into a string property is error: `last` is a string; this gives it a string or undefined [ts-assign-type]. ljos check --tsc gives TypeScript the same Task.Write the run's return type, as the examples do: a run that returns what a function of the file gives, with no type written, gives
unknown.Apps built for the web have
Task, and Controller, with the same meaning. A run that fetches uses the browser's fetch, so a request to another origin needs that server's leave, as Fetching from the network says.


