public
Created

indentation

  • Download Gist
ekho.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//// Object Ekho
var Ekho = Base.clone({
__init__:
function _init(parent, thing) {
this.listeners = { }
this.base = thing
this.parent = parent
return this },
 
listeners:
function listeners(event) {
return this.listeners[event]
|| (this.listeners[event] = []) },
 
listen:
function listen(event, handler, filter) {
handler = make_handler(handler, filter, [])
this.listeners(event).push(handler)
return handler },
 
tap:
function tap(origin, method, handler, filter) {
handler = make_handler(handler, filter, [])
 
origin[method].listen? origin[method].listen('*call*', handler)
: /* plain function? */ origin[method] = make_ekho(wrapper, this)
 
return handler
function wrapper() {
result = origin[method].apply(origin, arguments)
wrapper.shout.apply(wrapper, ['*call*'].concat(result))
return result }},
 
once:
function once(handler) { var real_fn
real_fn = handler.fn
handler.fn = function(event) {
real_fn.apply(this, arguments)
return DROP }
 
return handler },
 
deafen:
function deafen(event, handler) { var self
self = this
 
event? this.listeners[event] = filter()
: each(this.listeners, clean)
 
return this
 
function keepp(hd) { return handler && handler !== hd }
function filter() { return self.listeners(event).filter(keepp) }
function clean(ev) { return self.deafen(ev, handler) }},
 
shout:
function shout(event) {
args = slice.call(arguments, 1)
event = make_event(this, event, args)
 
args.unshift(event)
this.listeners[event.type] = filter(this.listeners(event.type))
 
if (!event.handled) event.bubble()
return this
 
function keepp(value) { return value !== DROP }
function filter(hooks){ var result
result = []
hooks.every(function(hd) {
if (keepp(hd.exec.apply(hd, args))) result.push(hd)
return !event.halted })
return result }}
})

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.