Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var shoe = require("shoe")
, MuxDemux = require("mux-demux")
, curry = require("ap").curry
, PauseStream = require("pause-stream")
, reemit = curry(function (stream, message) {
stream.emit(message)
})
module.exports = createOuterStream
function createOuterStream(uri, reconnect) {
var mdm = MuxDemux()
createShoeStream(uri, reconnect, mdm)
if (reconnect) {
var _createStream = mdm.createStream
mdm.createStream = createStream
}
return mdm
function createStream(meta) {
var ps = PauseStream()
createMdmStream(meta, ps)
mdm.on("disconnect", function () {
ps.pause()
})
mdm.on("connect", function () {
createMdmStream(meta, ps)
ps.resume()
})
return ps
function createMdmStream(meta, ps) {
var stream = _createStream(meta, ps)
ps.pipe(stream).pipe(ps, {
end: false
})
}
}
}
function createShoeStream(uri, reconnect, mdm) {
var stream = shoe(uri)
, emitConnect = reemit(mdm, "connect")
stream.on("connect", emitConnect)
mdm.pipe(stream).pipe(mdm, {
end: false
})
if (reconnect) {
stream.on("end", onend)
}
return mdm
function onend() {
stream.removeListener("end", onend)
stream.removeListener("connect", emitConnect)
mdm.emit("disconnect")
createShoeStream(uri, reconnect, mdm)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.