var Stream = require('stream');

// writable
exports.Event = function (stdin) {

    // pipe a handler stream into a source readable stream
    // http request:
    /*
        // call data handlers on data chunk 
        req.on('data', handlerStream.write);

        // write to writable response stream
        stream.write(err, data);
        res.write(chunk);
        // (maybe just pass along the res stream?)

    */

    // ws request:
    /*
        // call data handlers on data chunk 
        ws.on('data', handlerStream.write);

        // write to writable response stream
        stream.write(err, data);
        ws.write(chunk);
        // (maybe just pass along the ws stream?)

    */

    // flow request:
    /*
        // call data handlers on data chunk 
        flow.on('data', handlerStream.write);

        // write to writable response stream
        stream.write(err, data);
        flow.write(chunk);
        // (maybe just pass along the flow stream?)

    */

    // connect a readable source with a handler stream
    // and pass the writable source to the handlers 

    // pipe a writable stream
    // source readable (tcp, sensors)
    // source readable in code on('data')
    // writable data in code stream.write(null, data);
    // writable error in code stream.write(err)

    var stdout = Stream.Writable(); // handler stream
    var stderr = Stream.Writable(); // handler stream
    stdin.pipe(stdout);
    stdin.pipe(stderr);

    stdin.on('data', function (chunk) {
        //call data handlers
    })

    stdin.on('error', function (chunk) {
        //call error handlers
    })

    return stdin;
};

// duplex
exports.Handler = function (flow) {

    // create event object with method refs
    // create data\error\end handlers for streams
    var hStream = Stream.Writable();
    hStream.sh = flow.sh;
    hStream.dh = flow.dh;
    hStream.rh = flow.rh;
    hStream.eh = flow.eh;
    hStream.event = function () {
        // call stream handlers
    };

    hStream._write = function (chunk, enc, next) {

        // TODO handle request streams in flow
        //if (hStream.req) {
            // collect all chunks
            // and concat and return data on end
            // use callback as end handler
        //}
        // check if it's an error
        // - call error handlers
        // call all data handlers
    };

    // call all end handlers
    hStream.on('finish', function () {

        // call all end handlers
    });

    /*
    var dh;
    var pos = -1;
    var next = function (err, data) {

        // update handler ref
        dh = this.dh[++pos];

        if (!dh || data === null) {
            // end stream
        }

        if (err) {
            // emit an error (call error handlers)
        }

        dh[1].call(dh[0], stream, dh[2], data, next);
    };

    return function (data) {
        next(null, data);
    };
    */

    console.log(hStream);
    return hStream;
};

@@ -69,19 +69,570 @@
 n (stdin
-) %7B
+, stdout, endHandler) %7B%0A    %0A    var eventStream = Stream.Duplex();%0A%0A    eventStream._write = function (chunk, enc, callback) %7B%0A        // call all data handlers%0A        // write data handlers result to writable target stream (stdout, stderr)%0A        //this.events = %7B%7D;%0A        console.log('FLOW EVENT _write:', chunk, enc);%0A        this.push(chunk);%0A        callback();%0A    %7D%0A%0A    eventStream._read = function (n) %7B%0A        // use data handler result as source%0A        // this.push(dataHandlerResult);%0A        console.log('FLOW EVENT _read:', n);%0A    %7D
 %0A%0A    //
@@ -1854,24 +1854,26 @@
 e(err)%0A%0A    
+//
 var stdout =
@@ -1910,24 +1910,26 @@
  stream%0A    
+//
 var stderr =
@@ -1966,24 +1966,26 @@
  stream%0A    
+//
 stdin.pipe(s
@@ -1992,24 +1992,26 @@
 tdout);%0A    
+//
 stdin.pipe(s
@@ -2019,24 +2019,26 @@
 derr);%0A%0A    
+//
 stdin.on('da
@@ -2094,24 +2094,26 @@
 ers%0A    
+//
 %7D)%0A%0A    
 stdin.on
@@ -2104,16 +2104,18 @@
 %7D)%0A%0A    
+//
 stdin.on
@@ -2177,16 +2177,18 @@
 ers%0A    
+//
 %7D)%0A%0A    
@@ -2194,21 +2194,27 @@
  return 
-stdin
+eventStream
 ;%0A%7D;%0A%0A//

test duplex data push