Skip to content

Instantly share code, notes, and snippets.

@ocommaj
Created May 21, 2021 14:40
Show Gist options
  • Save ocommaj/3c8d2454ee327faba8ee9a9b2ce1a701 to your computer and use it in GitHub Desktop.
Save ocommaj/3c8d2454ee327faba8ee9a9b2ce1a701 to your computer and use it in GitHub Desktop.
const EventEmitter = require('events')
class TestEmitter extends EventEmitter {
constructor( opts = {} ) {
super(opts)
this.name = opts.name
this.testFireIncrementer = 0
}
testFire() {
this.testFireIncrementer+=1
this.emit('testFire', this.testFireIncrementer)
}
}
const emitter = new TestEmitter({ name: 'testEmitter' })
emitter.timedFire = function timedFire() {
setTimeout(() => emitter.testFire(), 1000)
}
function listener_2(i) {
console.log(`test fire again: ${i}`)
}
emitter.on('testFire', (i) => console.log(`test fire: ${i}`))
emitter.on('testFire', listener_2)
function main() {
emitter.timedFire()
setTimeout(() => emitter.removeListener('testFire', listener_2), 1000)
setTimeout(emitter.timedFire, 1000)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment