Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont error #3

Merged
merged 4 commits into from
Aug 4, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var es = require('event-stream')

function MuxDemux (name) {
function MuxDemux (opts) {

function createID() {
return (
Expand Down Expand Up @@ -32,14 +32,18 @@ function MuxDemux (name) {
})

function destroyAll (_err) {
md.removeListener('end', destroyAll)
md.removeListener('error', destroyAll)
md.removeListener('close', destroyAll)
md.removeListener('end', destroyAll)
md.removeListener('error', destroyAll)
md.removeListener('close', destroyAll)
var err = _err || new Error ('unexpected disconnection')
for (var i in streams) {
var s = streams[i]
s.emit('error', err)
s.destroy()
if (opts && opts.error === false) {
s.end()
} else {
s.emit('error', err)
s.destroy()
}
}
}

Expand All @@ -57,14 +61,14 @@ function MuxDemux (name) {
throw new Error('stream is not writable')
md.emit('data', [s.id, 'data', data])
}, function () {
md.emit('data', [s.id, 'end'])
md.emit('data', [s.id, 'end'])
})
s.pause = function () {
md.emit('data', [s.id, 'pause'])
}
s.resume = function () {
md.emit('data', [s.id, 'resume'])
}
}
s.once('close', function () {
md.emit('data', [s.id, 'close'])
delete streams[id]
Expand All @@ -73,7 +77,7 @@ function MuxDemux (name) {
s.readable = opts.readable
streams[s.id = id] = s
s.meta = meta
return s
return s
}

var outer = es.connect(es.split(), es.parse(), md, es.stringify())
Expand All @@ -88,15 +92,15 @@ function MuxDemux (name) {
pipe.call(outer, dest, opts)
md.on('end', destroyAll)
md.on('close', destroyAll)
md.on('error', destroyAll)
md.on('error', destroyAll)
return dest
}

outer.createStream = function (meta, opts) {
opts = opts || {writable: true, readable: true}
var s = createStream(createID(), meta, opts)
var _opts = {writable: opts.readable, readable: opts.writable}
md.emit('data', [s.id, 'new', {meta: meta, opts: _opts}])
md.emit('data', [s.id, 'new', {meta: meta, opts: _opts}])
return s
}
outer.createWriteStream = function (meta) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"test": "tap test/*.js"
},
"devDependencies": {
"assertions": "2.3"
"assertions": "2.3",
"tap": "~0.3.0"
},
"author": "Dominic Tarr <dominic.tarr@gmail.com> (dominictarr.com)",
"optionalDependencies": {},
Expand Down
10 changes: 10 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ client.createStream(meta)
there is actually no distinction between clients and servers.
if both sides are listening `on('connection',...)` then both sides may call `create{Write,Read,}Stream(meta)` and initiate new streams.

### MuxDemux(options)

Creates a MuxDemux stream. Optionally pass in an options hash

{
error: Boolean
}

If the error option is set to false then MuxDemux won't emit errors on the streams on unexpected disconnects and instead just end those streams

### createReadStream (meta)

open a `ReadableStream` from the other side.
Expand Down