-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
135 lines (119 loc) · 2.98 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
/* eslint-disable */
/**
* @fileOverview
* asEvented v0.4.3 - an event emitter mixin which provides
* the observer pattern to JavaScript object.
*
* Copyright 2012, Michal Kuklis
* Dual licensed under the MIT or GPL Version 2 licenses.
*
**/
/*
* Originally from https://github.com/mkuklis/asEvented
*
* Changelog:
* - removed UMD wrapper
* - do not export `bind` or `unbind`
*/
var ArrayProto = Array.prototype;
var nativeIndexOf = ArrayProto.indexOf;
var slice = ArrayProto.slice;
/**
* By default, we should simply log the error to the console.
*/
function defaultErrorHandler(e) {
global.console.error(e);
}
/**
* Initialize a custom error handler for this instance, which may decide to log or even re-throw the error.
*
* @param {Function} fn - the custom callback to be invoked whenever an error is
* detected as the result of a trigger event
*/
function setErrorHandler(fn) {
this.eventErrorHandler = fn;
}
function bind(event, fn) {
var i, part;
var events = this.events = this.events || {};
var parts = event.split(/\s+/);
var num = parts.length;
for (i = 0; i < num; i++) {
events[(part = parts[i])] = events[part] || [];
events[part].push(fn);
}
return this;
}
function one(event, fn) {
// [notice] The value of fn and fn1 is not equivalent in the case
// of the following in MSIE.
//
// var fn = function fn1 () { alert(fn === fn1) } ie.<9 false
var fnc = function () {
this.off(event, fnc);
fn.apply(this, slice.call(arguments));
};
this.on(event, fnc);
return this;
}
function unbind(event, fn) {
var eventName, i, index, num, parts;
var events = this.events;
if (!events) {
return;
}
parts = event.split(/\s+/);
for (i = 0, num = parts.length; i < num; i++) {
if ((eventName = parts[i]) in events !== false) {
index = (fn) ? _indexOf(events[eventName], fn) : 0;
if (index !== -1) {
events[eventName].splice(index, 1);
}
}
}
return this;
}
function trigger(event) {
var args, i;
var events = this.events;
if (!events || event in events === false) {
return;
}
args = slice.call(arguments, 1);
for (i = events[event].length - 1; i >= 0; i--) {
try {
events[event][i].apply(this, args);
}
catch (e) {
(this.eventErrorHandler || defaultErrorHandler).call(this, e, {
event: event,
data: args
});
}
}
return this;
}
// TODO: This is the same as the inArray method provided
// by the utils module. This should be replaced in favor
// of using that.
function _indexOf(array, needle) {
var i, l;
if (nativeIndexOf && array.indexOf === nativeIndexOf) {
return array.indexOf(needle);
}
for (i = 0, l = array.length; i < l; i++) {
if (array[i] === needle) {
return i;
}
}
return -1;
}
module.exports = function () {
this.on = bind;
this.off = unbind;
this.trigger = this.emit = trigger;
this.one = this.once = one;
this.setErrorHandler = setErrorHandler;
return this;
};
/* eslint-enable */