smartstream/dist/index.js
2017-03-04 13:50:12 +01:00

82 lines
4.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartstream.plugins");
/**
* class Smartstream handles
*/
class Smartstream {
/**
* constructor
*/
constructor(streamArrayArg) {
this.streamArray = [];
this.customEventObjectArray = [];
this.streamStartedDeferred = plugins.q.defer();
this.streamArray = streamArrayArg;
}
/**
* make something with the stream itself
*/
streamStarted() {
return this.streamStartedDeferred.promise;
}
/**
* attach listener to custom event
*/
onCustomEvent(eventNameArg, eventFunctionArg) {
this.customEventObjectArray.push({
eventName: eventNameArg,
eventFunction: eventFunctionArg
});
}
/**
* run the stream
* @returns Promise
*/
run() {
let done = plugins.q.defer();
// clone Array
let streamExecutionArray = [];
for (let streamItem of this.streamArray) {
streamExecutionArray.push(streamItem);
}
// combine the stream
let finalStream = null;
let firstIteration = true;
for (let stream of streamExecutionArray) {
if (firstIteration === true) {
finalStream = stream;
}
stream.on('error', (err) => {
done.reject(err);
});
for (let customEventObject of this.customEventObjectArray) {
stream.on(customEventObject.eventName, customEventObject.eventFunction);
}
if (!firstIteration) {
finalStream = finalStream.pipe(stream);
}
firstIteration = false;
}
this.streamStartedDeferred.resolve();
finalStream.on('end', function () {
done.resolve();
});
finalStream.on('close', function () {
done.resolve();
});
finalStream.on('finish', function () {
done.resolve();
});
return done.promise;
}
}
exports.Smartstream = Smartstream;
exports.cleanPipe = () => {
plugins.through2.obj((file, enc, cb) => {
cb();
}, (cb) => {
cb();
});
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGlEQUFnRDtBQWVoRDs7R0FFRztBQUNIO0lBS0U7O09BRUc7SUFDSCxZQUFZLGNBQXFCO1FBUHpCLGdCQUFXLEdBQUcsRUF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