bassine

An asynchronous and bounded resource pool for generic objects

Usage no npm install needed!

<script type="module">
  import bassine from 'https://cdn.skypack.dev/bassine';
</script>

README

Bassine

An asynchronous and bounded resource pool for generic objects

Bassine provides an implementation of a resource pool aimed at allowing bounded construction and re-use of expensive-to-allocate objects. This is for example the case with objects that spawn and control external processes such as database connections or headless browsers.

Contents

Installation

$ npm install --save bassine

Usage

Try out Bassine in your browser

const pool = new Pool(2, async i => i);

const one = await pool.borrow();
// => 1

const two = await pool.borrow();
// => 2

pool.return(one);
pool.return(two);

API

Pool

constructor

Create a new pool.

Parameters

  • limit Number The limit on the number of items available in the pool.
  • factory Function The factory function for constructing items for the pool.

Examples

const pool = new Pool(10, async i => `Item ${i}`);

borrow

Borrow an item from this pool.

Examples

const borrowed = await pool.borrow();
// => 'Item 0'

Returns Promise A promise that will resolve with the item when available.

return

Return an item to this pool.

Parameters

  • item any The item to return to this pool.

Examples

const returned = pool.return(borrowed);
// => true

Returns Boolean true if the item was accepted by this pool, otherwise false.

License

Copyright © 2016 Kasper Kronborg Isager. Licensed under the terms of the MIT license.