lockfn

Collection of function locking objects.

npm install lockfn
10 downloads in the last week
20 downloads in the last month
<h1>lockfn</h1>
<p><strong>(c)<a href="http://www.bumblehead.com" title="bumblehead">Bumblehead</a>, 2013</strong> <a href="#license">MIT-license</a></p>
<h3>OVERVIEW:</h3>
<p>A collection of function locking objects. Each object returns a function through which asynchronous code may be executed. The <a href="#objects">examples</a> are more descriptive than any explanation that would be provided here.</p>
<ul>
<li><code>LockFn.getQueuing</code>, <em>LockFnQueuing</em></li>
<li><code>LockFn.getCaching</code>, <em>LockFnCaching</em></li>
<li><code>LockFn.getRebounding</code>, <em>LockFnRebounding</em></li>
<li><code>LockFn.getThrottling</code>, <em>LockFnThrottling</em></li>
</ul>
<p>As a convenience, the lock objects are provided on a single namespace, <code>lockfn</code>. They could be used independently, but usually one and all are needed within the same application.</p>
<hr>
<h4><a id="install"></a>INSTALL:</h4>
<p>json-local may be downloaded directly or installed through <code>npm</code>.</p>
<ul>
<li><p><strong>npm</strong>   </p>
<pre><code class="lang-bash">$ npm install lockfn</code></pre>
</li>
<li><p><strong>Direct Download</strong></p>
<pre><code class="lang-bash">$ git clone https://github.com/iambumblehead/lockfn.git</code></pre>
</li>
</ul>
<hr>
<h4><a id="objects">OBJECTS:</h4>
<ul>
<li><p><strong>LockFnQueuing</strong>, <strong>LockFn.getQueuing</strong><br>a returned function will store mulitple callbacks and processes them one after another -waiting for the first to complete before calling the next. &#39;useful for holding off execution of function bodies that would add numerous calls to the stack.</p>
<p>the following would print <code>15</code>, then <code>10</code>:</p>
<pre><code class="lang-javascript">fn = function (err, res) { console.log(res); };
lockFnQueuing = LockFnQueuing.getNew();
lockFnQueuing(fn, function (exitFn) {
  setTimeout(function () { exitFn(null, 15); }, 200);
});
lockFnQueuing(fn, function (exitFn) {
  setTimeout(function () { exitFn(null, 10); }, 200);
});</code></pre>
</li>
<li><p><strong>LockFnCaching</strong>, <strong>LockFn.getCaching</strong><br>a returned function will store multiple callbacks and to each of them will return a value that is once-only generated by the first call. &#39;useful for making single request to a web resource that may return data needed by multiple functions.</p>
<p>the following would print <code>15</code>, then (immediately) <code>15</code>:</p>
<pre><code class="lang-javascript">fn = function (err, val) { console.log(null, val); };     
lockFnCaching = LockFnCaching.getNew();     
lockFnCaching(fn, function (exitFn) {
  setTimeout(function () { exitFn(null, 15); }, 200);
});
lockFnCaching(fn, function (exitFn) {
  setTimeout(function () { exitFn(null, 10); }, 200);
});</code></pre>
<p>this object also returns a function that will associate callbacks with a given namespace.</p>
<p>the following would print <code>15</code>, then (immediately) <code>15</code>:</p>
<pre><code class="lang-javascript">fn = function (err, val) { console.log(null, val); };     
lockFnCaching = LockFnCaching.getNamespaceNew();     
lockFnCaching(&#39;address&#39;, fn, function (exitFn) {
  setTimeout(function () { exitFn(null, 15); }, 200);
});
lockFnCaching(&#39;country&#39;, function (exitFn) {
  setTimeout(function () { exitFn(null, 10); }, 200);
}); 
lockFnCaching(&#39;address&#39;, function (exitFn) {
  setTimeout(function () { exitFn(null, 19); }, 200);
});</code></pre>
</li>
<li><p><strong>LockFnRebounding</strong>, <strong>LockFn.getRebounding</strong><br>a returned function will process one callback and during that time, future calls are rebounded until the callback has returned a value. &#39;useful for handling form submission events, where the form should be submitted once only amid multiple submission calls.</p>
<p>the following would print <code>submit one</code>, only.</p>
<pre><code class="lang-javascript">fn = function (err, val) { console.log(null, val); };     
lockFnRebounding = LockFnRebounding.getNamespaceNew();     
lockFnRebounding(function (exitFn) {
  setTimeout(function () { 
      console.log(&#39;submit one&#39;); exitFn() 
  }, 200);
});
lockFnRebounding(function (exitFn) {
  setTimeout(function () { 
      console.log(&#39;submit two&#39;); exitFn() 
  }, 100);
}); 
lockFnRebounding(function (exitFn) {
console.log(&#39;submit three&#39;); exitFn() 
});</code></pre>
</li>
<li><p><strong>LockFnThrottling</strong>, <strong>LockFn.getThrottling</strong><br>a returned function will process one callback and during a specified period of time, then it will process the last callback received during that time. &#39;useful for limiting the execution of functions bound to events triggered rapidly many times.</p>
<p>the following would print <code>go</code>, then <code>now</code>: </p>
<pre><code class="lang-javascript">lockFnThrottling = LockFnThrottling.getNew({ ms : 500 });
lockFnThrottling(function () { console.log(&#39;go&#39;) });
lockFnThrottling(function () { console.log(&#39;home&#39;) }); 
lockFnThrottling(function () { console.log(&#39;now&#39;) });</code></pre>
</li>
</ul>
<hr>
<h4><a id="license">License:</h4>
<p>(The MIT License)</p>
<p>Copyright (c) 2013 <a href="http://www.bumblehead.com" title="bumblehead">Bumblehead</a> <a href="&#109;&#97;&#105;&#x6c;&#x74;&#111;&#58;&#x63;&#x68;&#x72;&#x69;&#115;&#64;&#98;&#117;&#109;&#x62;&#108;&#101;&#104;&#101;&#97;&#100;&#46;&#x63;&#x6f;&#x6d;">&#x63;&#x68;&#x72;&#x69;&#115;&#64;&#98;&#117;&#109;&#x62;&#108;&#101;&#104;&#101;&#97;&#100;&#46;&#x63;&#x6f;&#x6d;</a></p>
<p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the &#39;Software&#39;), 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:</p>
<p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p>
<p>THE SOFTWARE IS PROVIDED &#39;AS IS&#39;, 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.</p>
npm loves you