Skip to content

Instantly share code, notes, and snippets.

@lmammino
Created February 6, 2016 17:56
Show Gist options
  • Save lmammino/bb80e7e86c4178d97918 to your computer and use it in GitHub Desktop.
Save lmammino/bb80e7e86c4178d97918 to your computer and use it in GitHub Desktop.
Read only event emitter in Node
"use strict";
const ticker = require('./ticker');
ticker.on('tick', (tickCount) => console.log(tickCount, 'TICK'));
// ticker.emit('something', {}); <-- This will fail
"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);
}
};
"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