A typed task queue with a simple API.
- Create a Queue
- Attach a Platform for storage and data persistence.
- Register one or more Action\ s to perform various types of tasks.
- Actions are simple objects with lifecycle methods.
- Add Task\ s to the Queue.
// index.ts
import { Queue, TaskStatus, Action } from "@exogee/kew";
import { Platform } from "@exogee/kew-react-native-async-storage";
interface ReverseMessageProps {
value: string;
}
@Metadata("ReverseMessage")
class ReverseMessage extends Action<ReverseMessageProps> {
@Start()
async start() {
this.props.value = this.props.value.split("").reverse().join("");
}
}
// Create a new queue
const queue = new Queue({
platform: new Platform("my_application_queue"),
actions: [ReverseMessage],
});
// Register an event listener
queue.on(
(task) => task.status === TaskStatus.FINISHED,
({ props }) => {
console.log("Task completed: ", props.value);
queue.stop();
}
);
// Add task to the queue and start
(async () => {
await queue.add("ReverseMessage", { value: "!olleH" });
await queue.start();
})();
See @exogee/kew-example
for a more complex example
Create a new queue with optional platform and Actions
Run the action actionKey
with optional props
and return the result immediately, without adding to the queue.
Add a new task of action type actionKey
to the queue with optional props
- returns a unique task ID.
Start the queue.
Stop the queue.
Pause the queue
Unpause the queue
Run the reducer named reducerKey
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 result.
Register one or more actions if not registered in the constructor.
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
const AsyncStoragePlatform: Platform = {
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 AsyncStoragePlatform;