npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

generic-pool-decorator

v1.0.1

Published

a decorator for generic-pool

Downloads

4

Readme

Generic Pool Decorator

The decorator we all know and love from generic-pool v2, now extracted into it's own module Use it to transparently handle object acquisition for a function.

install

npm install [--save] generic-pool-decorator

Pooled function decoration

To create a pool decorator

const decorator = require('generic-pool-decorator')
const pool; /* an instance of generic-pool */

const pooled = decorator(pool)

pooled is now our decorator function we use to 'decorate' other functions that need to use resources from the pool.

--

Let us start with a very basic function that calls a method on a resource that is managed by the pool. It doesn't know how get resources or return them, or where they even come from (all good things).

const myFunc = function(resource, cb){
  resource.doSomething()
  cb()
}

we can then decorate that function using pooled from above:

const pooledFunc = pooled(myFunc)

and whenever we want to carry out the action defined in our original function we just call the pooledFunc

pooledFunc()

The decorator has taken care of fetching the resource from the pool and the returning it afterwards.

--

If we need to pass in any arguments, or our own callback to be executed once our function is done, that is possible too.

Starting with another simple function that uses an imaginary client to increment a counter by a variable amount and return the new count via a callback:

const myCounterFunc = function(resource, someNumber, cb){
  const newTotal = resource.increment(someNumber)
  cb(null, newTotal)
}

we decorate it as before:

const pooledFunc = pooled(myCounterFunc)

and now we can use the decorated function like so

const addThis = 12
const myCallback = function(err, newCount){
  // pretend we handle errors and this is actually
  // some async work..
  console.log(newCount)
}; 
pooledFunc(addThis, myCallback)

the decorator will fetch a resource from the pool, pass it to our original function along with the addThis and our myCallback. Once our original function executes it's callback, the decorator returns the resource to the pool, and then executes our callback with the value from the original function.

--

Here some more complex examples from the previous docs:

const privateFn = function(client, arg, cb) {
    // Do something with the client and arg. Client is auto-released when cb is called
    cb(null, arg);
}
const publicFn = pooled(privateFn);

Keeping both private and public versions of each function allows for pooled functions to call other pooled functions with the same member. This is a handy pattern for database transactions:


const privateBottom = function(client, arg, cb) {
    //Use client, assumed auto-release
}

const publicBottom = pooled(privateBottom);

const privateTop = function(client, cb) {
    // e.g., open a database transaction
    privateBottom(client, "arg", function(err, retVal) {
        if(err) { return cb(err); }
        // e.g., close a transaction
        cb();
    });
}
const publicTop = pooled(privateTop);

Run Tests

$ npm install
$ npm test

The tests are run/written using Tap. Most are ports from the old espresso tests and are not in great condition.

Linting

We use eslint and the standard ruleset.

License

(The MIT License)

Copyright (c) 2010-2016 James Cooper, James Butler;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.