Skip to content

Instantly share code, notes, and snippets.

@finnp
Last active August 29, 2015 14:19
Show Gist options
  • Save finnp/0a047b9e348ea33b4cc6 to your computer and use it in GitHub Desktop.
Save finnp/0a047b9e348ea33b4cc6 to your computer and use it in GitHub Desktop.
var EventEmitter = require('events').EventEmitter
// my current implementation
module.exports = function (obj) {
var calls = new EventEmitter()
for(methodName in obj) {
if (typeof obj[methodName] !== 'function') continue
var originalMethod = obj[methodName]
obj[methodName] = (function (methodName, originalMethod) {
return function() {
calls.emit
.bind(calls, methodName)
.apply(calls, arguments)
return originalMethod.apply(obj, arguments)
}
})(methodName, originalMethod)
}
return calls
}
// context is a 2d canvas context
var oncall = require('./oncall.js')
var calls = oncall(context)
calls.on('fillRect', function(x, y, width, height) {
console.log(context.fillStyle) // 'blue'
console.log(x, y, width, height) // 0, 0, 200, 200
})
fakeContext.fillStyle = 'blue'
fakeContext.fillRect(0, 0, 200, 200)
fakeContext.fillStyle = 'green'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment