Created
August 7, 2017 21:47
-
-
Save msikma/137adf92f261cd9cee1b5e7a6f1219b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { EventEmitter } from 'events'; | |
class StreamTester extends EventEmitter { | |
constructor() { | |
super(); | |
this._done = false; | |
this._emitter1 = null; | |
this._emitter2 = null; | |
this._emitter3 = null; | |
this._stopEmitters = null; | |
process.nextTick(() => { | |
this._startEmitters(); | |
}); | |
} | |
_startEmitters() { | |
if (this._done) { | |
return; | |
} | |
this._emitter1 = setInterval(() => this.emit('data', 'emitter 1'), 280); | |
this._emitter2 = setInterval(() => this.emit('data', 'emitter 2'), 580); | |
this._emitter3 = setInterval(() => this.emit('data', 'emitter 3'), 740); | |
this._stopEmitters = setTimeout(() => { | |
this._pauseEmitters(); | |
this._done = true; | |
this.emit('end'); | |
}, 1500); | |
} | |
_pauseEmitters() { | |
if (this._done) { | |
return; | |
} | |
clearInterval(this._emitter1); | |
clearInterval(this._emitter2); | |
clearInterval(this._emitter3); | |
} | |
pause() { | |
this._pauseEmitters(); | |
} | |
resume() { | |
this._startEmitters(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment