Restart FrameProducer if it dies unexpectedly.

This commit is contained in:
Simo Kinnunen
2015-04-16 16:34:58 +09:00
parent 53bd852d48
commit 88162e8a33
2 changed files with 77 additions and 20 deletions

View File

@@ -0,0 +1,35 @@
var util = require('util')
var EventEmitter = require('eventemitter3').EventEmitter
function RiskyStream(stream) {
this.endListener = function() {
this.ended = true
this.stream.removeListener('end', this.endListener)
if (!this.expectingEnd) {
this.emit('unexpectedEnd')
}
this.emit('end')
}.bind(this)
this.stream = stream
.on('end', this.endListener)
this.expectingEnd = false
this.ended = false
}
util.inherits(RiskyStream, EventEmitter)
RiskyStream.prototype.end = function() {
this.expectEnd()
return this.stream.end()
}
RiskyStream.prototype.expectEnd = function() {
this.expectingEnd = true
return this
}
module.exports = RiskyStream