Created
May 30, 2012 20:01
-
-
Save gregglind/2838598 to your computer and use it in GitHub Desktop.
observer v. observer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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