Unbinding Events In Node.js
Let's take stdin.on as an example. Callbacks to stdin.on stack, so if I write (in CoffeeScript) stdin = process.openStdin() stdin.setEncoding 'utf8' stdin.on 'data', (input) ->
Solution 1:
You can use removeListener(eventType, callback)
to remove an event, which should work with all kinds of emitters.
Example from the API docs:
var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
So you need to have a variable that holds a reference to the callback, because obviously, it's otherwise impossible to tell which callback you want to have removed.
EDIT Should be someone like this in CS:
stdin = process.openStdin()
stdin.setEncoding 'utf8'
logger = (input) -> console.log'One'stdin.on 'data', logger
stdin.removeListener 'data', logger
stdin.on 'data', (input) -> console.log'Two'
See: http://nodejs.org/docs/latest/api/events.html#emitter.removeListener
Solution 2:
Or you can use:
stdin.once
instead of stdin.on
Post a Comment for "Unbinding Events In Node.js"