-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_src_main_eventEmitter.js.html
More file actions
144 lines (120 loc) · 4.83 KB
/
lib_src_main_eventEmitter.js.html
File metadata and controls
144 lines (120 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: lib/src/main/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: lib/src/main/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
**/
/** @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 < 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-connector.html">connector</a></li><li><a href="module-types.html">types</a></li><li><a href="module-vendor-sdk.html">vendor-sdk</a></li></ul><h3>Classes</h3><ul><li><a href="EventEmitter.html">EventEmitter</a></li><li><a href="module-connector.Connector.html">Connector</a></li><li><a href="module-types.Contact.html">Contact</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-vendor-sdk.Sdk.html">Sdk</a></li><li><a href="module-vendor-sdk-Call.html">Call</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.5</a> on Mon Sep 28 2020 15:08:25 GMT-0700 (Pacific Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>