Last active
August 29, 2015 14:02
-
-
Save bpostlethwaite/ba0380cecd68c5e95c38 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
// for units that are owned by a particular player | |
function clientEmitter (game, unitId, clientId) { | |
var ev = new EventEmitter() | |
var cachedEmit = ev.emit | |
// we need to override the event emitters emit function to put our own juice in there | |
ev.emit = function (msg, data) { | |
// first lets emit this stuff to the client, including whatever data | |
// is being sent. (we can always not send the data too). | |
// the controller on the client will get 'unitMessage' events | |
// and will have to check the args to do the right things | |
game.emit(clientId, 'unit-message', unitId, msg, data) | |
// now we need to call the underlying emitters emit method, so we | |
// call the cached function (since we overwrote ev.emit) but we also | |
// have to rebind this floating method to the ev object. So we use | |
// the function.call(this, argument1, argument2 [, ...]) pattern. | |
cachedEmit.call(ev, msg, data) | |
} | |
// so now we have replaced the emitters emit function with our own | |
// get our business done, then hook back into the real event emitters | |
// emit machinery so it's 'on' method will get called | |
return ev | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment