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
|
/* See license.txt for terms of usage */
// ********************************************************************************************* //
// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
var EXPORTED_SYMBOLS = ["fbObserverService"];
Cu.import("resource://firebug/fbtrace.js");
// ********************************************************************************************* //
// Observer implementation
/**
* @service meta service module for observers
* See also: <a href="https://developer.mozilla.org/en/NsIObserverService">
* nsIObserverService</a>
*/
var fbObserverService =
/** lends fbObserverService */
{
observersByTopic: {},
/* nsIObserverService */
addObserver: function(observer, topic, weak)
{
if (!this.observersByTopic[topic])
this.observersByTopic[topic] = [];
this.observersByTopic[topic].push(observer);
},
removeObserver: function(observer, topic)
{
var observers = this.observersByTopic[topic];
if (!observers)
throw new Error("observer-service.removeObserver FAILED no observers for topic "+topic);
for (var i=0; i < observers.length; i++)
{
if (observers[i] == observer)
{
observers.splice(i, 1);
return;
}
}
throw new Error("observer-service.removeObserver FAILED (no such observer) for topic "+topic);
},
notifyObservers: function(subject, topic, data)
{
var observers = this.observersByTopic[topic];
if (observers)
{
for (var i=0; i < observers.length; i++)
observers[i].observe(subject, topic, data);
}
},
enumerateObservers: function(topic, fnOfObserver)
{
var observers = this.observersByTopic[topic];
if (fnOfObserver)
{
for (var i=0; i < observers.length; i++)
fnOfObserver(observers[i]);
}
return observers; // may be null or array
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// For debugging observer registration
stackForTrack: [],
track: function(stack)
{
this.stackForTrack.push(stack.toString());
return this.stackForTrack.length;
},
untrack: function(index)
{
if (this.stackForTrack[index - 1])
{
delete this.stackForTrack[index - 1];
}
else
{
Components.reportError("observer-service. ERROR attempt to untrack item not tracked at " +
(index - 1));
}
},
getStacksForTrack: function()
{
return this.stackForTrack;
},
traceStacksForTrack: function()
{
if (!FBTrace.DBG_OBSERVERS)
return;
var result = false;
for (var i=0; i<this.stackForTrack.length; i++)
{
if (this.stackForTrack[i])
{
result = true;
break;
}
}
if (result)
{
FBTrace.sysout("fbObserverService getStacksForTrack ", this.stackForTrack);
}
}
};
// ********************************************************************************************* //
|