doc/utility.html
<!DOCTYPE html>
<html>
<head>
<title>utility.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>utility.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<pre><code>telegram.link
Copyright <span class="hljs-number">2014</span> Enrico Stara <span class="hljs-string">'enrico.stara@gmail.com'</span>
Released under the MIT License
http:<span class="hljs-comment">//telegram.link</span>
</code></pre>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>The <code>Promise</code> polyfill.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">require</span>(<span class="hljs-string">'es6-promise'</span>).polyfill();</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Calls a service and infers the arguments by the caller function.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callService</span>(<span class="hljs-params">service, emitter, channel, callback</span>) </span>{
<span class="hljs-keyword">var</span> argsValue = <span class="hljs-built_in">Array</span>.prototype.slice.call(<span class="hljs-built_in">Array</span>.prototype.slice.call(<span class="hljs-built_in">arguments</span>)[<span class="hljs-number">4</span>]);
<span class="hljs-keyword">var</span> callerFunc = <span class="hljs-built_in">arguments</span>.callee.caller;
<span class="hljs-keyword">var</span> eventName = service.name || service._name;
<span class="hljs-keyword">var</span> callerArgNames = _retrieveArgumentNames(callerFunc);
<span class="hljs-keyword">var</span> props = {};
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < callerArgNames.length - <span class="hljs-number">1</span>; i++) {
props[callerArgNames[i]] = argsValue[i];
}
<span class="hljs-keyword">if</span> (callback) {
emitter.once(eventName, callback);
}
<span class="hljs-keyword">if</span> (emitter.isReady(<span class="hljs-literal">true</span>)) {
<span class="hljs-keyword">try</span> {
service({
props: props,
channel: channel,
callback: createEventEmitterCallback(eventName, emitter)
});
} <span class="hljs-keyword">catch</span> (err) {
emitter.emit(<span class="hljs-string">'error'</span>, err);
}
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">fulfill, reject</span>) </span>{
emitter.once(<span class="hljs-string">'error'</span>, reject);
emitter.once(eventName, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">result</span>) </span>{
emitter.removeListener(<span class="hljs-string">'error'</span>, reject);
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> result !== <span class="hljs-string">'boolean'</span> && result.instanceOf(<span class="hljs-string">'mtproto.type.Rpc_error'</span>)) {
reject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(result.error_message));
} <span class="hljs-keyword">else</span> {
fulfill(result);
}
});
});
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">_retrieveArgumentNames</span>(<span class="hljs-params">func</span>) </span>{
<span class="hljs-keyword">var</span> found = <span class="hljs-regexp">/^[\s\(]*function[^(]*\(\s*([^)]*?)\s*\)/</span>.exec(func.toString());
<span class="hljs-keyword">return</span> found && found[<span class="hljs-number">1</span>] ? found[<span class="hljs-number">1</span>].split(<span class="hljs-regexp">/,\s*/</span>) : [];
}</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Provides a callback function that emits the supplied event type or an error event.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createEventEmitterCallback</span>(<span class="hljs-params">event, emitter</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">ex</span>) </span>{
<span class="hljs-keyword">if</span> (ex) {
emitter.emit(<span class="hljs-string">'error'</span>, ex);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">var</span> args = <span class="hljs-built_in">Array</span>.prototype.slice.call(<span class="hljs-built_in">arguments</span>);
args[<span class="hljs-number">0</span>] = event;
emitter.emit.apply(emitter, args);
<span class="hljs-keyword">if</span> (event == <span class="hljs-string">'end'</span>) {
emitter.removeAllListeners();
}
}
};
}</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Export the ‘service’ module.</p>
</div>
<div class="content"><div class='highlight'><pre>exports.callService = callService;
exports.createEventEmitterCallback = createEventEmitterCallback;</pre></div></div>
</li>
</ul>
</div>
</body>
</html>