forked from libin1991/webpack4-vue-react-more-page-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES6-eventbus.js
176 lines (161 loc) · 4.93 KB
/
ES6-eventbus.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class EventBus {
constructor() {
this.events = {};
}
on(type, callback, scope, ...args) {
if (typeof this.events[type] == "undefined") {
this.events[type] = [];
}
this.events[type].push({scope: scope, callback: callback, args: args});
}
off(type, callback, scope) {
if (typeof this.events[type] == "undefined") {
return;
}
let numOfCallbacks = this.events[type].length;
let newArray = [];
for (let i = 0; i < numOfCallbacks; i++) {
let event = this.events[type][i];
if (event.scope == scope && event.callback == callback) {
} else {
newArray.push(event);
}
}
this.events[type] = newArray;
}
has(type, callback, scope) {
if (typeof this.events[type] == "undefined") {
return false;
}
let numOfCallbacks = this.events[type].length;
if (callback === undefined && scope === undefined) {
return numOfCallbacks > 0;
}
for (let i = 0; i < numOfCallbacks; i++) {
let event = this.events[type][i];
if ((scope ? event.scope == scope : true) && event.callback == callback) {
return true;
}
}
return false;
}
emit(type, target, ...args) {
if (typeof this.events[type] == "undefined") {
return;
}
let bag = {
type: type,
target: target
};
args = [bag].concat(args);
let events = this.events[type].slice();
let numOfCallbacks = events.length;
for (let i = 0; i < numOfCallbacks; i++) {
let event = events[i];
if (event && event.callback) {
let concatArgs = args.concat(event.args);
event.callback.apply(event.scope, concatArgs);
}
}
}
debug() {
let str = "";
for (let type in this.events) {
let numOfCallbacks = this.events[type].length;
for (let i = 0; i < numOfCallbacks; i++) {
let event = this.events[type][i];
let className = "Anonymous";
if (event.scope) {
if (event.scope.constructor.name) {
className = event.scope.constructor.name;
}
}
str += `${className} listening for "${type}"\n`;
}
}
return str;
}
};
export default new EventBus();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EventBus Examples</title>
</head>
<body>
<script type="module">
import EventBus from '/src/eventbus.js';
// Simple example
{
function myHandler(event) {
console.log("myHandler / type: " + event.type);
}
EventBus.on("my_event", myHandler);
EventBus.emit("my_event");
}
// Keeping the scope
{
class TestClass1 {
constructor() {
this.className = "TestClass1";
EventBus.on("callback_event", this.callback, this);
}
callback(event) {
console.log(this.className + " / type: " + event.type + " / dispatcher: " + event.target.className);
}
}
class TestClass2 {
constructor() {
this.className = "TestClass2";
}
dispatch() {
EventBus.emit("callback_event", this);
}
}
let t1 = new TestClass1();
let t2 = new TestClass2();
t2.dispatch();
}
// Passing additional parameters
{
class TestClass1 {
constructor() {
this.className = "TestClass1";
EventBus.on("custom_event", this.doSomething, this);
}
doSomething(event, param1, param2) {
console.log(this.className + ".doSomething");
console.log("type=" + event.type);
console.log("params=" + param1 + param2);
console.log("coming from=" + event.target.className);
}
}
class TestClass2 {
constructor() {
this.className = "TestClass2";
}
ready() {
EventBus.emit("custom_event", this, "javascript events", " are really useful");
}
}
let t1 = new TestClass1();
let t2 = new TestClass2();
t2.ready();
}
// Debugging
console.log(EventBus.debug());
// Removing a registered handler
{
var handler = function() {
console.log('example callback');
};
EventBus.on('EXAMPLE_EVENT', handler);
EventBus.emit('EXAMPLE_EVENT');
EventBus.off('EXAMPLE_EVENT', handler);
// Not emitted because event was removed
EventBus.emit('EXAMPLE_EVENT');
}
</script>
</body>
</html>