lockfn

0.3.1 • Public • Published

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 = function (tplname, fn) {
      server_request(tplname, function (err, tpl) {
         fn(err, tpl); // tpl is '<b>tpl</b>'
      });
    };
    gettplcache = lockfn.caching(function (tplname, fn) {
      gettpl(tplname, fn)
    };
    gettplcache('listitem.mustache', function (err, tpl) {
      console.log('tpl is ' + tpl);        // tpl is <b>item</b>
    });
    gettplcache('listitem.mustache', function (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(function (tplname, fn) {
      gettpl(tplname, fn)
    };
    gettplcache('lakelistitem.mustache', function (err, tpl) {
      console.log('tpl is ' + tpl);       // tpl is <b>lake</b>
    });
    gettplcache('lakelistitem.mustache', function (err, tplcached) {
      console.log('tpl is ' + tplcached); // tpl is <b>lake</b>
    });
    gettplcache('countrylistitem.mustache', function (err, ctpl) {
      console.log('tpl is ' + tpl);       // tpl is <b>country</b>
    });
    gettplcache('countrylistitem.mustache', function (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(function (session, cfg, tplname, fn) {
      gettpl(tplname, fn)
    };
    gettplcache(session, cfg, 'nav.mustache', function (err, tpl) {
      console.log('tpl is ' + tpl);      // tpl is <b>nav</b>
    });
    gettplcache(session, cfg, 'nav.mustache', function (err, tplcache) {
      console.log('tpl is ' + tplcache); // tpl is <b>nav</b>
    });
    gettplcache(session, cfg, 'foot.mustache', function (err, tplcache) {
      console.log('tpl is ' + tplcache); // tpl is <b>foot</b>
    });

    Clear the cache and request the template anew

    gettplcache = lockfn.caching.namespace(function (session, cfg, tplname, fn) {
      gettpl(tplname, fn)
    };
    session = 'english';
    gettplcache(session, cfg, 'bathroom.mustache', function (err, tpl) {
      console.log(tpl);      // <b>i need the bathroom</b>
    });
    session = 'spanish';
    gettplcache(session, cfg, 'bathroom.mustache', function (err, tplcache) {
      console.log(tplcache); // <b>i need the bathroom</b>
    });
    gettplcache.clear();
    gettplcache(session, cfg, 'bathroom.mustache', function (err, tpl) {
      console.log(tpl);      // <b>puedo ir al bano</b>
    });
    session = 'english';
    gettplcache(session, cfg, 'bathroom.mustache', function (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(function (cipher, fn) {
  fn(null, busydecryption(cipher)); // busydecryption adds numerous frames
});
getdecryptedblobqueue(cipher, function (plain) {
  console.log(plain); // long string
});
getdecryptedblobqueue(cipher, function (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(function (cipher, key, type, fn) {
  fn(null, busydecryption(cipher, key, type));
});
getdecryptedblobqueue(cipher, key, type, function (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(function (arg1, arg2, exitfn) {
  setTimeout(function () { 
    console.log('submit one');  // submit one
    exitfn(1, 2) 
  }, 200);
});
rebound('a', 'b', function (num1, num2) {
  console.log(num1, num2); // 1 2
});
rebound('a', 'b', function (num1, num2) {
  console.log(num1, num2); // not called
});
rebound('a', 'b'); // not called
setTimeout(function () { 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(function () { console.log('rain') });  // rain
    lockthrottle(function () { console.log('in') });
    lockthrottle(function () { console.log('spain') }); // spain
    lockthrottle(function () {
      setTimeout(function () {
        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 following and previous calls.

scrounge

(The MIT License)

Copyright (c) Bumblehead chris@bumblehead.com

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.

Package Sidebar

Install

npm i lockfn

Weekly Downloads

25

Version

0.3.1

License

MIT

Unpacked Size

33.2 kB

Total Files

14

Last publish

Collaborators

  • bumblehead