UNPKG

scv-connector-base

Version:
146 lines (121 loc) 5.7 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: eventEmitter.js</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">Source: eventEmitter.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** An emitter implementation based on the Node.js EventEmitter API: https://nodejs.org/dist/latest-v6.x/docs/api/events.html#events_class_eventemitter Adapted from https://git.soma.salesforce.com/aura/lightning-global/blob/master/ui-lightning-components/src/main/modules/lightning/utilsPrivate/eventEmitter.js **/ /** @ignore **/ export class EventEmitter { constructor(whitelistedEventNames) { this.registry = {}; this._whitelistedEventNames = whitelistedEventNames ? whitelistedEventNames : null; } validateEventName(name) { if (!this._whitelistedEventNames) { return; } if (!this._whitelistedEventNames.has(name)) { throw new Error(`Unsupported event name: ${name}`); } } /** Registers a listener on the emitter @method EventEmitter#on @param {String} name - The name of the event @param {Function} listener - The callback function @return {EventEmitter} - Returns a reference to the `EventEmitter` so that calls can be chained **/ on(name, listener) { this.validateEventName(name); this.registry[name] = this.registry[name] || []; this.registry[name].push(listener); return this; } /** Registers a listener on the emitter that only executes once @method EventEmitter#once @param {String} name - The name of the event @param {Function} listener - The callback function @return {EventEmitter} - Returns a reference to the `EventEmitter` so that calls can be chained **/ once(name, listener) { this.validateEventName(name); const doOnce = function() { listener.apply(null, arguments); this.removeListener(name, doOnce); }.bind(this); this.on(name, doOnce); return this; } /** Synchronously calls each listener registered with the specified event @method EventEmitter#emit @param {String} name - The name of the event @param {Object} args - The args to be passed over to the listener @return {Boolean} - Returns `true` if the event had listeners, `false` otherwise **/ emit(name, ...args) { this.validateEventName(name); const listeners = this.registry[name]; let foundListener = false; if (listeners) { listeners.forEach(listener => { foundListener = true; listener.apply(null, args); }); } return foundListener; } /** Removes the specified `listener` from the listener array for the event named `name` @method EventEmitter#removeListener @param {String} name - The name of the event @param {Function} listener - The callback function @return {EventEmitter} - Returns a reference to the `EventEmitter` so that calls can be chained **/ removeListener(name, listener) { this.validateEventName(name); const listeners = this.registry[name]; if (listeners) { for (let i = 0, len = listeners.length; i &lt; len; i += 1) { if (listeners[i] === listener) { listeners.splice(i, 1); return this; } } } return this; } } </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-baseConnector.html">baseConnector</a></li><li><a href="module-types.html">types</a></li></ul><h3>Classes</h3><ul><li><a href="module-types.ActiveCallsResult.html">ActiveCallsResult</a></li><li><a href="module-types.CallInfo.html">CallInfo</a></li><li><a href="module-types.CallResult.html">CallResult</a></li><li><a href="module-types.CapabilitiesResult.html">CapabilitiesResult</a></li><li><a href="module-types.ConferenceResult.html">ConferenceResult</a></li><li><a href="module-types.Contact.html">Contact</a></li><li><a href="module-types.GenericResult.html">GenericResult</a></li><li><a href="module-types.HoldToggleResult.html">HoldToggleResult</a></li><li><a href="module-types.InitResult.html">InitResult</a></li><li><a href="module-types.MuteToggleResult.html">MuteToggleResult</a></li><li><a href="module-types.ParticipantRemovedResult.html">ParticipantRemovedResult</a></li><li><a href="module-types.ParticipantResult.html">ParticipantResult</a></li><li><a href="module-types.PhoneCall.html">PhoneCall</a></li><li><a href="module-types.PhoneCallAttributes.html">PhoneCallAttributes</a></li><li><a href="module-types.PhoneContactsResult.html">PhoneContactsResult</a></li><li><a href="module-types.RecordingToggleResult.html">RecordingToggleResult</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Fri Oct 30 2020 16:29:16 GMT-0700 (Pacific Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>