queue-nano-task
v1.0.0
Published
Nano-Task management library
Maintainers
Readme
Overview
queue-nano-task is a lightweight JavaScript library designed to manage and schedule nanotasks efficiently.
Inspired by queueMicrotask, it provides a controlled, internal flow of tasks that allows fine-grained task execution management.
Installation
Using npm:
npm i queue-nano-taskUsing yarn:
yarn add queue-nano-taskGetting Started
The main queueNanotask function accepts one required parameter and two optional:
- The first parameter is a task represented by a function that will be executed within the task queue.
- The second (optional) parameter sets the task priority, with a default value of 0.
- The third (optional) parameter determines the queue order, where LIFO is used if true; otherwise, FIFO by default.
If queueNanotask is executed outside the context of another queueNanotask call, the code is executed immediately during the call.
import { queueNanotask } from 'queue-nano-task'
const log: any[] = []
queueNanotask(() => log.push(42))
// log: [42]Task Priority
Control the execution priority of Nano-Tasks with the second and the third optional argument.
- The third argument, when
true, switches the order of the queue from the default FIFO to LIFO. - Lower priority values are executed before higher priority values.
- Tasks with the same priority are ordered based on FIFO or LIFO, depending on the third parameter.
import { queueNanotask, Task } from 'queue-nano-task'
const log: any[] = []
const logger = (value: any): Task => () => {
log.push(value)
}
queueNanotask(() => {
queueNanotask(logger('Mounted'), 2)
queueNanotask(logger('Mounting'), 1)
queueNanotask(logger('Rendering'), 0)
queueNanotask(logger('WillMount'), 1, true)
})
// log: ['Rendering', 'WillMount', 'Mounting', 'Mounted']In the example, 'Rendering' (priority 0) executes first, followed by 'WillMount' (priority 1, LIFO), then 'Mounting' (priority 1, FIFO), and finally 'Mounted' (priority 2).
Runs from the left to the right
[true, ..., false] > [true, ..., false] > ...^ 0 ^ ^ 1 ^
Issues & Contributions
If you find bugs or want to suggest features, please open an issue on GitHub.
