JSPM

queue-nano-task

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q43211F
  • License MIT

Nano-Task management library

Package Exports

  • queue-nano-task
  • queue-nano-task/index.es6.js
  • queue-nano-task/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (queue-nano-task) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.

stars watchers

Installation

Using npm:

npm i queue-nano-task

Using yarn:

yarn add queue-nano-task

Getting 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.

issues