Created
February 6, 2016 17:56
-
-
Save lmammino/bb80e7e86c4178d97918 to your computer and use it in GitHub Desktop.
Read only event emitter in Node
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
"use strict"; | |
const ticker = require('./ticker'); | |
ticker.on('tick', (tickCount) => console.log(tickCount, 'TICK')); | |
// ticker.emit('something', {}); <-- This will fail |
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
"use strict"; | |
const EventEmitter = require('events'); | |
module.exports = class Roee extends EventEmitter { | |
constructor (executor) { | |
super(); | |
var emit = this.emit.bind(this); | |
this.emit = undefined; | |
executor(emit); | |
} | |
}; |
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
"use strict"; | |
const Roee = require('./roee'); | |
const ticker = new Roee((emit) => { | |
let tickCount = 0; | |
setInterval(() => emit('tick', tickCount++), 1000); | |
}); | |
module.exports = ticker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment