Skip to content

Instantly share code, notes, and snippets.

@jmoyers
Created June 3, 2011 19:02
Show Gist options
  • Save jmoyers/1006950 to your computer and use it in GitHub Desktop.
Save jmoyers/1006950 to your computer and use it in GitHub Desktop.
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