smartstream/ts/index.ts

108 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-03-30 23:20:01 +00:00
import * as plugins from './smartstream.plugins.js';
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 {
2022-03-30 23:20:01 +00:00
(err: Error): 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 {
2022-03-30 23:20:01 +00:00
private streamArray: Array<plugins.stream.Duplex> = [];
2019-02-19 00:10:56 +00:00
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,
2022-03-30 23:20:01 +00:00
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
2022-03-30 23:20:01 +00:00
const streamExecutionArray: Array<plugins.stream.Duplex> = [];
2019-02-19 00:10:56 +00:00
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
}
2022-03-30 23:20:01 +00:00
stream.on('error', (err) => {
2019-02-19 00:10:56 +00:00
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
},
2022-03-30 23:20:01 +00:00
(cb) => {
2019-02-19 00:10:56 +00:00
cb();
2016-09-25 12:00:16 +00:00
}
2019-02-19 00:10:56 +00:00
);
};