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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 9x 9x 9x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 32x 32x 32x 32x 45x 32x 32x 32x 32x 32x 32x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 31x 31x 8x 31x 70x 32x 32x 31x 1x 1x 31x 1x 1x 31x 29x 11x 11x 9x | const {Writable, Readable} = require('stream');
const {Manager} = require('../manager/manager.js');
const uuid = require('uuid');
// /*
// * Example of yaml representation:
// *
// * ````yaml
// * stream_name:
// * type: FlowAll
// * options:
// * none_message: 'some message' #name of the option
// * methods:
// * write: #Override default write (not recomended)
// * code: [array of strings]
// * params: [array of param names]
// * when: #Conditional piping
// * cond: [array of strings] #Conditional piping (always receives payload as parameter)
// * dst: #Where to pipe (can be an array of destinations)
// * type: {destination type}
// * name: {destination name}
// * pipe: #unconditional piping
// * type: {destination type}
// * name: {destination name}
// * none: #Where to pipe when no matches where found
// * type: {destination type}
// * name: {destination name}
// * on: #add listener
// * finish: #event name
// * code: [array of strings]
// * params: [array of param names]
// * pon: #prepend listener
// * prefinish: #event name
// * code: [array of strings]
// * params: [array of param names]
// * once: #add once listener
// * finish: #event name
// * code: [array of strings]
// * params: [array of param names]
// * ponce: #prepend once listener
// * prefinish: #event name
// * code: [array of strings]
// * params: [array of param names]
// * ````
// *
// */
/**
* Check all piped streams, and flow data to all which matches criteria.
*
* @extends Writable
*/
class FlowAll extends Writable {
/**
* Create a FlowAll 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._cnd_readers = [];
this._uncnd_reader = null;
this._none_reader = null;
this.on('finish', () => {
this._cnd_readers.forEach(rdr=>rdr.reader.push(null));
if(this._uncnd_reader) this._uncnd_reader.push(null);
if(this._none_reader) this._none_reader.push(null);
});
this._corking_readers={};
this.type = 'FlowAll';
if(this.options.name) Manager.set(this.type, this);
}
/**
* Set stream(s) to pipe if criteria is match
* @param {Condition} cond
* @param {(Writable|Writable[])} dst
* @returns {(Writable|FlowAll)} if dst is an array, then returns "this", else dst is returned
*/
when(cond, dst) {
Iif(typeof cond != 'function') this.emit('error', '"when" first argument must be a function');
const self = this;
const id = uuid.v4();
const reader = new Readable({
objectMode: true,
read() {
Iif(self._corking_readers[id]) {
self.uncork();
delete self._corking_readers[id];
}
}
});
reader._readableState.sync = false;
const pipes = Array.isArray(dst)?dst:[dst];
pipes.forEach(dst=>reader.pipe(dst));
const pos = this._cnd_readers.push({
cond,
id,
reader
});
reader.options = {...reader.options||{}, name: this.options.name, port: 'when'+(pos-1)};
return Array.isArray(dst)?this:dst;
}
/**
* Set stream(s) to pipe unconditionally
* @param {(Writable|Writable[])} dst
* @returns {(Writable|FlowAll)} if dst is an array, then returns "this", else dst is returned
*/
pipe(dst) {
Eif(!this._uncnd_reader) {
const self = this;
this._uncnd_reader = new Readable({
objectMode: true,
read() {
Iif(self._writableState.corked && self._corking_readers['uncnd']) {
self.uncork();
delete self._corking_readers['uncnd'];
}
}
});
this._uncnd_reader._readableState.sync = false;
this._uncnd_reader.options = {...this._uncnd_reader.options||{}, name: this.options.name};
this._uncnd_reader.on('resume', ()=>this.uncork());
}
const pipes = Array.isArray(dst)?dst:[dst];
pipes.forEach(dst=>this._uncnd_reader.pipe(dst));
return Array.isArray(dst)?this:dst;
}
/**
* Set stream(s) to pipe when no other stream matched criteria. If unconditional piping was set, this will never be used.
* @param {(Writable|Writable[])} dst Destination stream
* @returns {(Writable|FlowAll)} if dst is an array, then returns "this", else dst is returned
*/
none(dst) {
Eif(!this._none_reader) {
const self = this;
this._none_reader = new Readable({
objectMode: true,
read() {
Iif(self._writableState.corked && self._corking_readers['none']) {
self.uncork();
delete self._corking_readers['none'];
}
}
});
this._none_reader.options = {...this._none_reader.options||{}, name: this.options.name, port: 'none'};
this._none_reader._readableState.sync = false;
this._none_reader.on('resume', ()=>this.uncork());
}
const pipes = Array.isArray(dst)?dst:[dst];
pipes.forEach(dst=>this._none_reader.pipe(dst));
return Array.isArray(dst)?this:dst;
}
/**
* Internal _write method. Do not call directly.
* Do not override unless you are sure of what you are doing
* @param {(DataWrapper|*)} payload chunk of data to be written
* @param {string} encoding encoding of data when objectMode=false. Never used.
* @param {WriteCallback} cb Internal Writable callback
*/
_write(payload, encoding, cb) {
var delivered = false;
if(this.goal) {
// is goal player
payload = payload.getChild(this);
}
this._cnd_readers.forEach(cr => {
if(cr.cond(payload)) {
delivered = true;
Iif(!cr.reader.push(payload) && !this._corking_readers[cr.id]) {
this.cork();
this._corking_readers[cr.id] = true;
}
}
});
if(this._uncnd_reader) {
delivered = true;
Iif(!this._uncnd_reader.push(payload) && !this._corking_readers['uncnd']) {
this.cork();
this._corking_readers['uncnd'] = true;
}
}
if(!delivered && this._none_reader) {
Iif(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;
}
Iif(!this._none_reader.push(payload) && !this._corking_readers['none']) {
this.cork();
this._corking_readers['none'] = true;
}
}
cb();
}
end() {
this._cnd_readers.forEach(cnd => cnd.reader.push(null));
if(this._uncnd_reader) this._uncnd_reader.push(null);
if(this._none_reader) this._none_reader.push(null);
}
}
module.exports.FlowAll = FlowAll; |