|
| 1 | +// -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; -*- |
| 2 | +// vim:set ft=javascript ts=2 sw=2 sts=2 cindent: |
| 3 | +// TODO: does 'arguments.callee.caller' work? |
| 4 | + |
| 5 | +var Dispatcher = (function($, window, undefined) { |
| 6 | + var Dispatcher = function() { |
| 7 | + var that = this; |
| 8 | + |
| 9 | + var table = {}; |
| 10 | + |
| 11 | + var on = function(message, host, handler) { |
| 12 | + if (handler === undefined) { |
| 13 | + handler = host; |
| 14 | + host = arguments.callee.caller; |
| 15 | + } |
| 16 | + if (table[message] === undefined) { |
| 17 | + table[message] = []; |
| 18 | + } |
| 19 | + table[message].push([host, handler]); |
| 20 | + return this; |
| 21 | + }; |
| 22 | + |
| 23 | + // Notify listeners that we encountered an error in an asynch call |
| 24 | + var inAsynchError = false; // To avoid error avalanches |
| 25 | + var handleAsynchError = function(e) { |
| 26 | + if (!inAsynchError) { |
| 27 | + inAsynchError = true; |
| 28 | + // TODO: Hook printout into dispatch elsewhere? |
| 29 | + console.warn('Handled async error:', e); |
| 30 | + that.post('dispatchAsynchError', [e]); |
| 31 | + inAsynchError = false; |
| 32 | + } else { |
| 33 | + console.warn('Dropped asynch error:', e); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + var post = function(asynch, message, args, returnType) { |
| 38 | + if (typeof(asynch) !== 'number') { |
| 39 | + // no asynch parameter |
| 40 | + returnType = args; |
| 41 | + args = message; |
| 42 | + message = asynch; |
| 43 | + asynch = null; |
| 44 | + } |
| 45 | + if (args === undefined) { |
| 46 | + args = []; |
| 47 | + } |
| 48 | + var results = []; |
| 49 | + // DEBUG: if (typeof(message) != "string" || !(message.match(/mouse/) || message == "hideComment")) console.log(message, args); |
| 50 | + |
| 51 | + if (typeof(message) === 'function') { |
| 52 | + // someone was lazy and sent a simple function |
| 53 | + var host = arguments.callee.caller; |
| 54 | + if (asynch !== null) { |
| 55 | + result = setTimeout(function() { |
| 56 | + try { |
| 57 | + message.apply(host, args); |
| 58 | + } catch(e) { |
| 59 | + that.handleAsynchError(e); |
| 60 | + } |
| 61 | + }, asynch); |
| 62 | + } else { |
| 63 | + result = message.apply(host, args); |
| 64 | + } |
| 65 | + results.push(result); |
| 66 | + } else { |
| 67 | + // a proper message, propagate to all interested parties |
| 68 | + var todo = table[message]; |
| 69 | + if (todo !== undefined) { |
| 70 | + $.each(todo, function(itemNo, item) { |
| 71 | + var result; |
| 72 | + if (asynch !== null) { |
| 73 | + result = setTimeout(function() { |
| 74 | + try { |
| 75 | + item[1].apply(item[0], args); |
| 76 | + } catch (e) { |
| 77 | + that.handleAsynchError(e); |
| 78 | + } |
| 79 | + }, asynch); |
| 80 | + } else { |
| 81 | + result = item[1].apply(item[0], args); |
| 82 | + } |
| 83 | + results.push(result); |
| 84 | + }); |
| 85 | +/* DEBUG |
| 86 | + } else { |
| 87 | + console.warn('Message ' + message + ' has no subscribers.'); // DEBUG |
| 88 | +*/ |
| 89 | + } |
| 90 | + } |
| 91 | + if (returnType == 'any') { |
| 92 | + var i = results.length; |
| 93 | + while (i--) { |
| 94 | + if (results[i] !== false) return results[i]; |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (returnType == 'all') { |
| 99 | + var i = results.length; |
| 100 | + while (i--) { |
| 101 | + if (results[i] === false) return results[i]; |
| 102 | + } |
| 103 | + } |
| 104 | + return results; |
| 105 | + }; |
| 106 | + |
| 107 | + var proxy = function(destination, message) { |
| 108 | + this.on(message, function() { |
| 109 | + destination.post(message, Array.prototype.slice.call(arguments)); |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + var dispatcher = { |
| 114 | + on: on, |
| 115 | + post: post, |
| 116 | + proxy: proxy, |
| 117 | + }; |
| 118 | + Dispatcher.dispatchers.push(dispatcher); |
| 119 | + return dispatcher; |
| 120 | + }; |
| 121 | + |
| 122 | + Dispatcher.dispatchers = []; |
| 123 | + Dispatcher.post = function(asynch, message, args, returnType) { |
| 124 | + $.each(Dispatcher.dispatchers, function(dispatcherNo, dispatcher) { |
| 125 | + dispatcher.post(asynch, message, args, returnType); |
| 126 | + }); |
| 127 | + }; |
| 128 | + |
| 129 | + return Dispatcher; |
| 130 | +})(jQuery, window); |
0 commit comments