Skip to content

Commit af9179a

Browse files
committed
Publish dist and src.
1 parent e6fb8dc commit af9179a

File tree

3 files changed

+154
-3
lines changed

3 files changed

+154
-3
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.DS_Store
22
node_modules
3-
npm-debug.log
4-
dist
3+
npm-debug.log

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
src
21
node_modules
32
babel.js
43
browserify.js

dist/index.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"use strict";
2+
3+
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
4+
5+
exports.__esModule = true;
6+
/**
7+
* Creates a new instance of Emitter.
8+
* @class
9+
* @returns {Object} Returns a new instance of Emitter.
10+
* @example
11+
* // Creates a new instance of Emitter.
12+
* var Emitter = require('emitter');
13+
*
14+
* var emitter = new Emitter();
15+
*/
16+
17+
var Emitter = (function () {
18+
function Emitter() {
19+
_classCallCheck(this, Emitter);
20+
}
21+
22+
/**
23+
* Adds a listener to the collection for the specified event.
24+
* @memberof! Emitter.prototype
25+
* @function
26+
* @param {String} event - The event name.
27+
* @param {Function} listener - A listener function to add.
28+
* @returns {Object} Returns an instance of Emitter.
29+
* @example
30+
* // Add an event listener to "foo" event.
31+
* emitter.on('foo', listener);
32+
*/
33+
34+
Emitter.prototype.on = function on(event, listener) {
35+
// Use the current collection or create it.
36+
this._eventCollection = this._eventCollection || {};
37+
38+
// Use the current collection of an event or create it.
39+
this._eventCollection[event] = this._eventCollection[event] || [];
40+
41+
// Appends the listener into the collection of the given event
42+
this._eventCollection[event].push(listener);
43+
44+
return this;
45+
};
46+
47+
/**
48+
* Adds a listener to the collection for the specified event that will be called only once.
49+
* @memberof! Emitter.prototype
50+
* @function
51+
* @param {String} event - The event name.
52+
* @param {Function} listener - A listener function to add.
53+
* @returns {Object} Returns an instance of Emitter.
54+
* @example
55+
* // Will add an event handler to "foo" event once.
56+
* emitter.once('foo', listener);
57+
*/
58+
59+
Emitter.prototype.once = function once(event, listener) {
60+
var self = this;
61+
62+
function fn() {
63+
self.off(event, fn);
64+
listener.apply(this, arguments);
65+
}
66+
67+
fn.listener = listener;
68+
69+
this.on(event, fn);
70+
71+
return this;
72+
};
73+
74+
/**
75+
* Removes a listener from the collection for the specified event.
76+
* @memberof! Emitter.prototype
77+
* @function
78+
* @param {String} event - The event name.
79+
* @param {Function} listener - A listener function to remove.
80+
* @returns {Object} Returns an instance of Emitter.
81+
* @example
82+
* // Remove a given listener.
83+
* emitter.off('foo', listener);
84+
*/
85+
86+
Emitter.prototype.off = function off(event, listener) {
87+
88+
var listeners = undefined;
89+
90+
// Defines listeners value.
91+
if (!this._eventCollection || !(listeners = this._eventCollection[event])) {
92+
return this;
93+
}
94+
95+
listeners.forEach(function (fn, i) {
96+
if (fn === listener || fn.listener === listener) {
97+
// Removes the given listener.
98+
listeners.splice(i, 1);
99+
}
100+
});
101+
102+
// Removes an empty event collection.
103+
if (listeners.length === 0) {
104+
delete this._eventCollection[event];
105+
}
106+
107+
return this;
108+
};
109+
110+
/**
111+
* Execute each item in the listener collection in order with the specified data.
112+
* @memberof! Emitter.prototype
113+
* @function
114+
* @param {String} event - The name of the event you want to emit.
115+
* @param {...Object} data - Data to pass to the listeners.
116+
* @returns {Object} Returns an instance of Emitter.
117+
* @example
118+
* // Emits the "foo" event with 'param1' and 'param2' as arguments.
119+
* emitter.emit('foo', 'param1', 'param2');
120+
*/
121+
122+
Emitter.prototype.emit = function emit(event) {
123+
var _this = this;
124+
125+
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
126+
args[_key - 1] = arguments[_key];
127+
}
128+
129+
var listeners = undefined;
130+
131+
// Defines listeners value.
132+
if (!this._eventCollection || !(listeners = this._eventCollection[event])) {
133+
return this;
134+
}
135+
136+
// Clone listeners
137+
listeners = listeners.slice(0);
138+
139+
listeners.forEach(function (fn) {
140+
return fn.apply(_this, args);
141+
});
142+
143+
return this;
144+
};
145+
146+
return Emitter;
147+
})();
148+
149+
/**
150+
* Exports Emitter
151+
*/
152+
exports["default"] = Emitter;
153+
module.exports = exports["default"];

0 commit comments

Comments
 (0)