Skip to content

Commit

Permalink
v0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Aug 7, 2012
1 parent 99a1585 commit dbe46a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions browser.js
Expand Up @@ -2,6 +2,7 @@
window.Buffer = require("buffer").Buffer

var shoe = require("mux-demux-shoe")
, PauseStream = require("pause-stream")
, through = require("through")
, es = require("event-stream")
, Backoff = require("backoff").fibonnaci
Expand All @@ -13,13 +14,18 @@ var shoe = require("mux-demux-shoe")
module.exports = reconnecter

function reconnecter(uri) {
var proxyWrite = through()
var proxyWrite = PauseStream()
, proxyRead = through()
, proxy = es.duplex(proxyWrite, proxyRead)
, metaStreams = []
, stream
, connected = false
, backoff = Backoff(BACKOFF_OPTIONS)

if (!connected) {
proxyWrite.pause()
}

proxy.createStream = createStream
proxy.createWriteStream = createWriteStream
proxy.createReadStream = createReadStream
Expand All @@ -44,11 +50,20 @@ function reconnecter(uri) {

function onconnect() {
backoff.reset()

proxyWrite.resume()
metaStreams.forEach(resume)

proxy.emit("connect")
connected = true
}

function onend() {
proxyWrite.pause()
metaStreams.forEach(pause)

proxy.emit("disconnect")
connected = false

// wait a second otherwise it spin locks
var delay = backoff.backoffStrategy_.next()
Expand All @@ -72,11 +87,15 @@ function reconnecter(uri) {

function createStream(meta, opts) {
var proxyMdmRead = through()
, proxyMdmWrite = through()
, proxyMdmWrite = PauseStream()
, proxy = es.duplex(proxyMdmWrite, proxyMdmRead)

var mdm = stream.createStream(meta, opts)

if (!connected) {
proxyMdmWrite.pause()
}

proxyMdmWrite.pipe(mdm).pipe(proxyMdmRead, {
end: false
})
Expand Down Expand Up @@ -107,4 +126,12 @@ function reconnecter(uri) {
, readable: true
})
}
}

function pause(details) {
details.proxyMdmWrite.pause()
}

function resume(details) {
details.proxyMdmWrite.resume()
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "boot",
"version": "0.5.0",
"version": "0.6.0",
"description": "An automatic reconnect mux-demux-shoe",
"keywords": [],
"author": "Raynos <raynos2@gmail.com>",
Expand Down

0 comments on commit dbe46a0

Please sign in to comment.