queue-tea
v1.0.5
Published
A simple, robust, persistable job & task queue written in typescript. Full type safety included.
Downloads
3
Readme
Cute & simple, robust, persistable job & task queue written in typescript. Full type safety included. ✌🏼
import createQueue from 'queue-tea'
interface Tasks {
syncDataWithCloud: { username: string; count: number }
runInBackground: undefined
}
const queue = createQueue<Tasks>({
tasks: {
syncDataWithCloud: async ({ username, count }) => {
fetch('http://example.com/api/', {
body: JSON.stringify({ username, count }),
})
},
runInBackground: async (_options, { createdAt, retries }) => {
// This is a fun task, that fails 3 times, than succeeds
if (retries < 2) {
throw new Error('Not this time')
}
return
},
},
})
queue.queueTask('syncDataWithCloud', { username: 'rainbow cat', count: 69 })
queue.queueTask('runInBackground')
queue.run()
Installation
You know the drill 👏
yarn add queue-tea
### or for the npm fans
npm install --save queue-tea
Introduction
queue-tea
is meant to be a local queue for Javascript applications running for React Native, Electron or the browser.
We use it to ensure some tasks that should be performed in the background, can fail and can be retried. It's also persistable by serializing it and providing the state as initialState
.
How it works
Options
| Option | Value | Default value |
| ------------ | -------------------------------------------------------------------------------- | ------------- |
| initialState | Task[]
| []
|
| onChange | onChange?: (queue: QueueType<G>) => Promise<void>;
| undefined
|
| retryDelay | (retries: number) => number
| defaultBackOff
|
Retrying
The queue relies on exceptions. When a tasks fails, it should throw an exception. It then will be retried.
Info: By default, the task will be retried every
1 second * retries + $noise
with a maximum of 5 seconds. You can change this by providing your own function to theretryDelay
option.