1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-23 18:45:52 +01:00

WeakMaps ftw.

This commit is contained in:
Kris Maglione
2012-10-21 17:49:24 -07:00
parent 88b3f6c835
commit 7d28f98ade

View File

@@ -48,6 +48,8 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
util.addObserver(this); util.addObserver(this);
this.overlays = {}; this.overlays = {};
this.weakMap = WeakMap();
this.onWindowVisible = []; this.onWindowVisible = [];
}, },
@@ -153,30 +155,28 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
}, },
getData: function getData(obj, key, constructor) { getData: function getData(obj, key, constructor) {
let { id } = this;
if (!(id in obj && obj[id])) if (!this.weakMap.has(obj))
obj[id] = {}; this.weakMap.set(obj, {});
let data = this.weakMap.get(obj);
if (arguments.length == 1) if (arguments.length == 1)
return obj[id]; return data;
if (obj[id][key] === undefined) if (data[key] === undefined)
if (constructor === undefined || callable(constructor)) if (constructor === undefined || callable(constructor))
obj[id][key] = (constructor || Array)(); data[key] = (constructor || Array)();
else else
obj[id][key] = constructor; data[key] = constructor;
return obj[id][key]; return data[key];
}, },
setData: function setData(obj, key, val) { setData: function setData(obj, key, val) {
let { id } = this; let data = this.getData(obj);
if (!(id in obj)) return data[key] = val;
obj[id] = {};
return obj[id][key] = val;
}, },
overlayWindow: function (url, fn) { overlayWindow: function (url, fn) {