-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
158 lines (139 loc) · 3.62 KB
/
index.js
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
'use strict';
function Jvent() {}
/**
* Adds a listener to the collection for a specified event.
* @public
* @function
* @name Jvent#on
* @param {string} event Event name.
* @param {function} listener Listener function.
* @example
* // Will add a event listener to the "ready" event
* var startDoingStuff = function (event, param1, param2, ...) {
* // Some code here!
* };
*
* me.on("ready", startDoingStuff);
*/
Jvent.prototype.on = function(event, listener) {
this._collection = this._collection || {};
this._collection[event] = this._collection[event] || [];
this._collection[event].push(listener);
return this;
};
/**
* Adds a one time listener to the collection for a specified event. It will execute only once.
* @public
* @function
* @name Jvent#once
* @param {string} event Event name.
* @param {function} listener Listener function.
* @returns itself
* @example
* // Will add a event handler to the "contentLoad" event once
* me.once("contentLoad", startDoingStuff);
*/
Jvent.prototype.once = function (event, listener) {
var that = this;
function fn() {
that.off(event, fn);
listener.apply(this, arguments);
}
fn.listener = listener;
this.on(event, fn);
return this;
};
/**
* Removes a listener from the collection for a specified event.
* @public
* @function
* @name Jvent#off
* @param {string} event Event name.
* @param {function} listener Listener function.
* @returns itself
* @example
* // Will remove event handler to the "ready" event
* var startDoingStuff = function () {
* // Some code here!
* };
*
* me.off("ready", startDoingStuff);
*/
Jvent.prototype.off = function (event, listener) {
var listeners = this._collection && this._collection[event],
j = 0;
if (listeners !== undefined) {
for (j; j < listeners.length; j += 1) {
if (listeners[j] === listener || listeners[j].listener === listener) {
listeners.splice(j, 1);
break;
}
}
if (listeners.length === 0) {
this.removeAllListeners(event);
}
}
return this;
};
/**
* Removes all listeners from the collection for a specified event.
* @public
* @function
* @name Jvent#removeAllListeners
* @param {string} event Event name.
* @returns itself
* @example
* me.removeAllListeners("ready");
*/
Jvent.prototype.removeAllListeners = function (event) {
this._collection = this._collection || {};
delete this._collection[event];
return this;
};
/**
* Returns all listeners from the collection for a specified event.
* @public
* @function
* @name Jvent#listeners
* @param {string} event Event name.
* @returns Array
* @example
* me.listeners("ready");
*/
Jvent.prototype.listeners = function (event) {
this._collection = this._collection || {};
return this._collection[event];
};
/**
* Execute each item in the listener collection in order with the specified data.
* @name Jvent#emit
* @public
* @protected
* @param {string} event The name of the event you want to emit.
* @param {...object} var_args Data to pass to the listeners.
* @example
* // Will emit the "ready" event with "param1" and "param2" as arguments.
* me.emit("ready", "param1", "param2");
*/
Jvent.prototype.emit = function () {
if (this._collection === undefined) {
return this;
}
var args = [].slice.call(arguments, 0), // converted to array
event = args.shift(),
listeners = this._collection[event],
i = 0,
len;
if (listeners) {
listeners = listeners.slice(0);
len = listeners.length;
for (i; i < len; i += 1) {
listeners[i].apply(this, args);
}
}
return this;
};
/**
* Expose Jvent
*/
module.exports = Jvent;