Bug 1285373 P1 Allow addon-sdk unloader to hold callbacks weakly. r=gabor

This commit is contained in:
Ben Kelly 2016-07-20 06:47:23 -07:00
parent 499efe0f10
commit 104a1642b5

View File

@ -10,16 +10,33 @@ module.metadata = {
"stability": "experimental"
};
const { Cu } = require('chrome');
const { on, off } = require('./events');
const unloadSubject = require('@loader/unload');
const observers = [];
const unloaders = [];
var when = exports.when = function when(observer) {
if (observers.indexOf(observer) != -1)
return;
observers.unshift(observer);
function WeakObserver(inner) {
this._inner = Cu.getWeakReference(inner);
}
Object.defineProperty(WeakObserver.prototype, 'value', {
get: function() { this._inner.get() }
});
var when = exports.when = function when(observer, opts) {
opts = opts || {};
for (var i = 0; i < observers.length; ++i) {
if (observers[i] === observer || observers[i].value === observer) {
return;
}
}
if (opts.weak) {
observers.unshift(new WeakObserver(observer));
} else {
observers.unshift(observer);
}
};
var ensure = exports.ensure = function ensure(obj, destructorName) {
@ -55,7 +72,12 @@ var ensure = exports.ensure = function ensure(obj, destructorName) {
function unload(reason) {
observers.forEach(function(observer) {
try {
observer(reason);
if (observer instanceof WeakObserver) {
observer = observer.value;
}
if (typeof observer === 'function') {
observer(reason);
}
}
catch (error) {
console.exception(error);