Skip to content

Instantly share code, notes, and snippets.

@gregglind
Created May 30, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregglind/2838598 to your computer and use it in GitHub Desktop.
Save gregglind/2838598 to your computer and use it in GitHub Desktop.
observer v. observer
var log = function(){
let A = Array.prototype.slice.call(arguments).join(" ");
dump(Date.now() +":" + A + "\n");
};
if (console !==undefined ) log = console.log;
var timers = require('timers');
var { Class } = require("api-utils/heritage");
var { Unknown } = require('api-utils/xpcom');
var { Cc, Ci } = require('chrome')
var observerService = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
// Create my observer exemplar
var StarObserver = Class({
extends: Unknown,
interfaces: [ 'nsIObserver' ], // Interfaces component implements
topic: '*',
initialize: function(fn) { this.fn = fn },
register: function register() {
observerService.addObserver(this, this.topic, false);
},
unregister: function() {
addObserver.removeObserver(this, this.topic, false);
},
observe: function observe(subject, topic, data) {
this.fn({
subject: subject,
type: topic,
data: data
});
}
});
// create instances of observers
let starobserver = StarObserver(function(event) {
log("star observer:", JSON.stringify(event));
});
// register observer
starobserver.register();
/* Using the wrapped observer service */
var observer = require("observer-service");
log("about to notify");
timers.setInterval(function(){
observer.notify("MYMESSAGE",{'some':'data'});
},500);
observer.add("*",function(subject,data){log('from * obserser-service',JSON.stringify(subject),JSON.stringify(data))});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment