Last active
August 29, 2015 14:19
-
-
Save finnp/0a047b9e348ea33b4cc6 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
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 | |
} |
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
// 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