-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsmitter.js
More file actions
222 lines (194 loc) · 5.53 KB
/
Copy pathpsmitter.js
File metadata and controls
222 lines (194 loc) · 5.53 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @name Psmitter
* @description Usefull event emitter for Browser
* @author Fernando Palacios <jf.palacios.sz@gmail.com>
* @license MIT
*/
(function (context) {
'use strict'
/**
* Psmitter
* @constructor
* @api public
*/
const Psmitter = function () {
this.events = {}
}
/**
* Register a listener for one or more events
* @param {string, array} eventNames event's to listen
* @param {Function} fn callback function to execute
* @return {Psmitter} current Psmitter instance
* @api public
*/
Psmitter.prototype.on = function (eventNames, fn) {
let toListen = eventNames
if (!Array.isArray(eventNames)) {
toListen = [toString(eventNames)]
}
for (let i = 0; i < toListen.length; i++) {
const event = toString(toListen[i])
if (!this.events[event]) {
this.events[event] = []
}
this.events[event].push(fn)
}
return this
}
/**
* Dispatch a event
* @param {string} eventName name of event
* @param {mixed} data data for send when the event is executed
* @return {Psmitter} current Psmitter instance
* @api public
*/
Psmitter.prototype.emit = function (eventName, data) {
eventName = toString(eventName)
if (!this.events[eventName]) {
return false
}
const funcs = this.events[eventName]
for (let i = 0; i < funcs.length; i++) {
const fn = funcs[i] || function () {}
if (isFunction(fn)) {
fn(data)
} else {
fn.fn(data)
funcs.splice(i, 1)
i--
}
}
return this
}
/**
* Register a listener that will be executed only once
* @param {string, array} eventName event's to listen
* @param {Function} fn callback function to execute
* @return {[type]} current Psmitter instance
* @api public
*/
Psmitter.prototype.once = function (eventName, fn) {
return this.on(eventName, { fn: fn })
}
/**
* Get a list of event names in the current Psmitter instance
* @return {array} array of event names
* @api public
*/
Psmitter.prototype.getEvents = function () {
return Object.keys(this.events)
}
/**
* Get a listeners list bind to determinate event
* @param {string} eventName name of event
* @return {array} list of listeners
* @api public
*/
Psmitter.prototype.getListeners = function (eventName) {
eventName = toString(eventName)
if (!this.events[eventName]) {
return []
}
const clearedListeners = this.events[eventName].map(function (fn) {
if (!isFunction(fn)) {
return fn.fn
}
return fn
})
return clearedListeners
}
/**
* Number of listeners registered to an event
* @param {string} eventName name of event
* @return {integer} number of listeners
* @api public
*/
Psmitter.prototype.countListeners = function (eventName) {
eventName = toString(eventName)
if (!this.events[eventName]) {
return false
}
return this.events[eventName].length
}
/**
* Remove an listener of a determinate event
* @param {string} eventName name of event
* @param {Function} listener callback to remove
* @return {Psmitter} current Psmitter instance
* @api public
*/
Psmitter.prototype.removeListener = function (eventName, listener) {
eventName = toString(eventName)
if (this.events[eventName]) {
let funcs = this.events[eventName]
for (let i = 0; i < funcs.length; i++) {
if (!isFunction(funcs[i])) {
funcs[i] = funcs[i].fn
}
if (funcs[i] === listener) {
funcs.splice(i, 1)
}
}
}
return this
}
/**
* Remove all listeners of a event
* @param {string} eventName name of event
* @return {Psmitter} current Psmitter instance
* @api public
*/
Psmitter.prototype.removeAllListeners = function (eventName) {
delete this.events[toString(eventName)]
}
/**
* Remove the last listener of an event
* @param {string} eventName name of event
* @api public
*/
Psmitter.prototype.removeLastListener = function (eventName) {
eventName = toString(eventName)
if (this.events[eventName]) {
this.events[eventName].length > 1 ? this.events[eventName].pop() : this.removeAllListeners(eventName)
}
}
/**
* Check if exists almost one listener registered to a event
* @param {string} eventName name of event
* @return {boolean} return `true` if exists some listener registered, otherwise `false`
* @api public
*/
Psmitter.prototype.hasSomeListener = function (eventName) {
eventName = toString(eventName)
return !!this.events[eventName] && this.events[eventName].length > 0
}
/**
* Check if value is function
* @param {mixed} fn some value for check
* @return {boolean} return `true` if value is a function otherwise return `false`
* @api private
*/
function isFunction (fn) {
return {}.toString.call(fn) === '[object Function]'
}
/**
* Convert any value to string
* @param {mixed} value any param
* @return {string} return the value in string format
* @api private
*/
function toString (value) {
return `${value}`
}
/**
* Psmitter instance
* check if already exist a Psmitter instance otherwise create one
*/
if (typeof define === 'function' && define.amd) {
define(function () {
return Psmitter
})
} else {
context.Psmitter = Psmitter
}
})(this)