forked from RIAEvangelist/event-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event-pubsub-browser-es5.js
124 lines (100 loc) · 2.7 KB
/
event-pubsub-browser-es5.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
'use strict';
window.EventPubSub=function EventPubSub() {
this._events_={};
this.publish=this.trigger=this.emit=emit;
this.subscribe=this.on=on;
this.unSubscribe=this.off=off;
this.emit$=emit$;
function on(type,handler){
if(!handler){
const err=new ReferenceError('handler not defined.');
throw(err);
}
if(!this._events_[type]){
this._events_[type]=[];
}
this._events_[type].push(handler);
return this;
}
function off(type,handler){
if(!this._events_[type]){
return this;
}
if(!handler){
var err=new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler');
throw err;
}
if(handler=='*'){
delete this._events_[type];
return this;
}
const handlers=this._events_[type];
while(handlers.includes(handler)){
handlers.splice(
handlers.indexOf(handler),
1
);
}
if(handlers.length<1){
delete this._events_[type];
}
return this;
}
function emit(type){
this.emit$.apply(this, arguments);
if(!this._events_[type]){
return this;
}
arguments.splice=Array.prototype.splice;
arguments.splice(0,1);
const handlers=this._events_[type];
for(let handler of handlers){
handler.apply(this, arguments);
}
return this;
}
function emit$(type, args){
if(!this._events_['*']){
return this;
}
const catchAll=this._events_['*'];
args.shift=Array.prototype.shift;
args.shift(type);
for(let handler of catchAll){
handler.apply(this, args);
}
return this;
}
return this;
}
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.includes called on null or undefined');
}
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
};
}