Skip to content

Instantly share code, notes, and snippets.

@mmckegg
Created November 12, 2015 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmckegg/97ea6c703de76b0a6818 to your computer and use it in GitHub Desktop.
Save mmckegg/97ea6c703de76b0a6818 to your computer and use it in GitHub Desktop.
module.exports = bindWatch
function bindWatch (obs, handler) {
if (typeof handler === 'function') {
return obs(handler.bind(obs))
} else if (handler instanceof Object) {
var removeListeners = Object.keys(handler).map(watchKey, {
obs: obs,
handlers: handler
})
return function unwatch () {
while (removeListeners.length) {
removeListeners.pop()()
}
}
}
}
function watchKey (key) {
var obs = this.obs[key]
var handler = this.handlers[key]
if (typeof obs === 'function' && typeof handler === 'function') {
return obs(handler.bind(obs))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment