smartstream/ts/index.ts

108 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-02-19 00:10:56 +00:00
import * as plugins from './smartstream.plugins';
2016-09-25 12:00:16 +00:00
2017-06-30 16:32:32 +00:00
// interfaces
2019-02-19 00:10:56 +00:00
import { Transform } from 'stream';
2017-03-04 13:26:10 +00:00
2016-09-25 12:00:16 +00:00
export interface IErrorFunction {
2019-02-19 00:10:56 +00:00
(err): any;
2016-09-25 12:00:16 +00:00
}
export interface ICustomEventFunction {
2019-02-19 00:10:56 +00:00
(): any;
}
export interface ICustomEventObject {
2019-02-19 00:10:56 +00:00
eventName: string;
eventFunction: ICustomEventFunction;
}
/**
2019-02-19 00:10:56 +00:00
* class Smartstream handles
*/
2016-09-25 12:00:16 +00:00
export class Smartstream {
2019-02-19 00:10:56 +00:00
private streamArray = [];
private customEventObjectArray: ICustomEventObject[] = [];
private streamStartedDeferred = plugins.smartpromise.defer();
2016-09-25 14:28:42 +00:00
2017-03-04 12:50:12 +00:00
/**
* constructor
*/
constructor(streamArrayArg: any[]) {
2019-02-19 00:10:56 +00:00
this.streamArray = streamArrayArg;
2017-03-04 12:50:12 +00:00
}
2017-03-04 12:50:12 +00:00
/**
* make something with the stream itself
*/
2019-02-19 00:10:56 +00:00
streamStarted(): Promise<any> {
return this.streamStartedDeferred.promise;
2017-03-04 12:50:12 +00:00
}
2017-03-04 12:50:12 +00:00
/**
* attach listener to custom event
*/
2019-02-19 00:10:56 +00:00
onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction) {
2017-03-04 12:50:12 +00:00
this.customEventObjectArray.push({
eventName: eventNameArg,
eventFunction: eventFunctionArg
2019-02-19 00:10:56 +00:00
});
2017-03-04 12:50:12 +00:00
}
/**
* run the stream
* @returns Promise
*/
2019-02-19 00:10:56 +00:00
run(): Promise<void> {
const done = plugins.smartpromise.defer<void>();
2017-03-04 12:50:12 +00:00
// clone Array
2019-02-19 00:10:56 +00:00
const streamExecutionArray = [];
for (const streamItem of this.streamArray) {
streamExecutionArray.push(streamItem);
}
2017-03-04 12:50:12 +00:00
// combine the stream
2019-02-19 00:10:56 +00:00
let finalStream = null;
let firstIteration: boolean = true;
for (const stream of streamExecutionArray) {
2017-03-04 12:50:12 +00:00
if (firstIteration === true) {
2019-02-19 00:10:56 +00:00
finalStream = stream;
2017-03-04 12:50:12 +00:00
}
2019-02-19 00:10:56 +00:00
stream.on('error', err => {
done.reject(err);
});
for (const customEventObject of this.customEventObjectArray) {
stream.on(customEventObject.eventName, customEventObject.eventFunction);
2017-03-04 12:50:12 +00:00
}
if (!firstIteration) {
2019-02-19 00:10:56 +00:00
finalStream = finalStream.pipe(stream);
2017-03-04 12:50:12 +00:00
}
2019-02-19 00:10:56 +00:00
firstIteration = false;
2017-03-04 12:50:12 +00:00
}
2019-02-19 00:10:56 +00:00
this.streamStartedDeferred.resolve();
2019-02-19 00:10:56 +00:00
finalStream.on('end', () => {
done.resolve();
});
finalStream.on('close', () => {
done.resolve();
});
finalStream.on('finish', () => {
done.resolve();
});
return done.promise;
2017-03-04 12:50:12 +00:00
}
}
2016-09-25 14:28:42 +00:00
2017-03-04 12:50:12 +00:00
export let cleanPipe = () => {
2017-03-04 13:26:10 +00:00
return plugins.through2.obj(
2017-03-04 12:50:12 +00:00
(file, enc, cb) => {
2019-02-19 00:10:56 +00:00
cb();
2017-03-04 12:50:12 +00:00
},
2019-02-19 00:10:56 +00:00
cb => {
cb();
2016-09-25 12:00:16 +00:00
}
2019-02-19 00:10:56 +00:00
);
};