Created
June 18, 2009 15:02
-
-
Save weepy/131938 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Hooks */ | |
var Hooks = function() { | |
this.list = {}; | |
this.hookTable = {}; | |
this.hookNum = 0; | |
} | |
Hooks.prototype = { | |
add: function(names, fn, priority, off) { | |
if(typeof names != "string") | |
console.log("Cannot add type as hook: " + n) | |
var ns = names.split(",") | |
for(var i in ns) { | |
if( this.list[ns[i]] ) | |
console.log("warn", "replacing hook: ", ns[i]) | |
this.list[ns[i]] = { name: ns[i], fn: fn, priority: priority == null ? 10 : priority, off: off, num: (this.hookNum++) }; | |
} | |
}, | |
remove: function(event) { | |
this.hookTable[event] = [] | |
}, | |
getOnHooks: function(event, param, options) { | |
var hooks = this.hookTable[event] = this.hookTable[event] || this.calcHooks(event); | |
var onhooks = [] | |
for(var i in hooks) { | |
if(!hooks[i].off) { | |
onhooks.push(hooks[i]) | |
if(this.logging) | |
console.log("about to fire hook: ", hooks[i].name, "from args:", [event, param, options]) | |
} | |
} | |
return onhooks | |
}, | |
fire: function(event, param, options) { | |
// var event = (event instanceof Array) ? event : [event] | |
// var hooks = [] | |
// for(var i in event) | |
// hooks.push( this.getOnHooks(event[i], param, options) ) | |
var hooks = this.getOnHooks(event, param, options) | |
for(var i in hooks) | |
hooks[i].fn(param, options) | |
}, | |
calcHooks: function(event) { | |
var hooks = [] | |
for(var i in this.list) { | |
var hook = this.list[i]; | |
var hk = hook.name.split(":"); | |
var ev = event.split(":"); | |
var add = true | |
for(var i in ev) { | |
if(hk[i] != ev[i]) { | |
add = false | |
break | |
} | |
} | |
if(add) | |
hooks.push(hook) | |
} | |
// if(hooks.length == 0) | |
// console.("Warning: no hooks found for event: " + event) | |
hooks.sort(function(a,b) { | |
return (a.priority - b.priority) || (a.num - b.num); //Chrome v1 seems to have slightly different sorting ?! | |
}) | |
return hooks; | |
} | |
}; | |
/* | |
var hooks = new Hooks() | |
hooks.add("Event", function() { console.log("Event fired with default priority") }) | |
hooks.add("Event:subevent", function() { console.log("Event:subevent fired with default priority") }) | |
hooks.fire("Event") | |
=> "Event fired with default priority" | |
=> "Event:subevent fired with default priority" | |
hooks.fire("Event:subevent") | |
=> "Event:subevent fired with default priority" | |
hooks.add("Event", function() { console.log("Event fired with high priority") }, 1) | |
hooks.add("Event", function() { console.log("Event fired with low priority") }, 100) | |
hooks.list["Event:subevent"].off = true | |
hooks.fire("Event") | |
=> "Event fired with high priority" | |
=> "Event fired with default priority" | |
=> "Event fired with low priority" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment