A typed task queue with a simple API
npm install @exogee/kewA typed task queue with a simple API.
---
- Create one or more _Queues_.
- Attach _Platforms_ for storage and data persistence.
- Register _Actions_ to perform various types of tasks.
---
- Simple objects with lifecycle methods.
- Can contain reducer functions to report queue data.
---
- Individual instance of an action with its data
- Can store serializable data that persists across app restarts.
---
- See @exogee/kew-example
Convenience method to create a new task queue with the provided plugins and handlers.
---
Add a new task of type key to the queue with optional data
---
Add a new task of type key to the queue with optional data
---
Start the queue.
---
Stop the queue.
---
Run a named reducer over the queue
Optionally, an initialValue can be provided which will be the initial value of the accumulator.
Optionally, a filter function can be provided which will only apply the reducer if the filter returns true for a task.
Returns a promise with the reduced data.
---
Register a plugin- can also be registered in createTaskQueue
---
Register one or more task handlers- can also be registered in createTaskQueue
---
Subscribe to a task queue event.
Returns a subscription object with a remove() method to unsubscribe.
---
Get a list of all tasks.
---
Remove all tasks from the queue
---
``ts
const AsyncStoragePlugin: QueuePlugin = {
storage: {
// load is called when the queue data needs to be reloaded.
async load() {
return await AsyncStorage.getItem("TASK_QUEUE")
.then((tasksJSON) => callback(JSON.parse(tasksJSON)))
.catch(() => ({}));
},
// sync is called with the new queue data whenever the queue is updated.
async sync(queue: QueueStorageData) {
await AsyncStorage.setItem("TASK_QUEUE", JSON.stringify(queue));
},
},
};
export default AsyncStoragePlugin;
`
---
Update the task data
---
- Tasks can implement any number of named reducers.
- When TaskQueue.reduce(reducerName)` is called, each queue task's reducer will be called in order where available-
with the accumulated result as the first parameter, and the new task data as the second.
- An initial value can be provided to runTaskReducer as an optional second argument.