This module provides a class to create an object pool structure. The pool supports a classical API when you take and
release some resources from the pool, and the API with support of sharing resources between different consumers.
In addition, the pool supports segmentation of resources via a provided hash function.
const pool = newPool(createDBConnection, {size:5});
// Number of resources in the pool (5) console.log(pool.size);
// Number of available resources in the pool (5) console.log(pool.available);
// If the pool is empty, `value` will be nullish const {value, free, destroy} = pool.take();
// 5 console.log(pool.size);
// 4 console.log(pool.available);
// Releasing a resource and returning it to the pool free();
// 5 console.log(pool.available);
// Creating a new resource if the pool is empty constanotherResource = pool.takeOrCreate();
// Destroying a resource instead of returning it to the pool // (if you provide a resource destructor when creating a pull instance, it will be used there) anotherResource.destroy();
// Returning a promise that resolves with a resource from the pool pool.takeOrWait().then(({value, free, destroy}) => {});
Using API of borrowing you can share resources between different consumers without taking their from the pool.
If you borrow a resource, you can't take it till all borrow consumers release it.
importPoolfrom'core/pool';
const pool = newPool(createDOMObserver, {size:1});
// Number of resources in the pool (5) console.log(pool.size);
// Number of available resources in the pool (5) console.log(pool.available);
// If the pool is empty, `value` will be nullish constresource1 = pool.borrow();
console.log(resource1.value);
// 5 console.log(pool.size);
// 5 console.log(pool.available);
// You can't take a resource if it already borrowed console.log(pool.take().value === null);
// But, you can borrow it constresource2 = pool.borrow();
console.log(resource2.value);
// Releasing resources and returning their to the pool resource1.free(); resource2.free();
// Now you can take it console.log(pool.take().value);
// Creating a new resource if the pool is empty constanotherResource = pool.borrowOrCreate();
// Destroying a resource instead of releasing it // (if you provide a resource destructor when creating a pull instance, it will be used there) anotherResource.destroy();
// Returning a promise that resolves with a borrowed resource from the pool pool.borrowOrWait().then(({value, free, destroy}) => {});
You can add a function to calculate a hash of the created resource.
Using this hash, you will be able to take or borrow a resource that matches to the specified hash.
By passing a maxSize property you can define how many elements can be contained in the pool.
Mind, if you also pass a size property larger than maxSize, there will be generated an exception.
You can clear a pool and destroys all created resources via invoking the clear method.
If you provide a resource destructor when creating a pull instance, it will be with this method.
You can pass callback functions to handle take, borrow, free, clear pool hooks.
Besides regular arguments, these handlers take arguments that are passed to the associated methods.
Returns an available resource from the pool.
The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
The returned result is wrapped with a structure that contains methods to release or drop this resource.
If the pool is empty, the structure value field will be nullish.
Returns an available resource from the pool.
The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
The returned result is wrapped with a structure that contains methods to release or drop this resource.
If the pool is empty, it creates a new resource and returns it.
Returns a promise with an available resource from the pull.
The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
The returned result is wrapped with a structure that contains methods to release or drop this resource.
If the pool is empty, the promise will wait till it release.
Borrows an available resource from the pool.
The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers.
Mind, you can’t take this resource from the pool when it’s borrowed.
The returned result is wrapped with a structure that contains methods to release or drop this resource.
If the pool is empty, the structure value field will be nullish.
Borrows an available resource from the pool.
The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers.
Mind, you can’t take this resource from the pool when it’s borrowed.
The returned result is wrapped with a structure that contains methods to release or drop this resource.
If the pool is empty, it creates a new resource and returns it.
Returns a promise with a borrowed resource from the pull.
The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers.
Mind, you can’t take this resource from the pool when it’s borrowed.
The returned result is wrapped with a structure that contains methods to release or drop this resource.
If the pool is empty, the promise will wait till it release.
core/pool
This module provides a class to create an object pool structure. The pool supports a classical API when you take and release some resources from the pool, and the API with support of sharing resources between different consumers. In addition, the pool supports segmentation of resources via a provided hash function.
"Classical" API
Borrowing of resources
Using API of borrowing you can share resources between different consumers without taking their from the pool. If you borrow a resource, you can't
take
it till all borrow consumers release it.Providing arguments to a resource constructor
There is a possibility to provide arguments into a resource constructor. Just pass them as the second argument into the pool constructor.
Also, you can pass arguments as a function. This function will be invoked with a resource index and should return arguments to pass.
In addition, you can provide arguments to create a resource to
takeOrCreate
andborrowOrCreate
methods.Segmentation of resources
You can add a function to calculate a hash of the created resource. Using this hash, you will be able to take or borrow a resource that matches to the specified hash.
Limiting the pool size
By passing a
maxSize
property you can define how many elements can be contained in the pool. Mind, if you also pass asize
property larger thanmaxSize
, there will be generated an exception.Clearing a pool
You can clear a pool and destroys all created resources via invoking the
clear
method. If you provide a resource destructor when creating a pull instance, it will be with this method.Attaching hook handlers
You can pass callback functions to handle
take
,borrow
,free
,clear
pool hooks. Besides regular arguments, these handlers take arguments that are passed to the associated methods.API
Constructor options
The structure constructor can take an object with optional parameters.
Getters
maxSize
The maximum number of resources that the pool can contain.
size
Number of resources that are stored in the pool.
available
Number of available resources that are stored in the pool.
Methods
take
Returns an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the structure value field will be nullish.
takeOrCreate
Returns an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, it creates a new resource and returns it.
takeOrWait
Returns a promise with an available resource from the pull. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the promise will wait till it release.
borrow
Borrows an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers. Mind, you can’t take this resource from the pool when it’s borrowed.
The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the structure value field will be nullish.
borrowOrCreate
Borrows an available resource from the pool. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers. Mind, you can’t take this resource from the pool when it’s borrowed.
The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, it creates a new resource and returns it.
borrowOrWait
Returns a promise with a borrowed resource from the pull. The passed arguments will be used to calculate a resource hash. Also, they will be provided to hook handlers.
When a resource is borrowed, it won’t be dropped from the pool. I.e. you can share it with other consumers. Mind, you can’t take this resource from the pool when it’s borrowed.
The returned result is wrapped with a structure that contains methods to release or drop this resource. If the pool is empty, the promise will wait till it release.
clear
Clears the pool, i.e. drops all created resource. The method takes arguments that will be provided to hook handlers.