2016-09-25 12:00:16 +00:00
|
|
|
import * as plugins from './smartstream.plugins'
|
|
|
|
|
|
|
|
export interface IErrorFunction {
|
2016-09-25 14:10:06 +00:00
|
|
|
(err): any
|
2016-09-25 12:00:16 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 14:10:06 +00:00
|
|
|
export interface ICustomEventFunction {
|
|
|
|
(): any
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ICustomEventObject {
|
|
|
|
eventName: string
|
|
|
|
eventFunction: ICustomEventFunction
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class Smartstream handles
|
|
|
|
*/
|
2016-09-25 12:00:16 +00:00
|
|
|
export class Smartstream {
|
2016-09-25 14:28:42 +00:00
|
|
|
private streamArray = []
|
|
|
|
private errorFunction: IErrorFunction = null
|
|
|
|
private customEventObjectArray: ICustomEventObject[] = []
|
|
|
|
private streamStartedDeferred = plugins.q.defer()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*/
|
2016-09-25 14:10:06 +00:00
|
|
|
constructor(streamArrayArg: any[]) {
|
2016-09-25 12:00:16 +00:00
|
|
|
this.streamArray = streamArrayArg
|
|
|
|
}
|
2016-09-25 14:10:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* attach an error handler to the stream to prevent throwing
|
|
|
|
*/
|
2016-09-25 12:00:16 +00:00
|
|
|
onError(errorFunctionArg: IErrorFunction) {
|
|
|
|
this.errorFunction = errorFunctionArg
|
|
|
|
}
|
2016-09-25 14:10:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* make something with the stream itself
|
|
|
|
*/
|
2016-09-25 14:28:42 +00:00
|
|
|
streamStarted(): plugins.q.Promise<any> {
|
|
|
|
return this.streamStartedDeferred.promise
|
|
|
|
}
|
2016-09-25 14:10:06 +00:00
|
|
|
|
2016-09-25 14:28:42 +00:00
|
|
|
/**
|
|
|
|
* attach listener to custom event
|
|
|
|
*/
|
|
|
|
onCustomEvent(eventNameArg: string, eventFunctionArg: ICustomEventFunction) {
|
|
|
|
this.customEventObjectArray.push({
|
|
|
|
eventName: eventNameArg,
|
|
|
|
eventFunction: eventFunctionArg
|
|
|
|
})
|
2016-09-25 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the stream
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
|
|
|
run(): plugins.q.Promise<void> {
|
|
|
|
let done = plugins.q.defer<void>()
|
|
|
|
|
|
|
|
// clone Array
|
|
|
|
let streamExecutionArray = []
|
|
|
|
for (let streamItem of this.streamArray) { streamExecutionArray.push(streamItem) }
|
|
|
|
|
|
|
|
// combine the stream
|
|
|
|
let finalStream = null
|
|
|
|
let firstIteration: boolean = true
|
|
|
|
for (let stream of streamExecutionArray) {
|
|
|
|
if (firstIteration === true) {
|
|
|
|
finalStream = stream
|
|
|
|
}
|
|
|
|
if (this.errorFunction !== null) {
|
|
|
|
stream.on('error', this.errorFunction)
|
|
|
|
}
|
|
|
|
for (let customEventObject of this.customEventObjectArray) {
|
|
|
|
stream.on(customEventObject.eventName, customEventObject.eventFunction)
|
|
|
|
}
|
|
|
|
if (!firstIteration) {
|
|
|
|
finalStream = finalStream.pipe(stream)
|
|
|
|
}
|
|
|
|
firstIteration = false
|
2016-09-25 12:00:16 +00:00
|
|
|
}
|
2016-09-25 14:28:42 +00:00
|
|
|
|
|
|
|
this.streamStartedDeferred.resolve()
|
|
|
|
|
2016-09-25 14:10:06 +00:00
|
|
|
finalStream.on('end',function(){
|
|
|
|
done.resolve()
|
|
|
|
})
|
|
|
|
finalStream.on('close',function(){
|
|
|
|
done.resolve()
|
|
|
|
})
|
|
|
|
finalStream.on('finish',function(){
|
|
|
|
done.resolve()
|
|
|
|
})
|
|
|
|
return done.promise
|
2016-09-25 12:00:16 +00:00
|
|
|
}
|
|
|
|
}
|