Is there a actual style guide for Add-on Module Classes? I am trying to bridge the gap between three documentation files:
There is no example of a Add-on Module that returns a Class and I just wanted to make sure that I am using the correct style. I am not blocked, but I don’t want to author a bunch of Modules and then find out they are incorrectly formatted.
What I was expecting, but does not work (based on Node.js docs and the Add-on Modules (non-Class) doc):
// urlrequestobserver.js
const {Cc, Ci} = require("chrome");
var UrlRequestObserver = exports.UrlRequestObserver = function UrlRequestObserver() {
this.register();
};
UrlRequestObserver.prototype.register = function register() {
this.observerService.addObserver(this, "http-on-opening-request", false);
};
// ...
What works but syntactically looks very different then non-Class modules and thus feels rather inconsistent.
// Listen for any requests to the container XML and add it to the GUID list in the extension pop-up
const {Class} = require('sdk/core/heritage');
const {Cc, Ci} = require("chrome");
var UrlRequestObserver = exports.UrlRequestObserver = Class({
initialize: function () {
this.register();
},
register: function () {
this.observerService.addObserver(this, "http-on-opening-request", false);
}
//...
});
My unit test…
// test/test-urlrequestobserver.js
var { UrlRequestObserver } = require("../urlrequestobserver");
exports["test require UrlRequestObserver"] = function(assert) {
assert.ok((UrlRequestObserver), "UrlRequestObserver can be require().");
};
exports["test newing up UrlRequestObserver"] = function(assert) {
var instance = new UrlRequestObserver();
assert.ok((instance), "UrlRequestObserver is a factory.");
};
Cheers,
H