Drop task
This is the way to create a droppable task.
This will cancel any new instances of the task. You can also provide a max
that will only drop the
task instances if the threshold is exceeded.
To specify a task as droppable, you can either use the dot notation or the options notation.
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task.drop(async () => { // your code });</script>
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task( async () => { // your code }, { kind: 'drop' }, );</script>
Max concurrency
Section titled “Max concurrency”This is how you can specify the maximum number of concurrent instances. The default is 1, here we’re setting it to 5.
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task.drop( async () => { // your code }, { max: 5 }, );</script>
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task( async () => { // your code }, { kind: 'drop', max: 5 }, );</script>
The task class
Section titled “The task class”The return value of the task function will be a svelte stateful class where you can access state
from all the instances running and eventually cancel them with cancelAll
.
Passing props
Section titled “Passing props”While defining a task, if the function that you pass in has some arguments, those will be required
by the perform
function (and it will be strongly typed too).
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task.drop(async (id: string) => { // your code });</script>
<button onclick={() => { dropTask.perform('42'); }}>perform</button>
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task( async (id: string) => { // your code }, { kind: 'drop' }, );</script>
<button onclick={() => { dropTask.perform('42'); }}>perform</button>
Getting the return value
Section titled “Getting the return value”If you return something from your task you can access the return value by awaiting the perform
function.
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task.drop(async () => { return 42; });</script>
<button onclick={() => { const number = await dropTask.perform(); console.log(number); // 42 }}>perform</button>
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task( async () => { return 42; }, { kind: 'drop' }, );</script>
<button onclick={() => { const number = await dropTask.perform(); console.log(number); // 42 }}>perform</button>
Getting the TaskInstance
Section titled “Getting the TaskInstance”If you don’t await the perform
function, then you’ll get back the
task instance that you can use either to cancel it or to get its current
state. The TaskInstance
is also a svelte stateful class and you can access the current value.
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task.drop(async () => { // your code });</script>
<button onclick={() => { const lastRun = dropTask.perform(); console.log(lastRun); // { isRunning: true, hasStarted: true, ... } lastRun.cancel(); }}>perform</button>
<script lang="ts"> import { task } from '@sheepdog/svelte';
const dropTask = task( async () => { // your code }, { kind: 'drop' }, );</script>
<button onclick={() => { lastRun = dropTask.perform(); console.log(lastRun); // { isRunning: true, hasStarted: true, ... } lastRun.cancel(); }}>perform</button>