1.0.2 • Published 2 years ago

@adrien.pgd/nodejsutilities v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Table of contents

All Classes

Classesverison
Queuev1.1
Tasksv1.0

Quick Start

import { Queue } from '@adrien.pgd/nodejsutilities'

const my_queue = new Queue();

/*...*/

Or

import utilities from '@adrien.pgd/nodejsutilities'

const my_queue = new utilities.Queue();

/*...*/

Details

Queue

Basic implementation of a queue.

Classverisonevent emitterunit tested
Queuev1.1yesyes

Methods

namedescriptionError (thow an error)
enqueueAdd an item at the end of the queueif the item do not match the format checker
dequeueRemove the first item and return itif the queue is empty
peekreturn the first item of the queueif the queue is empty
lengthReturn the length of the queue
is_emptyreturn if the queue is empty or not

emitter

signaldatadescription
enqueue(new_item)Sent when an element is enqueue
dequeue(deleted_item)Sent when an element is dequeue

Example

import { Queue } from '@adrien.pgd/nodejsutilities'

const my_queue = new Queue();

my_queue.on("enqueue", new_item => {
    console.log(`New element added at the end of the queue. Element(${new_item})`)
})

my_queue.on("dequeue", deleted_item => {
    console.log(`First element of the queue have been removed. Element(${deleted_item})`)
})

my_queue.enqueue(5); // my_queue = {5}
const value = my_queue.peek(); // value = 5
const value2 = my_queue.dequeue(); // value2 = 5; my_queue = {}

Tasks

Implementation of a Tasks system based on a queue. The purpose of this class is to provide a queue that will receive functions and executed them by order. Example of use : If you need to chain advertising messages.

Classverisonevent emitterunit tested
Tasksv1.0yesyes

Methods

namedescriptionError (thow an error)
add_taskAdd a task in the queueif the task is not a function

emitter

signaldatadescription
started(task_uuid)Sent when a task is execute
finished(task_uuid)Sent when a task is finished

Example

import { Tasks } from '@adrien.pgd/nodejsutilities'

// delay simulation
function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const my_tasks = new Tasks();
let last_ad_uuid;

my_tasks.on("finished", uuid => {
    if (last_ad_uuid === uuid)
        console.log(`Next ad`);
    else 
        console.log(`end`);
})

tasks.add_task(async ()=>{await sleep(3000); console.log("First add !")})
tasks.add_task(async ()=>{await sleep(3000); console.log("Second add !")})
last_ad_uuid = tasks.add_task(async ()=>{await sleep(3000); console.log("Last add !")})