1.0.5 • Published 2 years ago

@jakubneubauer/limited-blocking-queue v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

limited-blocking-queue

Javascript implementation of queue with asynchronous push/pull and with limit of stored items. Operation pull returns a promise fulfilled whenever the item will be available (possibly immediately). The push operation returns a promise fulfilled when the push is possible to perform. If the queue is not full, it will be immediately (with Promise.resolve()), if the queue is full, it will be when enough items will be pulled.

npm install --save @jakubneubauer/limited-blocking-queue

Example of Usage

Demonstration of async pull

import {LimitedBlockingQueue} from '@jakubneubauer/limited-blocking-queue';

var queue = new LimitedBlockingQueue();

// waits for next push()
queue.pull().then((result) => console.debug(result));

setTimeout(() => {
  queue.push('hello world')
}, 1000);

Demonstration of async push

import {LimitedBlockingQueue} from '@jakubneubauer/limited-blocking-queue';

var queue = new LimitedBlockingQueue(); // default size is 1

(async function() {
    // first push is done immediately
    queue.push(1).then(() => console.debug("first push done"));
    // second push is postponed because the queue is full
    queue.push(2).then(() => console.debug("second push done"));
    // Pull will make room in the queue, after that the second push will be done
    await queue.pull().then((item) => console.debug("Pulled item " + item));
    // This pulled item will be logged after the second push
    await queue.pull().then((item) => console.debug("Pulled item " + item));
})();

output:

first push done
Pulled item 1
second push done
Pulled item 2
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.0.8

2 years ago

0.0.5

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago