import { Processor } from './processor'; import { Socket, OutputSocket, SocketType, InputSocket } from './socket'; import { ErrorCodeSpace, ErrorInfoSpace } from './error'; import { VoidCallback } from '../common-types'; import { EventEmitter } from 'eventemitter3'; import { AppInputSocket } from '../io-sockets/app-input-socket'; import { WebFileDownloadSocket } from '../io-sockets/web-file-download.socket'; export declare enum FlowState { VOID = "void", WAITING = "waiting", FLOWING = "flowing", COMPLETED = "completed" } export declare enum FlowCompletionResultCode { NONE = "none", OK = "ok", FAILED = "failed" } export declare type FlowCompletionResult = { code: FlowCompletionResultCode; data: Blob; }; export declare enum FlowErrorType { CORE = "core", PROC = "proc", RUNTIME = "runtime" } export declare type FlowError = ErrorInfoSpace & { type: FlowErrorType; }; export declare enum FlowEvent { ERROR = "flow:error", STATE_CHANGE_PENDING = "flow:state-change-pending", STATE_CHANGE_ABORTED = "flow:state-change-aborted", STATE_CHANGED = "flow:state-changed" } export declare type FlowStateChangeCallback = (previousState: FlowState, newState: FlowState) => void; export declare enum FlowConfigFlag { NONE = 0, WITH_APP_SOCKET = 1, WITH_DOWNLOAD_SOCKET = 2, ALL = 4294967295 } export declare abstract class Flow extends EventEmitter { readonly flags: FlowConfigFlag; private onStateChangePerformed; private onStateChangeAborted; constructor(flags?: FlowConfigFlag, onStateChangePerformed?: FlowStateChangeCallback, onStateChangeAborted?: (reason: string) => void, downloadSocketParams?: { el: HTMLElement; mimeType: string; filenameTemplateBase: string; }); private _downloadSocket; private _appSocket; private _state; private _pendingState; private _prevState; private _processors; private _extSockets; private _error; private _whenCompleted; private _whenCompletedResolve; private _whenCompletedReject; private _completionResultCode; protected get extDownloadSocket(): WebFileDownloadSocket; protected get extAppSocket(): AppInputSocket; get procList(): Processor[]; get externalSockets(): Socket[]; getExternalSocketsByType(type: SocketType): Socket[]; getExternalInputSockets(): InputSocket[]; getExternalOutputSockets(): OutputSocket[]; get error(): FlowError; hasFlag(flag: FlowConfigFlag): boolean; addProc(...p: Processor[]): Flow; removeProc(...p: Processor[]): Flow; whenCompleted(): Promise; getCurrentState(): FlowState; getPendingState(): FlowState | null; getPreviousState(): FlowState | null; abortPendingStateChange(reason: string): void; getExternalSockets(): Set; getCompletionResult(): FlowCompletionResultCode; /** * Sets the state of the flow. State-changes are asynchroenous in principle, but can be sync if the * implementation does it so. * * State-changes can only be performed in the orders of VOID<->WAITING<->FLOWING * * The COMPLETED state is special in that it can be reached from any previous state. * However it has to be reached by calling setCompleted with some valid FlowCompletionResultCode value (not NONE). * * In principle as all state-changes are async (also the one to COMPLETED). * * Therefore, if the application wants to reach a "target" state (for example FLOWING) from an initial state VOID, * it should take care of listening to the state-change events in order to know when * to be able to set the next state-change, as to perform every asynchroneous state-change phase. * * NOTE/TODO: Generic convenience functions can easily be provided for travelling back and forth * through the flow-states. * */ set state(newState: FlowState); get state(): FlowState; protected addExternalSocket(s: Socket): Flow; protected connectWithAllExternalSockets(internalOut: OutputSocket): void; protected setCompleted(completionResult: FlowCompletionResult, error?: FlowError): void; private onStateChangePerformed_; private _onProcError; protected abstract onVoidToWaiting_(done: VoidCallback): any; protected abstract onWaitingToVoid_(done: VoidCallback): any; protected abstract onWaitingToFlowing_(done: VoidCallback): any; protected abstract onFlowingToWaiting_(done: VoidCallback): any; protected abstract onCompleted_(done: VoidCallback): any; protected abstract onStateChangeAborted_(reason: string): any; } export declare class DefaultFlow extends Flow { protected onVoidToWaiting_(done: VoidCallback): void; protected onWaitingToVoid_(done: VoidCallback): void; protected onWaitingToFlowing_(done: VoidCallback): void; protected onFlowingToWaiting_(done: VoidCallback): void; protected onCompleted_(done: VoidCallback): void; protected onStateChangeAborted_(reason: string): void; }