-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevent.js
More file actions
168 lines (144 loc) · 3.77 KB
/
event.js
File metadata and controls
168 lines (144 loc) · 3.77 KB
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
var eventEmitter = function() {
/**
* If opt_func is given, returns an instance of the being mixed class.
*/
function eventEmitter(opt_func) {
return opt_func ? new (mixin(opt_func, EventEmitter))
: new EventEmitter;
}
function mixin(subject, var_objs) {
var proto, i, p, len = arguments.length;
for (i = 0; i++ < len - 1;) {
proto = arguments[i].prototype;
for (p in proto) {
subject.prototype[p] = proto[p];
}
}
return subject;
}
function EventEmitter() {
this.events_ = {};
}
EventEmitter.prototype.emit = function(type, var_args) {
if (!this.events_)
EventEmitter.call(this);
var listener = listeners = this.events_[type];
if (!listener)
return false;
else if (typeof listener == 'function') {
switch (arguments.length) {
case 1:
listener.call();
break;
case 2:
listener.call(null, arguments[1]);
break;
case 3:
listener.call(null, arguments[1], arguments[2]);
break;
default:
listener.apply(null, Array.prototype.slice.call(arguments, 1));
}
} else {
var len = listeners.length;
for (var i = 0; i < len; i++)
listeners[i].apply(null, Array.prototype.slice.call(arguments, 1));
}
return true;
};
EventEmitter.prototype.addListener = EventEmitter.prototype.on = function(type, listener) {
if (!this.events_)
EventEmitter.call(this);
if (typeof listener != 'function')
throw TypeError('listener must be a function.');
// prevent creating an array if there is only one listener.
if (!this.events_[type])
this.events_[type] = listener;
else if (typeof this.events_[type] == 'object')
this.events_[type].push(listener);
else
this.events_[type] = [this.events_[type], listener];
return this;
};
EventEmitter.prototype.removeListener = function(type, listener) {
if (!this.events_)
EventEmitter.call(this);
if (typeof listener != 'function')
throw TypeError('listener must be a function.');
if (!this.events_[type])
return this;
var list = this.events_[type],
len = list.length,
pos = -1;
if (list === listener)
delete this.events_[type];
else {
for (var i = len; i-- > 0;) {
if (list[i] === listener) {
pos = i;
break;
}
}
if (pos < 0)
return this;
if (len == 1) {
list.length = 0;
delete this.events_[type];
} else {
list.splice(pos, 1);
}
}
return this;
};
/**
* There are 2 fasions to employ eventEmitter() in users' code:
*
* function Server() {}
*
* 1) var server = eventEmitter(Server);
* server.addListener('onMessage', function() {}).emit('onMessage');
*
* 2) var server = new Server();
* server.onMessage = new eventEmitter.Event('optional name');
* server.onMessage.addListener(function() {}).dispatch();
*
* I order to provide the second usage fasion, the library maintains an
* EventEmitter object as a singleton containing all instances of Event()
* that registers each of themself as a unique event type in the loop.
*/
function EventLoop() {
this.instance_ = undefined;
this.count_ = 0;
}
EventLoop.prototype = {
get: function() {
if (!this.instance_)
this.instance_ = new EventEmitter();
return this.instance_;
},
count: function() { return this.count_++; }
};
function EventWrapper(opt_name) {
this.loop = eventLoop.get();
this.name = opt_name || '_MaayaErikaYukachiEvent_' + eventLoop.count();
}
EventWrapper.prototype = {
addListener: function(listener) {
this.loop.addListener(this.name, listener);
return this;
},
removeListener: function(listener) {
this.loop.removeListener(this.name, listener);
return this;
},
dispatch: function(var_args) {
var args = Array.prototype.slice.call(arguments);
args.unshift(this.name);
return this.loop.emit.apply(this.loop, args);
}
};
var eventLoop = new EventLoop();
eventEmitter.Event = EventWrapper;
eventEmitter.EventEmitter = EventEmitter;
return eventEmitter;
}();