-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
93 lines (84 loc) · 2.5 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
(function (global, factory) {
"object" === typeof exports ? module.exports = factory() :
"function" === typeof define && define.amd ? define(factory) :
global.Happens = factory();
}(this, function () {
'use strict'
/**
* Module constructor
* @param {Object} target Target object to extends methods and properties into
* @return {Object} Target after with extended methods and properties
*/
function Happens(target){
var nu = this instanceof Happens;
if(target){
if(!nu)
for(var prop in Happens.prototype)
target[prop] = Happens.prototype[prop];
else
throw new Error("You can't pass a target when instantiating with the `new` keyword");
}
else if(!nu)
return new Happens
};
/**
* Initializes event
* @param {String} event Event name to initialize
* @return {Array} Initialized event pool
*/
Happens.prototype.__init = function(event) {
var tmp = this.__listeners || (this.__listeners = []);
return tmp[event] || (tmp[event] = []);
};
/**
* Adds listener
* @param {String} event Event name
* @param {Function} fn Event handler
*/
Happens.prototype.on = function(event, fn) {
validate(fn);
this.__init(event).push(fn);
};
/**
* Removes listener
* @param {String} event Event name
* @param {Function} fn Event handler
*/
Happens.prototype.off = function(event, fn) {
var pool = this.__init(event);
pool.splice(pool.indexOf(fn), 1);
};
/**
* Add listener the fires once and auto-removes itself
* @param {String} event Event name
* @param {Function} fn Event handler
*/
Happens.prototype.once = function(event, fn) {
validate(fn);
var self = this, wrapper = function() {
self.off(event, wrapper);
fn.apply(this, arguments);
};
this.on(event, wrapper );
};
/**
* Emit some event
* @param {String} event Event name -- subsequent params after `event` will
* be passed along to the event's handlers
*/
Happens.prototype.emit = function(event /*, arg1, arg2 */ ) {
var i, pool = this.__init(event).slice(0);
for(i in pool)
pool[i].apply(this, [].slice.call(arguments, 1));
};
/**
* Validates if a function exists and is an instanceof Function, and throws
* an error if needed
* @param {Function} fn Function to validate
*/
function validate(fn) {
if(!(fn && fn instanceof Function))
throw new Error(fn + ' is not a Function');
}
return Happens;
}));