KeepLatest task
This is the way to create a keepLatest task.
This will run the initial tasks and then, if over the max
concurrency will keep the very last
instance in a que. You can also provide a max
that will dictate the number of task instances that
will run initially.
To specify a task as keepLatest, you can either use the dot notation or the options notation.
import { task } from '@sheepdog/vanilla';
const latestTask = task.keepLatest(async () => { // your code});
import { task } from '@sheepdog/vanilla';
const latestTask = task( async () => { // your code }, { kind: 'keepLatest' },);
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.
import { task } from '@sheepdog/vanilla';
const latestTask = task.keepLatest( async () => { // your code }, { max: 5 },);
import { task } from '@sheepdog/vanilla';
const latestTask = task( async () => { // your code }, { kind: 'keepLatest', max: 5 },);
The task
Section titled “The task”The return value of the task function will be an object with getters 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).
import { task } from '@sheepdog/vanilla';
const latestTask = task.keepLatest(async (id: string) => { // your code});
button.addEventListener('click', () => { latestTask.perform('42');});
import { task } from '@sheepdog/vanilla';
const latestTask = task( async (id: string) => { // your code }, { kind: 'keepLatest' },);
button.addEventListener('click', () => { latestTask.perform('42');});
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.
import { task } from '@sheepdog/vanilla';
const latestTask = task.keepLatest(async () => { return 42;});
button.addEventListener('click', () => { const number = await latestTask.perform(); console.log(number); // 42});
import { task } from '@sheepdog/vanilla';
const latestTask = task( async () => { return 42; }, { kind: 'keepLatest' },);
button.addEventListener('click', () => { const number = await latestTask.perform(); console.log(number); // 42});
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 an object with getters you can access the current value
with instance.value
or register events on it with instance.on
.
import { task } from '@sheepdog/vanilla';
const latestTask = task.keepLatest(async () => { // your code});
button.addEventListener('click', () => { const lastRun = latestTask.perform(); lastRun.on('success', () => { console.log(lastRun.value); }); lastRun.cancel();});
import { task } from '@sheepdog/vanilla';
const latestTask = task( async () => { // your code }, { kind: 'keepLatest' },);
button.addEventListener('click', () => { lastRun = latestTask.perform(); lastRun.on('success', () => { console.log(lastRun.value); }); lastRun.cancel();});