dispatcher.js
3.89 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
// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*-
// vim:set ft=javascript ts=2 sw=2 sts=2 cindent:
// TODO: does 'arguments.callee.caller' work?
var Dispatcher = (function($, window, undefined) {
var Dispatcher = function() {
var that = this;
var table = {};
var on = function(message, host, handler) {
if (handler === undefined) {
handler = host;
host = arguments.callee.caller;
}
if (table[message] === undefined) {
table[message] = [];
}
table[message].push([host, handler]);
return this;
};
// Notify listeners that we encountered an error in an asynch call
var inAsynchError = false; // To avoid error avalanches
var handleAsynchError = function(e) {
if (!inAsynchError) {
inAsynchError = true;
// TODO: Hook printout into dispatch elsewhere?
console.warn('Handled async error:', e);
that.post('dispatchAsynchError', [e]);
inAsynchError = false;
} else {
console.warn('Dropped asynch error:', e);
}
};
var post = function(asynch, message, args, returnType) {
if (typeof(asynch) !== 'number') {
// no asynch parameter
returnType = args;
args = message;
message = asynch;
asynch = null;
}
if (args === undefined) {
args = [];
}
var results = [];
// DEBUG: if (typeof(message) != "string" || !(message.match(/mouse/) || message == "hideComment")) console.log(message, args);
if (typeof(message) === 'function') {
// someone was lazy and sent a simple function
var host = arguments.callee.caller;
if (asynch !== null) {
result = setTimeout(function() {
try {
message.apply(host, args);
} catch(e) {
handleAsynchError(e);
}
}, asynch);
} else {
result = message.apply(host, args);
}
results.push(result);
} else {
// a proper message, propagate to all interested parties
var todo = table[message];
if (todo !== undefined) {
$.each(todo, function(itemNo, item) {
var result;
if (asynch !== null) {
result = setTimeout(function() {
try {
item[1].apply(item[0], args);
} catch (e) {
handleAsynchError(e);
}
}, asynch);
} else {
result = item[1].apply(item[0], args);
}
results.push(result);
});
/* DEBUG
} else {
console.warn('Message ' + message + ' has no subscribers.'); // DEBUG
*/
}
}
if (returnType == 'any') {
var i = results.length;
while (i--) {
if (results[i] !== false) return results[i];
}
return false;
}
if (returnType == 'all') {
var i = results.length;
while (i--) {
if (results[i] === false) return results[i];
}
}
return results;
};
var proxy = function(destination, message) {
this.on(message, function() {
destination.post(message, Array.prototype.slice.call(arguments));
});
};
var dispatcher = {
on: on,
post: post,
proxy: proxy,
};
Dispatcher.dispatchers.push(dispatcher);
return dispatcher;
};
Dispatcher.dispatchers = [];
Dispatcher.post = function(asynch, message, args, returnType) {
$.each(Dispatcher.dispatchers, function(dispatcherNo, dispatcher) {
dispatcher.post(asynch, message, args, returnType);
});
};
return Dispatcher;
})(jQuery, window);