|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Listener{ |
|
|
|
|
|
constructor(wl){ // wl = white list |
|
|
|
|
|
this.__listeners = {}; |
|
|
|
|
|
this.__IsAllowed = (name) => { |
|
|
|
|
|
if (wl !== null) |
|
|
|
|
|
return (wl.length > 0) ? (wl.indexOf(name) >= 0) : true; |
|
|
|
|
|
return false; |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
on(name, fn){ |
|
|
|
|
|
if (this.__IsAllowed(name)){ |
|
|
|
|
|
if (!(name in this.__listeners)){ |
|
|
|
|
|
this.__listeners[name] = []; |
|
|
|
|
|
} |
|
|
|
|
|
this.__listeners[name].push(fn); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
trigger(){ |
|
|
|
|
|
if (arguments.length > 0){ |
|
|
|
|
|
let name = arguments[0]; |
|
|
|
|
|
if (name in this.__listeners){ |
|
|
|
|
|
let args = Array.prototype.slice.call(arguments); |
|
|
|
|
|
this.__listeners[name].forEach((fn) => { |
|
|
|
|
|
fn.apply(null, args); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class IO{ |
|
|
|
|
|
constructor(rl, wl){ |
|
|
|
|
|
rl = (rl === null || (rl instanceof Array)) ? rl : []; |
|
|
|
|
|
wl = (wl === null || (wl instanceof Array)) ? wl : []; |
|
|
|
|
|
this.__rlisteners = (rl !== null) ? (new Listener(rl)) : null; |
|
|
|
|
|
this.__wlisteners = (wl !== null) ? (new Listener(wl)) : null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onRead(name, fn){ |
|
|
|
|
|
this.__rlisteners.on(name, fn); |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onWrite(name, fn){ |
|
|
|
|
|
this.__wlisteners.on(name, fn); |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onRW(name, fn){ |
|
|
|
|
|
this.__rlisteners.on(name, fn); |
|
|
|
|
|
this.__wlisteners.on(name, fn); |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
triggerRead(){ |
|
|
|
|
|
this.__rlisteners.apply(this.__rlisteners, Array.prototype.slice.call(arguments)); |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
triggerWrite(){ |
|
|
|
|
|
this.__wlisteners.apply(this.__wlisteners, Array.prototype.slice.call(arguments)); |
|
|
|
|
|
return this; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = IO; |