Created
June 3, 2011 19:02
-
-
Save jmoyers/1006950 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
getListeners: (channel)-> | |
branches = ['root'].concat(channel.split(@pattern)) | |
numbranches = branches.length | |
curr = @listeners | |
walk = (a, b)-> | |
for v,i in a | |
if a[i] != b[i] then return false | |
return true | |
for branch, bcounter in branches | |
if typeof curr[branch] == 'object' | |
curr = curr[branch] | |
listeners = [] | |
trail = branches.slice(bcounter+1) | |
if branch is @wildcard | |
freaks = [].concat(curr.ghosts).concat(curr.wanderers) | |
if bcounter < (numbranches - 1) | |
for freak in freaks | |
diff = freak.markers.length - trail.length | |
match = diff >= 0 and walk(trail.reverse(), freak.markers.reverse()) | |
if match then listeners.push(freak.listener) | |
return listeners | |
else | |
return _.pluck(freaks, 'listener') | |
else if bcounter == (numbranches - 1) | |
return curr.listeners |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment