1 /* See license.txt for terms of usage */
  2 
  3 // ********************************************************************************************* //
  4 // Constants
  5 
  6 const Cc = Components.classes;
  7 const Ci = Components.interfaces;
  8 const Cr = Components.results;
  9 const Cu = Components.utils;
 10 
 11 var EXPORTED_SYMBOLS = ["fbObserverService"];
 12 
 13 Cu.import("resource://firebug/fbtrace.js");
 14 
 15 // ********************************************************************************************* //
 16 // Observer implementation
 17 
 18 /**
 19  * @service meta service module for observers
 20  * See also: <a href="https://developer.mozilla.org/en/NsIObserverService">
 21  * nsIObserverService</a>
 22  */
 23 var fbObserverService =
 24 /** lends fbObserverService */
 25 {
 26     observersByTopic: {},
 27 
 28     /* nsIObserverService */
 29     addObserver: function(observer, topic, weak)
 30     {
 31         if (!this.observersByTopic[topic])
 32             this.observersByTopic[topic] = [];
 33 
 34         this.observersByTopic[topic].push(observer);
 35     },
 36 
 37     removeObserver: function(observer, topic)
 38     {
 39         var observers = this.observersByTopic[topic];
 40         if (!observers)
 41             throw new Error("observer-service.removeObserver FAILED no observers for topic "+topic);
 42 
 43         for (var i=0; i < observers.length; i++)
 44         {
 45             if (observers[i] == observer)
 46             {
 47                 observers.splice(i, 1);
 48                 return;
 49             }
 50         }
 51 
 52         throw new Error("observer-service.removeObserver FAILED (no such observer) for topic "+topic);
 53     },
 54 
 55     notifyObservers: function(subject, topic, data)
 56     {
 57         var observers = this.observersByTopic[topic];
 58         if (observers)
 59         {
 60             for (var i=0; i < observers.length; i++)
 61                 observers[i].observe(subject, topic, data);
 62         }
 63     },
 64 
 65     enumerateObservers: function(topic, fnOfObserver)
 66     {
 67         var observers = this.observersByTopic[topic];
 68         if (fnOfObserver)
 69         {
 70             for (var i=0; i < observers.length; i++)
 71                 fnOfObserver(observers[i]);
 72         }
 73         return observers;  // may be null or array
 74     },
 75 
 76     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 77     // For debugging observer registration
 78 
 79     stackForTrack: [],
 80 
 81     track: function(stack)
 82     {
 83         this.stackForTrack.push(stack.toString());
 84         return this.stackForTrack.length;
 85     },
 86 
 87     untrack: function(index)
 88     {
 89         if (this.stackForTrack[index - 1])
 90         {
 91             delete this.stackForTrack[index - 1];
 92         }
 93         else
 94         {
 95             Components.reportError("observer-service. ERROR attempt to untrack item not tracked at " +
 96                 (index - 1));
 97         }
 98     },
 99 
100     getStacksForTrack: function()
101     {
102         return this.stackForTrack;
103     },
104 
105     traceStacksForTrack: function()
106     {
107         if (!FBTrace.DBG_OBSERVERS)
108             return;
109 
110         var result = false;
111         for (var i=0; i<this.stackForTrack.length; i++)
112         {
113             if (this.stackForTrack[i])
114             {
115                 result = true;
116                 break;
117             }
118         }
119 
120         if (result)
121         {
122             FBTrace.sysout("fbObserverService getStacksForTrack ", this.stackForTrack);
123         }
124     }
125 };
126 
127 // ********************************************************************************************* //
128