UNPKG

@irrelon/emitter

Version:

Simple JavaScript event emitter with some powerful features.

3,461 lines (877 loc) 37.4 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Class: Emitter</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Class: Emitter</h1> <section> <header> <h2><span class="attribs"><span class="type-signature"></span></span>Emitter<span class="signature">(obj, prototypeMode)</span><span class="type-signature"></span></h2> <div class="class-description">Emitter</div> </header> <article> <div class="container-overview"> <h2>Constructor</h2> <h4 class="name" id="Emitter"><span class="type-signature"></span>new Emitter<span class="signature">(obj, prototypeMode)</span><span class="type-signature"></span></h4> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>obj</code></td> <td class="type"> </td> <td class="description last"></td> </tr> <tr> <td class="name"><code>prototypeMode</code></td> <td class="type"> <span class="param-type">Boolean</span> </td> <td class="description last">Defaults to true. Set to true to add emitter methods to the the passed object's prototype property e.g. obj.prototype.on = emitter.on. Set to false to add emitter methods the object directly e.g. obj.on = emitter.on.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line801">line 801</a> </li></ul></dd> </dl> </div> <h3 class="subsection-title">Methods</h3> <h4 class="name" id=".deferEmit"><span class="type-signature">(static) </span>deferEmit<span class="signature">(eventName, data<span class="signature-attributes">opt</span>)</span><span class="type-signature"></span></h4> <div class="description"> Queues an event to be fired. This has automatic de-bouncing so that any events of the same type that occur within 100 milliseconds of a previous one will all be wrapped into a single emit rather than emitting tons of events for lots of chained inserts etc. Only the data from the last de-bounced event will be emitted. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>eventName</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="attributes"> </td> <td class="description last">The name of the event to emit.</td> </tr> <tr> <td class="name"><code>data</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="description last">Optional data to emit with the event.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line756">line 756</a> </li></ul></dd> </dl> <h4 class="name" id=".emit"><span class="type-signature">(static) </span>emit<span class="signature">(event)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Emit an event. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to emit.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line304">line 304</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".emit"><span class="type-signature">(static) </span>emit<span class="signature">(event, data)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Emit an event with data. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to emit.</td> </tr> <tr> <td class="name"><code>data</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="description last">Data to emit with the event.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line316">line 316</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".emitStatic"><span class="type-signature">(static) </span>emitStatic<span class="signature">(event)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Emit an event that will fire on listeners even when the listener is registered AFTER the event has been emitted. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to emit.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line437">line 437</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".emitStatic"><span class="type-signature">(static) </span>emitStatic<span class="signature">(event, data)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Emit an event with data that will fire on listeners even when the listener is registered AFTER the event has been emitted. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to emit.</td> </tr> <tr> <td class="name"><code>data</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="description last">Data to emit with the event.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line450">line 450</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".emitStatic"><span class="type-signature">(static) </span>emitStatic<span class="signature">(event)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Remove a static event emitter. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to remove static emitter for.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line621">line 621</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".emitStaticId"><span class="type-signature">(static) </span>emitStaticId<span class="signature">(event)</span><span class="type-signature"></span></h4> <div class="description"> Require an id to emit. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> </td> <td class="description last"></td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line516">line 516</a> </li></ul></dd> </dl> <h4 class="name" id=".emitStaticId"><span class="type-signature">(static) </span>emitStaticId<span class="signature">(event, id)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Emit an event that will fire on listeners even when the listener is registered AFTER the event has been emitted. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to emit.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The id of the event to emit.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line526">line 526</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".emitStaticId"><span class="type-signature">(static) </span>emitStaticId<span class="signature">(event, id, data<span class="signature-attributes">opt</span>)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Emit an event that will fire on listeners even when the listener is registered AFTER the event has been emitted. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Attributes</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="attributes"> </td> <td class="description last">The event to emit.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="attributes"> </td> <td class="description last">The id of the event to emit.</td> </tr> <tr> <td class="name"><code>data</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="attributes"> &lt;optional><br> </td> <td class="description last">The data to emit with the event.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line539">line 539</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".off"><span class="type-signature">(static) </span>off<span class="signature">(event, id)</span><span class="type-signature"></span></h4> <div class="description"> Cancels all listeners for an event based on the passed event name and id. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event name to cancel listeners for.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="description last">The ID to cancel all listeners for.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line279">line 279</a> </li></ul></dd> </dl> <h4 class="name" id=".off"><span class="type-signature">(static) </span>off<span class="signature">(event, id, listener)</span><span class="type-signature"></span></h4> <div class="description"> Cancels an event listener based on an event name, id and listener function. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to cancel listener for.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The ID of the event to cancel listening for.</td> </tr> <tr> <td class="name"><code>listener</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">The event listener function used in the on() or once() call to cancel.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line250">line 250</a> </li></ul></dd> </dl> <h4 class="name" id=".off"><span class="type-signature">(static) </span>off<span class="signature">(event, listener)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Cancels the event listener for the passed event and listener function. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event to cancel listener for.</td> </tr> <tr> <td class="name"><code>listener</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">The event listener function used in the on() or once() call to cancel.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line211">line 211</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".off"><span class="type-signature">(static) </span>off<span class="signature">(event)</span><span class="type-signature"> &rarr; {*}</span></h4> <div class="description"> Cancels all event listeners for the passed event. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line187">line 187</a> </li></ul></dd> </dl> <h5>Returns:</h5> <dl> <dt> Type </dt> <dd> <span class="param-type">*</span> </dd> </dl> <h4 class="name" id=".on"><span class="type-signature">(static) </span>on<span class="signature">(event, id, listener)</span><span class="type-signature"></span></h4> <div class="description"> Attach an event listener to the passed event only if the passed id matches the document id for the event being fired. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event to listen for.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="description last">The document id to match against.</td> </tr> <tr> <td class="name"><code>listener</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">The method to call when the event is fired.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line84">line 84</a> </li></ul></dd> </dl> <h4 class="name" id=".on"><span class="type-signature">(static) </span>on<span class="signature">(event, listener)</span><span class="type-signature"></span></h4> <div class="description"> Attach an event listener to the passed event. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event to listen for.</td> </tr> <tr> <td class="name"><code>listener</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">The method to call when the event is fired.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line73">line 73</a> </li></ul></dd> </dl> <h4 class="name" id=".once"><span class="type-signature">(static) </span>once<span class="signature">(event, id, listener)</span><span class="type-signature"></span></h4> <div class="description"> Attach an event listener to the passed event only if the passed id matches the document id for the event being fired. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event to listen for.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">*</span> </td> <td class="description last">The document id to match against.</td> </tr> <tr> <td class="name"><code>listener</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">The method to call when the event is fired.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line162">line 162</a> </li></ul></dd> </dl> <h4 class="name" id=".once"><span class="type-signature">(static) </span>once<span class="signature">(event, listener)</span><span class="type-signature"></span></h4> <div class="description"> Attach an event listener to the passed event which will only fire once. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event to listen for.</td> </tr> <tr> <td class="name"><code>listener</code></td> <td class="type"> <span class="param-type">function</span> </td> <td class="description last">The method to call when the event is fired.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line141">line 141</a> </li></ul></dd> </dl> <h4 class="name" id=".willEmit"><span class="type-signature">(static) </span>willEmit<span class="signature">(event)</span><span class="type-signature"> &rarr; {boolean}</span></h4> <div class="description"> Checks if an event has any event listeners or not. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event to check for.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line647">line 647</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> True if one or more event listeners are registered for the event. False if none are found. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">boolean</span> </dd> </dl> <h4 class="name" id=".willEmitId"><span class="type-signature">(static) </span>willEmitId<span class="signature">(event, id)</span><span class="type-signature"> &rarr; {boolean}</span></h4> <div class="description"> Checks if an event has any event listeners or not based on the passed id. </div> <h5>Parameters:</h5> <table class="params"> <thead> <tr> <th>Name</th> <th>Type</th> <th class="last">Description</th> </tr> </thead> <tbody> <tr> <td class="name"><code>event</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The name of the event to check for.</td> </tr> <tr> <td class="name"><code>id</code></td> <td class="type"> <span class="param-type">String</span> </td> <td class="description last">The event ID to check for.</td> </tr> </tbody> </table> <dl class="details"> <dt class="tag-source">Source:</dt> <dd class="tag-source"><ul class="dummy"><li> <a href="Emitter.js.html">Emitter.js</a>, <a href="Emitter.js.html#line683">line 683</a> </li></ul></dd> </dl> <h5>Returns:</h5> <div class="param-desc"> True if one or more event listeners are registered for the event. False if none are found. </div> <dl> <dt> Type </dt> <dd> <span class="param-type">boolean</span> </dd> </dl> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Emitter.html">Emitter</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Jan 22 2019 14:10:25 GMT+0000 (GMT) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>