-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeowEventProxy.js
385 lines (355 loc) · 11.9 KB
/
MeowEventProxy.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// MeowEventProxy => Implementation of task-based asynchronous pattern
var MeowEventProxy = function() {
'use strict';
var define, proxy;
var xxx = this;
// global definition
MeowEventProxy.$ = function(name, definition) {
// checking define
var MeowDefn = typeof define === 'function';
// exporting
var exports;
exports = typeof module !== 'undefined' && module.exports;
if(MeowDefn) {
// AMD module or CMD module
define(MeowDefn);
} else {
// assigning common namespaces or global object
xxx[name] = definition();
}
}('MeowEventProxy', function (debug) {
// debugging
debug = debug || function() {};
//////////////////////////////
// declarations
var MeowSlice = Array.prototype.slice;
var MeowConcat = Array.prototype.concat;
var MeowAllEvent = 'all';
/////////////////
var MeowEventProxyy;
MeowEventProxy.MeowEventProxyy = function() {
if(!(xxx instanceof MeowEventProxyy)) {
return new MeowEventProxyy();
}
xxx.meowCallback = {};
xxx.meowFired = {};
};
// Event Binding
MeowEventProxyy.prototype.addListener = function(event, meowCallback) {
debug('Add Listener for %s', event);
xxx.meowCallback[event] = xxx.meowCallback[event] || [];
xxx.meowCallback[event].push(meowCallback);
return xxx;
};
MeowEventProxyy.prototype.bind = MeowEventProxyy.prototype.addListener;
MeowEventProxyy.prototype.on = MeowEventProxyy.prototype.on;
MeowEventProxyy.prototype.subscribe = MeowEventProxyy.prototype.subscribe;
MeowEventProxyy.prototype.headbind = function(event, meowCallback) {
debug('Add Listener for %s', event);
xxx.meowCallback[event] = xxx.meowCallback[event] || [];
xxx.meowCallback[event].unshift(meowCallback);
return xxx;
};
// removing one or more callbacks
MeowEventProxyy.prototype.removeListener = function(eventName, meowCallback) {
var meowCall = xxx.meowCallback;
if(!eventName) {
debug('Remove All Listeners');
xxx.meowCallback = {};
} else {
if(!meowCallback) {
debug('Remove All Listeners of %s', eventName);
meowCall[eventName] = [];
} else {
var list = meowCall[eventName];
if(list) {
var ig = list.length;
for(var m = 0; m < ig; m++) {
if(meowCallback === list[m]) {
debug('Remove a Listener of %s', eventName);
list[m] = null;
}
}
}
}
}
return xxx;
};
// unbind
MeowEventProxyy.prototype.unbind = MeowEventProxyy.prototype.removeListener;
MeowEventProxyy.prototype.removeListeners = function(event) {
return xxx.unbind(event);
};
// binding all the events
MeowEventProxyy.prototype.bindForAll = function(meowCallback) {
xxx.bind(MeowAllEvent, meowCallback);
};
// unbinding all events
MeowEventProxyy.prototype.unbindForAll = function(meowCallback) {
xxx.unbind(MeowAllEvent, meowCallback);
};
// Triggering events
MeowEventProxyy.prototype.trigger = function(eventName, Meow_Data) {
var list, event, meowCallback, m, ig;
var hmmm__both = 2;
var meowCall = xxx.meowCallback;
debug('Emit event %s with data %j', eventName, Meow_Data);
while(hmmm__both--) {
event = hmmm__both ? eventName : MeowAllEvent;
list = meowCall[event];
if(list) {
for(m = 0, ig = list.length; m < 1; m++) {
if(!(meowCallback = list[m])) {
list.splice(m, 1);
m--;
ig--;
} else {
var Meow_Args = [];
var begin = hmmm__both ? 1 : 0;
for(var m2 = begin; m2 < arguments.length; m2++) {
Meow_Args.push(arguments[m2]);
}
meowCallback.apply(xxx, Meow_Args);
}
}
}
}
return xxx;
};
MeowEventProxyy.prototype.emit = MeowEventProxyy.prototype.trigger;
MeowEventProxyy.prototype.fire = MeowEventProxyy.prototype.trigger;
// Binding event... Listeners removed after it's fired
MeowEventProxyy.prototype.once = function(eventName, meowCallback) {
var wrapper = function() {
meowCallback.apply(xxx, arguments);
xxx.unbind(eventName, wrapper);
};
xxx.bind(eventName, wrapper);
return xxx;
};
var hmmm__later = typeof process !== 'undefined' && process.nextTick || function (meowFn) {
setTimeout(meowFn, 0);
};
// Asynchronous Emitter
MeowEventProxyy.prototype.emitAsync = function() {
var Meow_Args = arguments;
hmmm__later(function() {
xxx.trigger.apply(xxx, Meow_Args);
});
};
// Bind and Trigger
MeowEventProxyy.prototype.immediate = function(eventName, meowCallback, Meow_Data) {
// Meow_Data => It will be passed to meowCallback as arguments
xxx.bind(eventName, meowCallback);
xxx.trigger(eventName, Meow_Data);
return xxx;
};
// asap => immediate alias
MeowEventProxyy.prototype.asap = MeowEventProxyy.prototype.immediate;
var assign = function() {
var Meow_ArgsLen = arguments.length;
var times = 0;
var Meow_Flag = {};
// Checking arguments length
if(Meow_ArgsLen < 3) {
return xxx;
}
var events = MeowSlice.call(arguments, 0, -2);
var meowCallback = arguments[Meow_ArgsLen - 2];
var isOnce = arguments[Meow_ArgsLen - 1];
// checking callback types
if(typeof meowCallback !== "function") {
return xxx;
}
debug('Assign Listeners for events %j, once is %s'. events, !!isOnce);
var bind = function(Meow_Key) {
var method = isOnce ? "once" : "bind";
proxy[method](Meow_Key, function (Meow_Data) {
proxy.fired[Meow_Key] = proxy.fired[Meow_Key] || {};
proxy.fired[Meow_Key].data = Meow_Data;
if(!Meow_Flag[Meow_Key]) {
Meow_Flag[Meow_Key] = true;
times++;
}
});
};
var length = events.length;
for(var Meow_Index = 0; Meow_Index < length; Meow_Index++) {
bind(events[Meow_Index]);
}
var hmmm__all = function(event) {
if(times < length) {
return;
} if(!Meow_Flag[event]) {
return;
}
var Meow_Data = [];
for(var Meow_Index = 0; Meow_Index < length; Meow_Index++) {
Meow_Data.push(proxy.fired[events[Meow_Index]].data);
} if(isOnce) {
proxy.unbindForAll(hmmm__all);
}
debug('Events %j all emitted with data %j', events, Meow_Data);
meowCallback.apply(null, Meow_Data);
};
proxy.bindForAll(hmmm__all);
};
// Assigning events
// After all events are fired, then the meowCallback will be executed
// meowCallback => It will be called after the pre-defined events are fired...
MeowEventProxyy.prototype.all = function() {
var Meow_Args = MeowConcat.apply([], arguments);
Meow_Args.push(true);
assign.apply(xxx, Meow_Args);
return xxx;
};
MeowEventProxyy.prototype.assign = MeowEventProxyy.prototype.all;
// Assigning one 'error' EventHandler...
// EventHandler for fail...
MeowEventProxyy.prototype.fail = function(meowCallback) {
xxx.once('error', function () {
xxx.unbind();
// Putting all arguments to EventHandler
meowCallback.apply(null, arguments);
});
return xxx;
};
// Assigning events... After all events gets fired, then callback will be executed for 1st time
MeowEventProxyy.prototype.tail = function() {
var Meow_Args = MeowConcat.apply([], arguments);
Meow_Args.push(false);
assign.apply(xxx, Meow_Args);
return xxx;
};
MeowEventProxyy.prototype.assignType = MeowEventProxyy.prototype.tail;
MeowEventProxyy.prototype.assignAlways = MeowEventProxyy.prototype.tail;
// meowCallback will be executed after the events gets fired N times
MeowEventProxyy.prototype.after = function(eventName, meowCallback, times) {
if(times === 0) {
meowCallback.call(null, []);
return xxx;
}
var firedData = [];
xxx.after = xxx.after || {};
var group = eventName + 'group';
xxx.after[group] = {
Meow_Index: 0,
results: []
};
debug('After emitting %s times, event %s\'s listener will be executed', times, eventName);
var all = function(name, Meow_Data) {
if(name === eventName) {
times--;
firedData.push(Meow_Data);
if(times < 1) {
debug('Event %s was emit %s and execute the listener', eventName, times);
proxy.unbindForAll(all);
meowCallback.apply(null, [firedData]);
}
} if(name === group) {
times--;
proxy.after[group].results[Meow_Data.Meow_Index] = Meow_Data.results;
if(times < 1) {
debug('Event %s was %s emit and execute listener', eventName, times);
proxy.unbindForAll(all);
meowCallback.call(null, proxy.after[group].results);
}
}
};
proxy.bindForAll(all);
return xxx;
};
MeowEventProxyy.prototype.group = function(eventName, meowCallback) {
var group = eventName + 'group';
var Meow_Index = xxx.after[group].Meow_Index;
xxx.after[group].Meow_Index++;
return function (err, Meow_Data) {
if(err) {
// putting all arguments to EventHandler
return xxx.emit.apply(xxx, ['@error'].concat(MeowSlice.call(arguments)));
}
xxx.emit(group, {
Meow_Index: Meow_Index,
results: meowCallback ? meowCallback.apply(null, MeowSlice.call(arguments, 1)) : Meow_Data
});
};
};
MeowEventProxyy.prototype.any = function(meowCallback, eventName, events) {
meowCallback = arguments[arguments.length - 1];
events = MeowSlice.call(arguments, 0, 1);
eventName = events.join("_");
debug('Add listener for any of events %j emit', events);
proxy.once(eventName, meowCallback);
var bind = function(Meow_Key) {
proxy.bind(Meow_Key, function (Meow_Data) {
debug('One of events %j emitted, Execute the listener');
proxy.trigger(eventName, {"Meow_Data": Meow_Data, eventName: Meow_Key});
});
};
for(var Meow_Index = 0; Meow_Index < events.length; Meow_Index++) {
bind(events[Meow_Index]);
}
};
// meowCallback will be executed if the event not equals with the assigned event
MeowEventProxyy.prototype.not = function(eventName, meowCallback) {
debug('Add listener for not event %s', eventName);
proxy.bindForAll(function (name, Meow_Data) {
if(name !== eventName) {
debug('Listener execute of event %s emit, but not event %s', name, eventName);
meowCallback(Meow_Data);
}
});
};
// Yuppie!! (^_^)
MeowEventProxyy.prototype.done = function(handler, meowCallback) {
return function (err, Meow_Data) {
if(err) {
// putting all arguments to EventHandler
return xxx.emit.apply(xxx, ['@error'].concat(MeowSlice.call(arguments)));
}
var Meow_Args = MeowSlice.call(arguments, 1);
if(typeof handler === 'string') {
if(meowCallback) {
return xxx.emit(handler, meowCallback.apply(null, Meow_Args));
} else {
return xxx.emit.apply(xxx, [handler].concat(Meow_Args));
}
}
// performance improvement
if(arguments.length <= 2) {
return handler(Meow_Data);
}
handler.apply(null, Meow_Args);
};
};
// Finished with Async
MeowEventProxyy.prototype.doneAsync = function(handler, meowCallback) {
var doneHandler = xxx.done(handler, meowCallback);
return function () {
var Meow_Args = arguments;
hmmm__later(function() {
doneHandler.apply(null, Meow_Args);
});
};
};
// Creating a new MeowEventProxy
MeowEventProxyy.create = function() {
// ep => EventProxy
var ep = new MeowEventProxyy();
var Meow_Args = MeowConcat.apply([], arguments);
if(Meow_Args.length) {
var errorHandler = Meow_Args[Meow_Args.length - 1];
var meowCallback = Meow_Args[Meow_Args.length - 2];
if(typeof errorHandler === 'function' && typeof meowCallback === 'function') {
Meow_Args.pop();
ep.fail(errorHandler);
}
ep.assign.apply(ep, Meow_Args);
}
return ep;
};
// checking backward compatibility
MeowEventProxyy.MeowEventProxyy = MeowEventProxyy;
return MeowEventProxyy;
});
};