memoq
v1.2.0
Published
A memoized function queue
Readme
memoq
A simple task queue that stores the output of each task and associates it with an id. Future tasks can retrieve old task results and perform operations on it. Additionally, the NodeJS process executing the task queue can be killed and restarted where it left off (assuming cache files have persisted and user-instantiated queue has not been modified).
Installation
# npm
npm i memoq@latest
#pnpm
pnpm add memoq@latestUsage
import { Queue, Task } from 'memoq';
const queue = new Queue();
queue.add(new Task(
`root`, // ID of task
[], // Depends on data from no other tasks
async (data) => { // `data` is empty object
return 3;
}
));
queue.add(new Task(
`step1`, // ID of task
[], // Depends on data from no other tasks
async (data) => { // `data` is empty object
return 4;
}
));
queue.add(new Task(
`step2`, // ID of task
[`root`, `step1`], // Depends on data from task IDs `root` and `step1`
async (data) => { // `data` contains cached results
const dr = data['root']; // Return value of `root` task
const ds1 = data['step1']; // Return value of `step1` task
const val = dr + ds1;
console.log(val); // Outputs 7
return val;
}
));
await queue.start(); // Start the queueReference
class Queue
public new Queue()
Creates a new instance of Queue
public add(task: Task): void
Adds a
Taskinstance to the queue
task- theTaskto add
public async start(): void
Starts executing tasks in the queue, and recalls prior state from the cache if present
class Task
public new Task(id: string, dependsOn: string[], fn: TaskFunction, delay: Returnable<number>)
Creates a new instance of Task
id- ID of the new taskdependsOn- IDs of the data to import into the new taskfn- the actual function to execute (parameterdatagiven)delay- the delay, in ms, before this task begins executing (default 0)- This is of type
Returnable<number>, which means it can either by of typenumberor a function that returns a value of typenumber
- This is of type
Changelog
1.2.0
Improvements
Queue- added task duration in logging
Bugs
- N/A
1.1.0
Improvements
Task-delayparameter in constructor modifiedREADME- Changelog added
Bugs
- N/A
1.0.1
Improvements
README.mdaddedLICENSEadded
Bugs
- N/A
1.0.0
Improvements
- Package created
Bugs
- N/A
