1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-01-08 10:04:12 +01:00

Hashes are better than arrays

This commit is contained in:
Kris Maglione
2009-01-02 20:47:46 -05:00
parent f36b293e0d
commit f4b8edbb67

View File

@@ -57,11 +57,14 @@ const liberator = (function () //{{{
run: function () { this.func.apply(this.self, this.args); } run: function () { this.func.apply(this.self, this.args); }
}; };
var callbacks = []; const callbacks = {};
var observers = []; const observers = {};
function registerObserver(type, callback) function registerObserver(type, callback)
{ {
observers.push([type, callback]); if (!(type in observers))
observers[type] = [];
observers[type].push(callback);
} }
let nError = 0; let nError = 0;
@@ -628,19 +631,15 @@ const liberator = (function () //{{{
// TODO: move to ui.js? // TODO: move to ui.js?
registerCallback: function (type, mode, func) registerCallback: function (type, mode, func)
{ {
// TODO: check if callback is already registered if (!(type in callbacks))
callbacks.push([type, mode, func]); callbacks[type] = {};
callbacks[type][mode] = func;
}, },
triggerCallback: function (type, mode, data) triggerCallback: function (type, mode, data)
{ {
// liberator.dump("type: " + type + " mode: " + mode + "data: " + data + "\n"); if (callbacks[type] && callbacks[type][mode])
for (let i = 0; i < callbacks.length; i++) return callbacks[type][mode].call(this, data);
{
let [thistype, thismode, thisfunc] = callbacks[i];
if (mode == thismode && type == thistype)
return thisfunc.call(this, data);
}
return false; return false;
}, },
@@ -648,16 +647,14 @@ const liberator = (function () //{{{
unregisterObserver: function (type, callback) unregisterObserver: function (type, callback)
{ {
observers = observers.filter(function ([t, c]) t != type || c != callback); if (type in observers)
observers[type] = observers[type].filter(function (c) c != callback);
}, },
triggerObserver: function (type) triggerObserver: function (type)
{ {
for (let [,[thistype, callback]] in Iterator(observers)) for (let [,fn] in Iterator(observers[type] || []))
{ fn.apply(null, Array.slice(arguments, 1));
if (thistype == type)
callback.apply(null, Array.slice(arguments, 1));
}
}, },
beep: function () beep: function ()