Usage
The simplest way to use @sheepdog/svelte
is the following
This will run your async function when the user presses the button. “But why not use a normal async function?” I hear you ask.
Because the myTask
variable that you used to call perform
is also a svelte store that contains
a lot of useful information about your task!
Task
structure
isRunning
: whether the task is currently running or notlast
: the last task instance, regardless of whether it errored, was canceled, or was successfullastCanceled
: the last canceled task instancelastErrored
: the last errored task instancelastRunning
: the last running task instance, as soon as the task stops running, this will be undefinedlastSuccessful
: the last successful task instanceperformCount
: the number of times the task has been run,
TaskInstance
structure
To make it easier to reason about, every type of task modifier utilizes the same underlying structure. You can find more detail about the structure of the TaskInstance
in the reference docs.
Task modifiers
@sheepdog/svelte
allows you to specify a task modifier that changes the behavior of the perform
function. By default when you call perform
the task will be executed immediately regardless if
there are other instances of the same task in execution. For example, you could use the enqueue
modifier to instruct @sheepdog/svelte
to execute the task instances one after another (in a
queue - who would’ve thought)
Here’s how you can specify the modifier:
There are currently five task modifiers that you can apply to your task.
default
: the default behavior, every task will run immediatelydrop
: if other tasks are already running the new instance will be immediately canceledenqueue
: every task instance is stored in a queue and executed with a FIFO strategykeepLatest
: like drop but the last instance will actually be kept around and executed as soon as the current running instance finishesrestart
: the oldest running task instance is canceled and the new one will start running immediately