bounded-buffer
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

bounded-buffer

travis-ci codecov

The Node.js SDK for the classic Producer-Consumer problem, using the Record Semaphore solution.

Installation

$ npm install bounded-buffer

Basic usage

import BoundedBuffer from "bounded-buffer";

const sleep = (n: number) => new Promise((resolve) => setTimeout(resolve, n));

const boundedBuffer = new BoundedBuffer<number>({
    bufferSize: 10,
});

const put = async(n: number) => {
    let i = 0;
    while (i < n) {
        await boundedBuffer.putItem(123);
        i++;
        await sleep(5);
    }
}

const get = async(n: number) => {
    let i = 0;
    while (i < n) {
        await boundedBuffer.getItem();
        i++;
        await sleep(5);
    }
}

Promise.all(
    [
        put(100)
            .catch(e => console.error(e)),
        get(100)
            .catch(e => console.error(e))
    ]
).then(() => {
    boundedBuffer.destroy();
    console.log('bufferSize', boundedBuffer['buffer'].length)
})

Running in background

Sometimes the producers or consumers need be running in background:

License

(MIT)

Copyright (c) 2019 ImHype

Readme

Keywords

none

Package Sidebar

Install

npm i bounded-buffer

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

27.9 kB

Total Files

23

Last publish

Collaborators

  • junesmith