UNPKG

@d3vtool/mutex

Version:

This library allows you to use concept of mutex for shared in-memory resource in asynchronous environment.

48 lines (47 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Queue = void 0; class QueueNode { constructor(data) { this.data = data; this.prev = null; this.next = null; } } class Queue { constructor() { this.head = null; this.tail = null; } enqueue(data) { const newNode = new QueueNode(data); if (this.tail === null) { this.head = this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } dequeue() { if (this.head === null) { return null; } const currentNode = this.head; this.head = this.head.next; if (this.head) { this.head.prev = null; } else { this.tail = null; } const data = currentNode.data; currentNode.next = currentNode.prev = null; return data; } isEmpty() { return this.head === null; } } exports.Queue = Queue;