ContentMine/thresher

View on GitHub
doc/ticker.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html><html lang="en"><head><title>ticker</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="ticker"><meta name="groc-project-path" content="lib/ticker.js"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">lib/ticker.js</div></div><div id="document"><div class="segment"><div class="comments doc-section"><div class="wrapper"><p>Create a new Ticker.
A Ticker is a simple counter to measure
progress towards a goal.
For example if there are 10 tasks that must
be completed before some action is performed,
a Ticker with length 10 can be used. The tasks
can run asychronously and tick the Ticker as
they finish. After 10 ticks, the Ticker's callback
is executed.</p>

<p>Parameters:</p>

<ul>
<li><p><strong>length must be an Integer.</strong><br/>(length of the ticker)</p></li>
<li><p><strong>cb must be a Function.</strong><br/>(callback)</p></li>
</ul></div></div><div class="code"><div class="wrapper"><span class="kd">var</span> <span class="nx">Ticker</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">length</span><span class="p">,</span> <span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">length</span> <span class="o">===</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">&#39;Ticker requires a length&#39;</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">log</span><span class="p">.</span><span class="nx">warn</span><span class="p">(</span><span class="s1">&#39;Ticker has no callback&#39;</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">length</span> <span class="o">=</span> <span class="nx">length</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">cb</span> <span class="o">=</span> <span class="nx">cb</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">progress</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
  <span class="nx">log</span><span class="p">.</span><span class="nx">debug</span><span class="p">(</span><span class="s1">&#39;Ticker created with length&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
<span class="p">}</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Progress by one. If the ticker is finished,
run the callback.</p></div></div><div class="code"><div class="wrapper"><span class="nx">Ticker</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">tick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">progress</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
  <span class="nx">log</span><span class="p">.</span><span class="nx">debug</span><span class="p">(</span><span class="s1">&#39;Ticker progress:&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">progress</span><span class="p">,</span> <span class="s1">&#39;of&#39;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">progress</span> <span class="o">==</span> <span class="k">this</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">log</span><span class="p">.</span><span class="nx">debug</span><span class="p">(</span><span class="s1">&#39;Ticker finished&#39;</span><span class="p">);</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">cb</span><span class="p">();</span>
  <span class="p">}</span>
<span class="p">}</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Lengthen the ticker by <code>x</code>.</p></div></div><div class="code"><div class="wrapper"><span class="nx">Ticker</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">elongate</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">length</span> <span class="o">+=</span> <span class="nx">x</span> <span class="o">||</span> <span class="mi">1</span><span class="p">;</span>
<span class="p">}</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Ticker</span><span class="p">;</span></div></div></div></div></body></html>