Permalink
Browse files

v0.6.0

  • Loading branch information...
1 parent 99a1585 commit dbe46a03c75f1814a08862be9fb7433cd020a17c @Raynos committed Aug 7, 2012
Showing with 30 additions and 3 deletions.
  1. +29 −2 browser.js
  2. +1 −1 package.json
View
@@ -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
@@ -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
@@ -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()
@@ -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
})
@@ -107,4 +126,12 @@ function reconnecter(uri) {
, readable: true
})
}
+}
+
+function pause(details) {
+ details.proxyMdmWrite.pause()
+}
+
+function resume(details) {
+ details.proxyMdmWrite.resume()
}
View
@@ -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>",

0 comments on commit dbe46a0

Please sign in to comment.