CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-queue

Asynchronous function queue with adjustable concurrency control and Promise/callback support

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

array-operations.mddocs/

Array Operations

Familiar Array methods for adding, removing, and manipulating jobs in the queue. The Queue class implements most of the Array API for intuitive job management.

Capabilities

Add Jobs to Queue

Push Jobs

Adds one or more jobs to the end of the queue.

/**
 * Adds jobs to the end of the queue
 * @param workers - Jobs to add to the queue
 * @returns New length of the queue
 */
push(...workers: QueueWorker[]): number;

Usage Examples:

// Add single job
q.push((callback) => {
  setTimeout(() => callback(null, 'done'), 1000);
});

// Add multiple jobs
q.push(
  (cb) => cb(null, 'job 1'),
  (cb) => cb(null, 'job 2'),
  () => Promise.resolve('job 3')
);

// With Promise-based job
q.push(async () => {
  const result = await fetch('/api/data');
  return result.json();
});

Unshift Jobs

Adds one or more jobs to the front of the queue (they'll execute first).

/**
 * Adds jobs to the front of the queue
 * @param workers - Jobs to insert at the beginning
 * @returns New length of the queue
 */
unshift(...workers: QueueWorker[]): number;

Usage Example:

// Add high-priority job to front
q.unshift((callback) => {
  console.log('High priority job');
  callback();
});

Splice Jobs

Adds and/or removes jobs at a specific index.

/**
 * Changes queue contents by removing existing jobs and/or adding new jobs
 * @param start - Index to start changing the queue
 * @param deleteCount - Number of jobs to remove
 * @param workers - Jobs to add at the start index
 * @returns Queue instance for chaining
 */
splice(start: number, deleteCount?: number, ...workers: QueueWorker[]): Queue;

Usage Examples:

// Insert job at position 2
q.splice(2, 0, (cb) => cb(null, 'inserted job'));

// Replace job at position 1
q.splice(1, 1, (cb) => cb(null, 'replacement job'));

// Remove 2 jobs starting at position 3
q.splice(3, 2);

Remove Jobs from Queue

Pop Jobs

Removes and returns the last job from the queue.

/**
 * Removes and returns the last job from the queue
 * @returns The removed job, or undefined if queue is empty
 */
pop(): QueueWorker | undefined;

Usage Example:

const lastJob = q.pop();
if (lastJob) {
  console.log('Removed last job');
}

Shift Jobs

Removes and returns the first job from the queue.

/**
 * Removes and returns the first job from the queue
 * @returns The removed job, or undefined if queue is empty
 */
shift(): QueueWorker | undefined;

Usage Example:

const firstJob = q.shift();
if (firstJob) {
  console.log('Removed first job');
}

Search and Manipulate Jobs

Find Job Index

Finds the index of a specific job in the queue.

/**
 * Returns the first index of a job in the queue
 * @param searchElement - Job to locate
 * @param fromIndex - Index to start search from
 * @returns Index of the job, or -1 if not found
 */
indexOf(searchElement: QueueWorker, fromIndex?: number): number;

/**
 * Returns the last index of a job in the queue
 * @param searchElement - Job to locate  
 * @param fromIndex - Index to start search from (backwards)
 * @returns Index of the job, or -1 if not found
 */
lastIndexOf(searchElement: QueueWorker, fromIndex?: number): number;

Usage Examples:

const myJob = (cb) => cb(null, 'my job');
q.push(myJob);

// Find the job
const index = q.indexOf(myJob);
console.log(`Job is at index ${index}`);

// Search from specific position
const laterIndex = q.indexOf(myJob, 5);

Extract Queue Section

Extracts a section of the queue and modifies the original queue.

/**
 * Extracts a section of the queue (modifies original queue)
 * @param start - Beginning index of extraction
 * @param end - End index of extraction (exclusive)
 * @returns Queue instance for chaining
 */
slice(start?: number, end?: number): Queue;

Usage Example:

// Keep only jobs 2-5
q.slice(2, 5);

// Keep jobs from index 3 onwards
q.slice(3);

Reverse Job Order

Reverses the order of jobs in the queue.

/**
 * Reverses the order of jobs in the queue
 * @returns Queue instance for chaining
 */
reverse(): Queue;

Usage Example:

// Reverse job execution order
q.reverse();

Job Types

/**
 * A job function that can accept a callback or return a Promise
 */
interface QueueWorker {
  (callback?: QueueWorkerCallback): void | Promise<any>;
  /** Override queue timeout for this specific job */
  timeout?: number;
  /** Promise reference if job returns a Promise */
  promise?: Promise<any>;
}

/**
 * Callback function for async jobs
 */
interface QueueWorkerCallback {
  (error?: Error, data?: Object): void;
}

Job Examples:

// Callback-based job
function callbackJob(callback) {
  setTimeout(() => {
    callback(null, 'callback result');
  }, 1000);
}

// Promise-based job
function promiseJob() {
  return new Promise((resolve) => {
    setTimeout(() => resolve('promise result'), 1000);
  });
}

// Job with custom timeout
function slowJob(callback) {
  setTimeout(() => callback(null, 'slow result'), 5000);
}
slowJob.timeout = 6000; // Override queue timeout

// Mixed jobs
q.push(callbackJob, promiseJob, slowJob);

Install with Tessl CLI

npx tessl i tessl/npm-queue

docs

array-operations.md

event-system.md

index.md

queue-management.md

tile.json