Skip to content

Commit

Permalink
Replace EventEmitter with core events (mozilla#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
xabolcs committed Jun 12, 2015
1 parent 45fd7ac commit dce52bb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 97 deletions.
1 change: 0 additions & 1 deletion extension/lib/config.js
Expand Up @@ -19,7 +19,6 @@ const PREFERENCES = {
modified_prefs: 'extensions.' + self.id + '.modifiedPrefs',

// Application preferences
memory_log: 'javascript.options.mem.log',
memory_notify: 'javascript.options.mem.notify'
}

Expand Down
100 changes: 43 additions & 57 deletions extension/lib/garbage-collector.js
Expand Up @@ -5,7 +5,7 @@
"use strict";

const { Cc, Ci, Cu } = require("chrome");
const { EventEmitter } = require("sdk/deprecated/events");
const { emit, on, off } = require("sdk/event/core");
const prefs = require("sdk/preferences/service");
const timers = require("sdk/timers");
const unload = require("sdk/system/unload");
Expand All @@ -14,59 +14,16 @@ const config = require("./config");

Cu.import('resource://gre/modules/Services.jsm');

const reporter = EventEmitter.compose({
_pref_gc_notifications: null,
const PREF_GC_NOTIFICATIONS = config.preferences.memory_notify;

constructor: function Reporter() {
// Report unhandled errors from listeners
this.on("error", console.exception.bind(console));
var _isEnabled;
var reporter = {
name: "CollectorReporter",
pref_gc_notifications: PREF_GC_NOTIFICATIONS
};

// Make sure we clean-up correctly
unload.ensure(this, 'unload');

if (config.application.branch >= 16) {
this._pref_gc_notifications = config.preferences.memory_notify;
}
else {
this._pref_gc_notifications = config.preferences.memory_log;
}

// Ensure GC/CC observer and console messages preference is enabled
this._isEnabled = prefs.get(this._pref_gc_notifications);
if (!this._isEnabled)
this._enable();

Services.obs.addObserver(this, config.application.topic_cc_statistics, false);
Services.obs.addObserver(this, config.application.topic_gc_statistics, false);
},

get pref_gc_notifications() {
return this._pref_gc_notifications;
},

unload: function Reporter_unload() {
this._removeAllListeners();

Services.obs.removeObserver(this, config.application.topic_cc_statistics, false);
Services.obs.removeObserver(this, config.application.topic_gc_statistics, false);
},

_enable: function Reporter__enable() {
var modifiedPrefs = JSON.parse(prefs.get(config.preferences.modified_prefs,
"{}"));
if (!modifiedPrefs.hasOwnProperty(this._pref_gc_notifications)) {
modifiedPrefs[this._pref_gc_notifications] = prefs.get(this._pref_gc_notifications);
}

prefs.set(this._pref_gc_notifications, true);
prefs.set(config.preferences.modified_prefs, JSON.stringify(modifiedPrefs));
this._isEnabled = true;
},

/**
* Callback for GC/CC related observer notifications
*/
observe: function Reporter_observe(aSubject, aTopic, aData) {
var CollectorObserver = {
observe: function CollectorObserver_observe(aSubject, aTopic, aData) {
let data = { };
let type = aTopic;

Expand All @@ -80,13 +37,40 @@ const reporter = EventEmitter.compose({
if ('timestamp' in data)
data['timestamp'] = Math.round(data['timestamp'] / 1000);

// Once the console listener can be removed, we can emit directly
timers.setTimeout(function (aScope) {
aScope._emit(type, data);
}, 0, this);
emit(reporter, type, data);
}
}

var _enable = function () {
var modifiedPrefs = JSON.parse(prefs.get(config.preferences.modified_prefs,
"{}"));
if (!modifiedPrefs.hasOwnProperty(PREF_GC_NOTIFICATIONS)) {
modifiedPrefs[PREF_GC_NOTIFICATIONS] = prefs.get(PREF_GC_NOTIFICATIONS);
}
})();

prefs.set(PREF_GC_NOTIFICATIONS, true);
prefs.set(config.preferences.modified_prefs, JSON.stringify(modifiedPrefs));
_isEnabled = true;
}

var init = function () {
// Ensure GC/CC observer and console messages preference is enabled
_isEnabled = prefs.get(PREF_GC_NOTIFICATIONS);
if (!_isEnabled)
_enable();

reporter.on = on.bind(null, reporter);
reporter.off = off.bind(null, reporter);

Services.obs.addObserver(CollectorObserver, config.application.topic_cc_statistics, false);
Services.obs.addObserver(CollectorObserver, config.application.topic_gc_statistics, false);

unload.when((reason) => {
off(reporter);
Services.obs.removeObserver(CollectorObserver, config.application.topic_cc_statistics, false);
Services.obs.removeObserver(CollectorObserver, config.application.topic_gc_statistics, false);
});
}

/**
* Trigger a Cycle Collector run
Expand All @@ -108,6 +92,8 @@ var doGlobalGC = function () {
Services.obs.notifyObservers(null, "child-gc-request", null);
}

init();


exports.reporter = reporter;

Expand Down
18 changes: 12 additions & 6 deletions extension/lib/main.js
Expand Up @@ -115,7 +115,7 @@ exports.main = function (options, callbacks) {
}
});

memory.reporter.on(config.application.topic_memory_statistics, function (aData) {
function memoryStatisticsForwarder(aData) {
if (gData.current.memory)
gData.previous.memory = gData.current.memory;
gData.current.memory = aData;
Expand All @@ -125,25 +125,31 @@ exports.main = function (options, callbacks) {
// Memory statistics aren't pretty useful yet to be logged
// See: https://github.com/mozilla/memchaser/issues/106
//logger.log(config.application.topic_memory_statistics, aData);
});
}

memory.reporter.on(config.application.topic_memory_statistics, memoryStatisticsForwarder);

garbage_collector.reporter.on(config.application.topic_gc_statistics, function (aData) {
function gcStatisticsForwarder(aData) {
if (gData.current.gc)
gData.previous.gc = gData.current.gc;
gData.current.gc = aData;

widget.postMessage({ type: "update_garbage_collector", data: gData });
logger.log(config.application.topic_gc_statistics, aData);
});
}

garbage_collector.reporter.on(config.application.topic_cc_statistics, function (aData) {
garbage_collector.reporter.on(config.application.topic_gc_statistics, gcStatisticsForwarder);

function ccStatisticsForwarder(aData) {
if (gData.current.cc)
gData.previous.cc = gData.current.cc;
gData.current.cc = aData;

widget.postMessage({ type: "update_cycle_collector", data: gData });
logger.log(config.application.topic_cc_statistics, aData);
});
}

garbage_collector.reporter.on(config.application.topic_cc_statistics, ccStatisticsForwarder);

simple_prefs.on('log.directory', function (aData) {
logger.dir = prefs.get(config.preferences.log_directory);
Expand Down
68 changes: 35 additions & 33 deletions extension/lib/memory.js
Expand Up @@ -5,7 +5,7 @@
"use strict";

const { Cc, Ci, Cu } = require("chrome");
const { EventEmitter } = require("sdk/deprecated/events");
const { emit, on, off } = require("sdk/event/core");
const prefs = require("sdk/preferences/service");
const self = require("sdk/self");
const timer = require("sdk/timers");
Expand All @@ -18,48 +18,50 @@ Cu.import('resource://gre/modules/Services.jsm');
var memSrv = Cc["@mozilla.org/memory-reporter-manager;1"]
.getService(Ci.nsIMemoryReporterManager);

var interval;
var _timer;

const reporter = EventEmitter.compose({
constructor: function Reporter() {
// Report unhandled errors from listeners
this.on("error", console.exception.bind(console));

// Make sure we clean-up correctly
unload.ensure(this, 'unload');

// TODO: Reading the pref should be moved out of this module
this.interval = prefs.get(config.preferences.memory_poll_interval,
config.extension.memory_poll_interval_default);
this._timer = timer.setInterval(function (aScope) { aScope.retrieveStatistics() },
this.interval, this);
},

unload: function Reporter_unload() {
this._removeAllListeners();
timer.clearInterval(this._timer);
},

/**
* Retrieve memory statistics
*/
retrieveStatistics: function Reporter_retrieveStatistics() {
var data = {
timestamp: Date.now(),
//explicit: memSrv.explicit,
resident: memSrv.residentFast
}

this._emit(config.application.topic_memory_statistics, data);

var reporter = {
name: "memoryReporter"
}

/**
* Retrieve memory statistics
*/
reporter.retrieveStatistics = function Reporter_retrieveStatistics() {
var data = {
timestamp: Date.now(),
//explicit: memSrv.explicit,
resident: memSrv.residentFast
}

})();
emit(reporter, config.application.topic_memory_statistics, data);
}

var init = function () {
// TODO: Reading the pref should be moved out of this module
interval = prefs.get(config.preferences.memory_poll_interval,
config.extension.memory_poll_interval_default);
_timer = timer.setInterval(reporter.retrieveStatistics, interval);

reporter.on = on.bind(null, reporter);
reporter.off = off.bind(null, reporter);

// Make sure we clean-up correctly
unload.when ((reason) => {
off(reporter);
timer.clearInterval(_timer);
});
}

var minimizeMemory = function (aCallback) {
Services.obs.notifyObservers(null, "child-mmu-request", null);
memSrv.minimizeMemoryUsage(aCallback);
}

init();


exports.reporter = reporter;

Expand Down

0 comments on commit dce52bb

Please sign in to comment.