A minimal in-process queue system for TypeScript/JavaScript.
npm install @moznion/miniqA minimal in-process queue system for TypeScript/JavaScript that supports TTL mechanism.
``ts
let i = 0;
const queueTaskRunner = new QueueTaskRunner
const task1 = await Task.make
return new Promise
setTimeout(() => {
resolve(++i);
}, 500);
});
});
const task2 = await Task.make
return new Promise
resolve(++i);
});
});
const task3 = await Task.make
return new Promise
resolve(++i);
});
});
queueTaskRunner.enqueue(task1); // runner runs task1, and the task takes 500ms
queueTaskRunner.enqueue(task2); // runner queues task2 and defer it after task1
queueTaskRunner.enqueue(task3); // runner queues task3 and defer it after task2
console.log(await task1.getResult()); // => 1
console.log(await task2.getResult()); // => 2
console.log(await task3.getResult()); // => 3
`
QueueTaskRunner#enqueue(task: Task receives a task and appends that task to the queue and runs the tasks in a queue
from the beginning of the queue.
Task represents the task that you wanted to do according to the FIFO, and the task runner executes the enqueuedasync do(): Promise
task's .do()
The point is this returns the Promise so the client code cannot get the result of the actual procedure,async Task#getResult(): Promise
but you can use to take that.await getResult()
So the basic usage should be enqueuing a task and waiting for the result by for the task.
Please see also typedoc.
You can give a task the TTL like:
`ts
const task = await Task.make
return 'foo';
}, Date.now() + 60000);
// ~~~~~~~~~~~~~~~~~~ TTL that represents 60 secs from now
...
await task.getResult(); // if the TTL has exceeded, it rejects this task and throws TaskTTLExceededError.`
And a task that has the TTL has been enqueued and when the task runner takes the task to run, if the TTL exceeds
the current timestamp at that moment, it doesn't run that task and it rejects the task with TaskTTLExceededError`.
MIT