now allows the handling of custom events and run returns promise
This commit is contained in:
77
ts/index.ts
77
ts/index.ts
@ -1,23 +1,86 @@
|
||||
import * as plugins from './smartstream.plugins'
|
||||
|
||||
export interface IErrorFunction {
|
||||
(err): number
|
||||
(err): any
|
||||
}
|
||||
|
||||
export interface IStreamStartFunction {
|
||||
(stream): any
|
||||
}
|
||||
|
||||
export interface ICustomEventFunction {
|
||||
(): any
|
||||
}
|
||||
|
||||
export interface ICustomEventObject {
|
||||
eventName: string
|
||||
eventFunction: ICustomEventFunction
|
||||
}
|
||||
|
||||
/**
|
||||
* class Smartstream handles
|
||||
*/
|
||||
export class Smartstream {
|
||||
streamArray = []
|
||||
errorFunction: IErrorFunction = null
|
||||
constructor(streamArrayArg: any[]){
|
||||
streamStartFunction: IStreamStartFunction = null
|
||||
customEventObjectArray: ICustomEventObject[] = []
|
||||
constructor(streamArrayArg: any[]) {
|
||||
this.streamArray = streamArrayArg
|
||||
}
|
||||
|
||||
/**
|
||||
* attach an error handler to the stream to prevent throwing
|
||||
*/
|
||||
onError(errorFunctionArg: IErrorFunction) {
|
||||
this.errorFunction = errorFunctionArg
|
||||
}
|
||||
run() {
|
||||
let combinedStream = plugins.streamCombiner2.obj(this.streamArray)
|
||||
if (this.errorFunction !== null) {
|
||||
combinedStream.on('error', this.errorFunction)
|
||||
|
||||
/**
|
||||
* make something with the stream itself
|
||||
*/
|
||||
onStreamStart(): plugins.q.Promise<any> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
return combinedStream
|
||||
finalStream.on('end',function(){
|
||||
done.resolve()
|
||||
})
|
||||
finalStream.on('close',function(){
|
||||
done.resolve()
|
||||
})
|
||||
finalStream.on('finish',function(){
|
||||
done.resolve()
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
}
|
@ -1,3 +1,2 @@
|
||||
import 'typings-global'
|
||||
export import q = require('q')
|
||||
export let streamCombiner2 = require('stream-combiner2')
|
||||
|
Reference in New Issue
Block a user