The one with basic React bindings
Task
management is hard.
Background tasks like fetching, syncing, and cache eviction are common in intelligent web applications. Yet managing them - with scheduling, failure handling, retries, and so on - can be a pain.
Specify your tasks imperatively, ahead of time, and then configure their schedules, timeouts, and retry sequences - and let TinyTick take care of everything for you. Oh and it's only 1.8kB.
Create and start a Manager
object.
This is the main entry point for the TinyTick API.
import {createManager} from 'tinytick';
const manager = createManager().start();
Register a Task
.
A TinyTick task is simply an asynchronous function that can take an optional string argument (and a few other things, as you'll see later!). Simply register it with a string Id
.
const ping = async (url) => await fetch(url);
manager.setTask('ping', ping);
By default, TinyTask schedules the task to start as soon as possible. And it will generate a unique Id
for each 'task run' so you can track its progress.
const taskRunId = manager.scheduleTaskRun(
'ping',
'https://example.com',
);
The Manager
object exposes plenty of accessors to let you inspect the tasks you have registered and the state of the task runs you've scheduled.
console.log(manager.getTaskIds());
// -> ['ping']
console.log(manager.getTaskRunInfo(taskRunId));
// -> {taskId: 'ping', arg: 'https://example.com', ...}
Tasks (or individual task runs) can have a timeout set, and they will be aborted if they run over. Task
functions are passed an AbortSignal parameter so you can handle the timeout. You can pass this straight on to the fetch call, for example.
manager.setTask(
'ping',
async (url, signal) => await fetch(url, {signal}),
undefined,
{maxDuration: 100}, // milliseconds
);
If a task run fails (for taking too long, or throwing an exception), you can indicate that you want it to retry, and even configure a backoff strategy.
manager.setTask(
'ping',
async (url, signal) => await fetch(url, {signal}),
undefined, // we'll explain this argument in a moment!
{maxRetries: 3, retryDelay: '1000, 5000, 10000'},
);
A Task
can be assigned a category, which can have its own configuration for duration, retries, and retry delays. But of course, individual properties can still be overridden per task or per task run.
manager.setCategory('network', {
maxDuration: 100,
maxRetries: 3,
retryDelay: '1000, 5000, 10000',
});
manager.setTask('ping', ping, 'network', {
maxRetries: 5,
});
We are building up a set of Example Use Cases guides to show you how to use TinyTick in practice. If you're trying to access relational- or graph-like data over a network, for example, take a look at the Paginated And Nested Data guide for a start!
manager.scheduleTaskRun('fetchParents');
// -> 'Fetching https://api.org/parents?page=1'
// -> 'Storing parent A'
// -> 'Storing parent B'
// -> 'Fetching https://api.org/children?parentId=A&page=1'
// -> 'Fetching https://api.org/children?parentId=B&page=1'
// -> 'Fetching https://api.org/parents?page=2'
If you chose to install TinyTick in your app, you'll only add a gzipped 1.8kB to your app. Life is easy when you have zero dependencies!
TinyBase has 100.0% test coverage, including the code throughout the documentation - even on this page. The guides, demos, and API examples are designed to make things as easy as possible.
Total | Tested | Coverage | |
---|---|---|---|
Lines | 225 | 225 | 100.0% |
Statements | 257 | 257 | 100.0% |
Functions | 96 | 96 | 100.0% |
Branches | 87 | 87 | 100.0% |
Tests | 115 | ||
Assertions | 456 |
TinyTick is part of a group of small libraries designed to help make rich client and local-first apps easier to build. Check out the others!
TinyBase
A reactive data store and sync engine.
TinyWidgets
A collection of tiny, reusable, UI components.