All files / lib/flows each.js

53.62% Statements 37/69
34.29% Branches 24/70
63.64% Functions 7/11
55.38% Lines 36/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1429x 9x                             1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 1x     1x             1x 1x     1x 1x 1x                                                           1x 1x 3x             1x               1x       1x     1x 1x 1x     1x     1x                         1x         1x 1x 1x               9x
const {Writable, Readable} = require('stream');
const {Manager} = require('../manager/manager.js');
 
/**
 * Stream every element of an array individually.
 * 
 * @extends Writable
 */
class FlowEach extends Writable {
 
    /** 
     * Create a FlowEach stream
     * @param {object} [options] Global options
     * @param {string} [options.name] Name for this stream
     */
    constructor(options) {
        options = {...options, objectMode: true};
        super(options);
        this.options = options;
        this.type = 'FlowEach';
        Iif(this.options.name) Manager.set(this.type, this);
        this._reader = null;
        this._corking_readers = {};
        this._reader_messages = [];
        this._none_reader = null;
    }
 
    /**
     * Set stream(s) to pipe to
     * @param {(Writable|Writable[])} dst 
     * @returns {(Writable|FlowAll)} if dst is an array, then returns "this", else dst is returned
     */
    pipe(dst) {
        Eif(!this._reader) {
            const self = this;
            this._reader = new Readable({
                objectMode: true,
                read() {
                    Iif(self._writableState.corked && self._corking_readers['reader']) {
                        self.uncork();
                        delete self._corking_readers['reader'];
                        self._deliver_messages();
                    }
                }
            });
            this._reader.options = {...this._reader.options||{}, name: this.options.name};
            this._reader._readableState.sync = false;
        }
 
        const pipes = Array.isArray(dst)?dst:[dst];
        pipes.forEach(dst=>this._reader.pipe(dst));
        return Array.isArray(dst)?this:dst;
    }
 
    /**
     * Set stream(s) to pipe to when no other stream was piped
     * @param {(Writable|Writable[])} dst Destination stream
     * @returns {(Writable|FlowAll)} if dst is an array, then returns "this", else dst is returned
     */
    none(dst) {
        if(!this._none_reader) {
            const self = this;
            this._none_reader = new Readable({
                objectMode: true,
                read() {
                    if(self._writableState.corked && self._corking_readers['reader']) {
                        self.uncork();
                        delete self._corking_readers['reader'];
                    }
                }
            });
            this._none_reader.options = {...this._none_reader.options||{}, name: this.options.name, port: 'none'};
            this._none_reader._readableState.sync = false;
        }
 
        const pipes = Array.isArray(dst)?dst:[dst];
        pipes.forEach(dst=>this._none_reader.pipe(dst));
        return Array.isArray(dst)?this:dst;
    }
 
    _deliver_messages() {
        Eif(this._reader) {
            while(this._reader_messages.length) {
                Iif(!this._reader.push(this._reader_messages.shift()) && !this._corking_readers['reader']) {
                    this.cork();
                    this._corking_readers['reader'] = true;
                    break;
                }
            }
        }
        Iif(!this._reader_messages.length && this._needsEnd) {
            this._reader && this._reader.push(null);
            this._none_reader && this._none_reader.push(null);
        }
    }
 
    _write(payload, encoding, cb) {
        let data;
        Iif(this.goal) {
            // Is goal player
            data = payload.data;
        } else {
            data = payload;
        }
        
        Eif(this._reader) {
            this._reader_messages = this._reader_messages.concat((!Array.isArray(data)?[data]:data));
            Iif(this.goal) {
                this._reader_messages = this._reader_messages.map(data=>payload.getChild(this).setData(data));   
            };
            this._deliver_messages();
        }
 
        Iif(!this._reader && this._none_reader) {
            if(this.options.none_reason) {
                let data = typeof this.options.none_reason == 'function'?this.options.none_reason(payload):this.options.none_reason;
                payload = (this.goal)?payload.getChild(this).setData(data):data;
            } else {
                payload = this.goal?payload.getChild(this):payload
            }
            if(!this._none_reader.push(payload) && !this._corking_readers['none']) {
                this.cork();
                this._corking_readers['none'] = true;
            }
        }
 
        cb();
 
    }
 
    end() {
        Eif(!this._reader_messages.length) {
            this._reader && this._reader.push(null);
            this._none_reader && this._none_reader.push(null);
        } else {
            this._needsEnd = true;
        }
    }
 
}
 
module.exports.FlowEach = FlowEach;