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
|
What have the EventEmitter framework ever done for us?
######################################################
:date: 2011-08-31T14:08:10
:category: computer
:tags: jetpack, firefox, bugTriage
So, I was one of the people behind `the bug request`_ which lead to `the
EventEmitter framework`_.
So, when I have now a moment I was looking at my scripts how to make to
use it. The situation before was that I had this ``pageMod`` creator::
pageMod.PageMod({
include : interestingURLsArray,
contentScriptWhen : 'ready',
contentScriptFile : contentScriptLibraries,
onAttach : function onAttach(worker, msg) {
worker.on('message', function(msg) {
messageHandler(worker, msg);
});
});
and ``messageHandler`` handler was just one very ugly
``switch(msg.cmd)`` which contained sections like::
case "GetURL":
libbz.getURL(msg.data.url,
function(stuff) {
worker.postMessage(new Message(msg.data.backMessage,
stuff));
});
break;
Not nice but useable. All the ugly switching logic was hidden behind the
corner and it was obvious what it does. Now with the advent of
``EventEmitter`` framework I should change my pageMod’s ``onAttach``
handler to contain endless list of very ugly spaghetti calls like::
worker.port.on('GetURL', function (command) {
libbz.getURL(command.url,
function(stuff) {
worker.port.emit(command.backMessage,
stuff);
});
});
What is the advantage? `What have the EventEmitter framework ever done
for us?`_ I am not sure I know what is the answer,
I guess we could improve the situation a bit if page.PageMod creator was
actually returning an instance variable and event handlers could be
hooked on it (in the similar pattern as the ``Widget``\ ’s on the
instance variable. Why it isn't possible to do so with PageMod?
.. _`the bug request`:
https://bugzilla.mozilla.org/show_bug.cgi?id=635748
.. _`the EventEmitter framework`:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/api-utils/docs/events.html
.. _`What have the EventEmitter framework ever done for us?`:
https://youtu.be/Qc7HmhrgTuQ
|