Created
May 21, 2021 14:40
-
-
Save ocommaj/3c8d2454ee327faba8ee9a9b2ce1a701 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
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