All files / lib/goals goals.js

90.82% Statements 89/98
74.14% Branches 43/58
100% Functions 25/25
97.62% Lines 82/84

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 1969x 9x 9x 9x                           8x 8x 8x 8x 8x 8x 8x     15x     8x 8x 8x 7x   8x       8x 8x 8x 8x   8x     3x 3x 3x   1x 1x   3x 3x     8x 7x   8x       6x 6x 6x   2x 2x   6x 6x     8x 7x   8x 8x 8x 8x                 2x 2x 2x     3x     2x 2x       2x 2x 2x               3x 3x       32x 3x                 2x 2x     4x     2x 2x     2x 2x 2x                   3x 3x 1x 1x 1x         3x 2x 2x 2x                           2x       8x       8x   8x 8x 8x         9x  
const {Writable, Readable} = require('stream');
const {Manager} = require('../manager/manager.js');
const {DataWrapper} = require('../wrapper/data_wrapper.js');
const vm = require('vm');
 
/**
 * Goal wrapps many streams within, hidding complex business logic. Represents a goal to achieve.
 * 
 * @extends Writable
 */
class Goal extends Writable {
    /** 
     * Create a Goal stream
     * @param {object} [options] Global options
     * @param {string} [options.name] Name for this stream
     */
    constructor(options, cb) {
        options = {...options, objectMode: true};
        super(options);
        this.options = options;
        this.type = 'Goal';
        Iif(this.options.name) Manager.set(this.type, this);
        const self = this;
        this._start = new Readable({
            objectMode: true,
            read() {
                self.uncork();
            }
        });
        this._start.options = {...this._start.options||{}, name: this.options.name, port: 'start'};
        this._start._readableState.sync = false;
        this.on('prefinish', ()=>{
            this._start.push(null);
        })
        this._error_reader = new Readable({
            objectMode: true,
            read() {}
        });
        this._error_reader.options = {...this._error_reader.options||{}, name: this.options.name, port: 'error'};
        this._error_reader._readableState.sync = false;
        this._resolve_reader = null;
        this._reject_reader = null;
 
        this._resolve_writer =new Writable({
            objectMode: true,
            write: (payload, encoding, cb) => {
                let data = payload instanceof DataWrapper?payload.data:payload;
                if(self._cb) self._cb(null, data);
                if(self._resolve_reader) {
                    //process.nextTick(()=>self._resolve_reader.push(data));
                    let goOn = self._resolve_reader.push(data);
                    Iif(!self._resolve_writer.corked && !goOn) self._resolve_writer.cork();
                }
                self.emit('resolve', data);
                cb();
            }
        });
        this._resolve_writer.on('prefinish', ()=>{
            if(this._resolve_reader) this._resolve_reader.push(null);
        });
        this._reject_writer = new Writable({
            objectMode: true,
            write: (payload, encoding, cb) => {
                // TODO - save payload for statistics
                let data = payload instanceof DataWrapper?payload.data:payload;
                if(self._cb) self._cb(data);
                if(self._reject_reader) {
                    //process.nextTick(()=>self._resolve_reader.push(data));
                    let goOn = self._reject_reader.push(data);
                    Iif(!self._reject_writer.corked && !goOn) self._reject_writer.cork();
                }
                self.emit('reject', data);
                cb();
            }
        });
        this._reject_writer.on('prefinish', ()=>{
            if(this._reject_reader) this._reject_reader.push(null);
        });
        this._error_reader.pipe(this._reject_writer);
        this._cb = cb;
        this.on('pipe', src => {
            this._src = src;
        })
    }
 
    /**
     * Pipe to a destination stream when goal was achieved
     * @param {Writable} dst 
     */
    resolve(dst) {
        Eif(!this._resolve_reader) {
            const self = this;
            this._resolve_reader = new Readable({
                objectMode: true,
                read() {
                    Eif(self._resolve_writer) self._resolve_writer.uncork()
                }
            });
            this._resolve_reader.options = {...this._resolve_reader.options||{}, name: this.options.name, port: 'resolve'};
            this._resolve_reader._readableState.sync = false;
            
        }
 
        const pipes = Array.isArray(dst)?dst:[dst];
        pipes.forEach(dst=>this._resolve_reader.pipe(dst));
        return Array.isArray(dst)?this:dst;
    }
 
    /**
     * Register a node style callback, called when goal is resolved or rejected
     * @param {NodeCallback} cb 
     */
    callback(cb) {
        this._cb = cb;
        return this;
    }
 
    _registerChild(src) {
        src.on('error', (error)=>{
            this._error_reader.push(error);
        })
    }
 
    /**
     * Pipe to a destination stream when goal was not achieved
     * @param {Writable} dst 
     */
    reject(dst) {
        Eif(!this._reject_reader) {
            this._reject_reader = new Readable({
                objectMode: true,
                read() {
                    Iif(self._reject_writer) self._reject_writer.uncork()
                }
            });
            this._reject_reader.options = {...this._reject_reader.options||{}, name: this.options.name, port: 'reject'};
            this._reject_reader._readableState.sync = false;
        }
 
        const pipes = Array.isArray(dst)?dst:[dst];
        pipes.forEach(dst=>this._reject_reader.pipe(dst));
        return Array.isArray(dst)?this:dst;
    }
 
    /**
     * Thenable function to work with promises.
     * @param {ThenCallback} thenCallback called when goal is achieved 
     * @param {CatchCallback} [catchCallback] called when goal is not achieved
     * @returns {Promise}
     */
    then(rescb, rejcb) {
        return new Promise((resolve, reject) => {
            this.on('resolve', data => {
                Iif(!rescb) return resolve(data);
                try {
                    resolve(rescb(data));
                } catch(e) {
                    reject(e);
                }
            });
            this.on('reject', data => {
                Iif(!rejcb) return reject(data);
                try {
                    resolve(rejcb(data));
                } catch(e) {
                    reject(e);
                }
            })
        });
    }
 
    /**
     * Thenable function to work with promises.
     * @param {CatchCallback} catchCallback called when goal is not achieved
     * @returns {Promise}
     */
    catch(rejcb) {
        return this.then(null, rejcb);
    }
 
    build(chain, options) {
        Manager._build(chain, {...options, goal: this});
    }
 
    _write(payload, encoding, cb) {
        var data = new DataWrapper(payload, this);
        //process.nextTick(()=>this._start.push(data));
        let goOn = this._start.push(data);
        Iif(!this._writableState.corked && !goOn) this.cork();
        cb();
    }
 
}
 
module.exports.Goal = Goal;
// module.exports.GoalGroup = GoalGroup;