Created
November 12, 2015 09:44
-
-
Save mmckegg/97ea6c703de76b0a6818 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
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