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

lockfn

v0.4.0

Published

Collection of function locking objects.

Downloads

196

Readme

lockfn

(c)Bumblehead MIT-license

A collection of function locks through which asynchronous code may be controlled.

  • lockfn.caching Most common lock. Obtain data once for multiple callers.

    A template applied to each item of a list view will be requested once only:

    gettpl = (tplname, fn) => {
      server_request(tplname, (err, tpl) => {
         fn(err, tpl); // tpl is '<b>tpl</b>'
      });
    };
    gettplcache = lockfn.caching((tplname, fn) => {
      gettpl(tplname, fn)
    };
    gettplcache('listitem.mustache', (err, tpl) => {
      console.log('tpl is ' + tpl);        // tpl is <b>item</b>
    });
    gettplcache('listitem.mustache', (err, tplcached) => {
      console.log('tpl is ' + tplcached);  // tpl is <b>item</b>
    });

    This list uses different templates and unique templates are requested once only:

    // difference between lockfn.caching and lockfn.caching.namespace...
    //
    //   lockfn.caching: the first value obtained is returned to all callers.
    //   if 'lakelistitem.mustache' is obtained first, it is returned to all
    //   callers.
    //
    //   lockfn.caching.namespace: applies caching to values unique to string
    //   param given before the callback. Here 'lakelistitem.mustache' returns a
    //   cached value to calls for 'lakelistitem.mustache'. A different value is
    //   requested and cached for 'countrylistitem.mustache':
    gettplcache = lockfn.caching.namespace((tplname, fn) => {
      gettpl(tplname, fn)
    };
    gettplcache('lakelistitem.mustache', (err, tpl) => {
      console.log('tpl is ' + tpl);       // tpl is <b>lake</b>
    });
    gettplcache('lakelistitem.mustache', (err, tplcached) => {
      console.log('tpl is ' + tplcached); // tpl is <b>lake</b>
    });
    gettplcache('countrylistitem.mustache', (err, ctpl) => {
      console.log('tpl is ' + tpl);       // tpl is <b>country</b>
    });
    gettplcache('countrylistitem.mustache', (err, ctplcached) => {
      console.log('tpl is ' + tplcached); // tpl is <b>country</b>
    });

    Use multiple params with functions returned by lockfn.caching and lockfn.caching.namespace. Both require a callback as last parameter. lockfn.caching.namespace requires a string as the second-to-last parameter (a namespace for which the cache is held).

    gettplcache = lockfn.caching.namespace((session, cfg, tplname, fn) => {
      gettpl(tplname, fn)
    };
    gettplcache(session, cfg, 'nav.mustache', (err, tpl) => {
      console.log('tpl is ' + tpl);      // tpl is <b>nav</b>
    });
    gettplcache(session, cfg, 'nav.mustache', (err, tplcache) => {
      console.log('tpl is ' + tplcache); // tpl is <b>nav</b>
    });
    gettplcache(session, cfg, 'foot.mustache', (err, tplcache) => {
      console.log('tpl is ' + tplcache); // tpl is <b>foot</b>
    });

    Clear the cache and request the template anew

    gettplcache = lockfn.caching.namespace((session, cfg, tplname, fn) => {
      gettpl(tplname, fn)
    };
    session = 'english';
    gettplcache(session, cfg, 'bathroom.mustache', (err, tpl) => {
      console.log(tpl);      // <b>i need the bathroom</b>
    });
    session = 'spanish';
    gettplcache(session, cfg, 'bathroom.mustache', (err, tplcache) => {
      console.log(tplcache); // <b>i need the bathroom</b>
    });
    gettplcache.clear();
    gettplcache(session, cfg, 'bathroom.mustache', (err, tpl) => {
      console.log(tpl);      // <b>puedo ir al bano</b>
    });
    session = 'english';
    gettplcache(session, cfg, 'bathroom.mustache', (err, tpl) => {
      console.log(tpl);      // <b>puedo ir al bano</b>
    });
  • lockfn.expiring Auto-call a function after given period of time has passed. If a function waits on a response from a server -auto-call that function after a few seconds (and do not allow future calls to the function).

    lockfn.expiring(function callmeafter5sec () {
      console.log('called in 5 seconds or less');
    }, 5000);
  • lockfn.queuing Avoid stack overflow. Control caller access to a function that adds numerous frames to the stack by queuing calls so each completes before next begins:

getdecryptedblobqueue = lockfn.queuing((cipher, fn) => {
  fn(null, busydecryption(cipher)); // busydecryption adds numerous frames
});
getdecryptedblobqueue(cipher, (plain) => {
  console.log(plain); // long string
});
getdecryptedblobqueue(cipher, (plain) => {
  console.log(plain); // long string
}); 

Like lockfn.caching, a callback is required as the last parameter. Other params given in calls to the lock are passed to the function the lock was constructed around.

getdecryptedblobqueue = lockfn.queuing((cipher, key, type, fn) => {
  fn(null, busydecryption(cipher, key, type));
});
getdecryptedblobqueue(cipher, key, type, (plain) => {
  console.log(plain); // long string
});
  • lockfn.rebounding A returned function handles one call, ignoring other calls until the first returns. Useful for handling form submit events, where a form should be submitted once only amid multiple submit events.

    Like lockfn.caching, a callback is given as the last parameter but is not required.

// the following would print 'submit one' only
rebound = lockrebound((arg1, arg2, exitfn) => {
  setTimeout(function () { 
    console.log('submit one');  // submit one
    exitfn(1, 2) 
  }, 200);
});
rebound('a', 'b', (num1, num2) => {
  console.log(num1, num2); // 1 2
});
rebound('a', 'b', (num1, num2) => {
  console.log(num1, num2); // not called
});
rebound('a', 'b'); // not called
setTimeout(() => { rebound('a', 'b'); }, 220); // is called
  • lockfn.throttling A returned function handles first and last calls made within a specified amount of time. Other calls during that time are ignored. Limit calls which may be triggered rapidly many times. For example ,

    The following would print 'rain', 'spain', then 'plain':

    lockthrottle = lockfn.throttling({ ms : 500 });
    lockthrottle(() => { console.log('rain') });  // rain
    lockthrottle(() => { console.log('in') });
    lockthrottle(() => { console.log('spain') }); // spain
    lockthrottle(() => {
      setTimeout(() => {
        console.log('plain') };                         // plain
      }, 600)
    });

How:

Locks here are simple but effective due to the execution model found in various ECMAScript environments. Messages are handled in a queue sequentially, protecting the space in one lock call from calls made before or afterward.

scrounge

(The MIT License)

Copyright (c) Bumblehead [email protected]

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.