-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomm.js
168 lines (145 loc) · 4.51 KB
/
comm.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
/**
* This object provides a standard interface for the addon and the app to talk
* to each other. Although in a chrome addon you can call functions in other
* parts of the addon directly, since the FF addon isn't set up this way, it's
* better to just use the same standard interface: message passing. This is more
* "correct" anyway (just ask the erlang guys, they'll tell you).
*
* Note that because in many instances this object is used globally (ie, one
* Comm for the entire background page), it has a feature Firefox (which uses
* ports for each panel/tab/etc) lacks which is "contexts." I can bind a
* callback to an object (a tab, a string, whatever) and later on I can clear
* all bindings attached to that context with one function call (vs trying to
* clear out all the bindings by hand).
*/
var Comm = (function() {
// holds all event <--> callback bindings
var bindings = {};
// matches contexts with binding pairs
var contexts = {};
/**
* Bind a callback (function) to an event (string). Optionally allows
* binding event/cb pairs to a context object, which allows all bindings
* under that context to be cleared at once via Comm.unbind_context()
*/
var bind = function(event, cb, context)
{
context || (context = false);
// init the bindings for this event, if needed
if(!bindings[event]) bindings[event] = [];
// if we have a context, set the event/cb pair into it
if(context)
{
if(!contexts[context]) contexts[context] = [];
contexts[context].push([event, cb]);
}
// try to prevent double-bindings
if(bindings[event].indexOf(cb) >= 0) return true;
// binding doesn't exist! create it
bindings[event].push(cb);
};
/**
* Clean context entries of the given event and (optional) cb.
*/
var clean_contexts = function(event, cb)
{
Object.each(contexts, function(bindings, context) {
bindings = bindings.filter(function(binding) {
var bind_event = binding[0];
var bind_cb = binding[1];
if(bind_event == event && !cb) return false;
if(bind_event == event && bind_cb == cb) return false;
return true;
});
contexts[context] = bindings;
});
};
/**
* Unbind an event/callback pair. If callback isn't given, all events of
* type `event` are unbound. If event *and* allback aren't passed then
* unbind all bindings (empty slate).
*/
var unbind = function(event, cb)
{
// if event is blank, unbind everything
if(!event)
{
bindings = {};
contexts = {};
return true;
}
// if callback is blank, unbind all of the given event's bindings
if(!cb)
{
if(bindings[event]) delete bindings[event];
clean_contexts(event);
return true;
}
// remove the event/callback pair (if found)
if(bindings[event])
{
var idx = bindings[event].indexOf(cb);
if(idx < 0) return false;
bindings[event].splice(idx, 1);
if(bindings[event].length == 0) delete bindings[event];
clean_contexts(event, cb);
return true;
}
return false;
};
/**
* Unbind all bindings associated with the given context
*/
unbind_context = function(context)
{
var bindings = contexts[context];
if(!bindings) return false;
Array.clone(bindings).each(function(binding) {
var bind_event = binding[0];
var bind_cb = binding[1];
unbind(bind_event, bind_cb);
});
return true;
};
/**
* Trigger an event (asynchronously), passing all the given arguments (sans
* even name) to the callbacks bound to that event type.
*/
var trigger = function(event, _args)
{
// clone the actuments, pop off the event name
var args = Array.prototype.slice.call(arguments, 0)
args.shift();
// send an event ALWAYS triggered on any trigger event
if(event != 'all') trigger('all', event, args);
var callbacks = bindings[event]
if(!callbacks || callbacks.length == 0) return true;
// clone the callbacks (so if they change while firing we don't get
// weird loop corruptions)
callbacks = callbacks.slice(0);
// remember, triggers are async
(function() {
for(var i = 0, n = callbacks.length; i < n; i++)
{
var cb = callbacks[i];
cb.apply(this, args);
}
}).delay(0, this);
return true;
};
var num_bindings = function()
{
var num_bindings = 0;
Object.each(bindings, function(callbacks, event) {
num_bindings += callbacks.length;
});
return num_bindings;
};
this.bind = bind;
this.unbind = unbind;
this.unbind_context = unbind_context;
this.trigger = trigger;
this.num_bindings = num_bindings;
this._bindings = function() { return bindings; };
this._context = function() { return contexts; };
});