-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncEmitter.js.html
197 lines (168 loc) · 5.04 KB
/
asyncEmitter.js.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: asyncEmitter.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: asyncEmitter.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
/**
* Now, an event emitter is an object/method which
* triggers an event as soon as some action takes
* place so as to pass the control to the parent function.
*/
class AsyncEmitter {
constructor() {
this.events = new Map();
this.wrappers = new Map();
}
/**
* It's used to add function when certain event is triggered
* @param {String} name - name of the event
* @param {Function} fn - function which will be called when
* event is triggered
* */
on(name, fn) {
if (fn === undefined) {
return new Promise(resolve => {
this.on(name, resolve);
});
}
const event = this.events.get(name);
if (event) {
event.add(fn);
} else {
this.events.set(name, new Set([fn]));
}
}
/**
* It's used to hinge a function on a
* certain event for a time specified by the third argument
* @param {String} name - name of the event
* @param {Function} fn - function which will be called when
* event is triggered
* @param {Number} timeout - time during which the function
* will process this event,
* and after which it will be removed from this event
*/
onTemporary(name, fn, timeout = 0) {
if (timeout) {
this.on(name, fn);
setTimeout(() => {
this.remove(name, fn);
}, timeout);
}
}
/**
* It's used to add function which will occur only once
* @param {String} name - name of the event
* @param {Function} fn - function which will be called when
* event is triggered
* */
once(name, fn) {
if (fn === undefined) {
return new Promise(resolve => {
this.once(name, resolve);
});
}
const g = (...args) => {
this.remove(name, g);
return fn(...args);
};
this.wrappers.set(fn, g);
this.on(name, g);
}
/**
* It's used to trigger events
* @param {String} name - name of the event
* @param {any} args - arguments for functions
* */
async emit(name, ...args) {
const event = this.events.get(name);
const fns = [...event.values()];
return Promise.all(fns.map(fn => fn(...args)));
}
/**
* It`s used to detach (delete) a function
* from a specific event
* @param {String} name - name of the event
* @param {Function} fn - function that we want to
* remove from this event
*/
remove(name, fn) {
const { events, wrappers } = this;
const event = events.get(name);
if (event && event.has(fn)) {
event.delete(fn);
return;
}
const wrapper = wrappers.get(fn);
if (wrapper) {
event.delete(wrapper);
wrappers.delete(fn);
}
}
/**
* Method to clear all events from emmiter or just one event
* @param {String} name - name of event, optinal paramater
*/
clear(name) {
if (name) this.events.delete(name);
else this.events.clear();
}
/**
* Return all listeners of event
* @param {String} name - name of event
* @returns {Array} of listeners
*/
listeners(name) {
if (name) {
const listeners = this.events.get(name);
if (listeners) return [...listeners];
else return [];
}
}
/**
* Return number of listeners of event, or number of events
* @param {String} name - name of event, optinal paramater
* @returns {Number}
*/
count(name) {
if (name) return this.listeners(name).length;
else return this.events.size;
}
/**
* Return array of all events of emitter
* @returns {Array}
*/
names() {
return Array.from(this.events.keys());
}
}
module.exports = AsyncEmitter;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AsyncEmitter.html">AsyncEmitter</a></li></ul><h3>Global</h3><ul><li><a href="index.html#asyncMemoize">asyncMemoize</a></li><li><a href="index.html#map">map</a></li><li><a href="index.html#Queue">Queue</a></li><li><a href="index.html#queue">queue</a></li><li><a href="index.html#reduce">reduce</a></li><li><a href="index.html#retry">retry</a></li><li><a href="index.html#series">series</a></li><li><a href="index.html#some">some</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Jan 04 2021 15:21:06 GMT+0200 (GMT+02:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>