1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import * as events from "events";
|
10 | import * as stream from "stream";
|
11 | import * as Q from "q";
|
12 |
|
13 |
|
14 |
|
15 | declare class Orchestrator extends events.EventEmitter {
|
16 | doneCallback?: ((error?: any) => any) | undefined;
|
17 | isRunning: boolean;
|
18 | seq: any[];
|
19 | tasks: { [name: string]: Orchestrator.Task };
|
20 |
|
21 | reset(): Orchestrator;
|
22 |
|
23 | /** Define a task
|
24 | * @param name The name of the task.
|
25 | * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete:
|
26 | * - Take in a callback
|
27 | * - Return a stream or a promise
|
28 | */
|
29 | add(name: string, fn?: Orchestrator.TaskFunc): Orchestrator;
|
30 | /** Define a task
|
31 | * @param name The name of the task.
|
32 | * @param deps An array of task names to be executed and completed before your task will run.
|
33 | * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete:
|
34 | * - Take in a callback
|
35 | * - Return a stream or a promise
|
36 | */
|
37 | add(name: string, deps?: string[], fn?: Orchestrator.TaskFunc): Orchestrator;
|
38 |
|
39 | task(name: string): Orchestrator.Task;
|
40 | task(name: string, fn: Orchestrator.TaskFunc): void;
|
41 | task(name: string, dep: string[], fn: Orchestrator.TaskFunc): void;
|
42 |
|
43 | /** Have you defined a task with this name?
|
44 | * @param name The task name to query
|
45 | */
|
46 | hasTask(name: string): boolean;
|
47 |
|
48 | start: Orchestrator.StartMethod;
|
49 |
|
50 | stop(err?: any, successfulFinish?: boolean): void;
|
51 |
|
52 | sequence: Orchestrator.Sequencify;
|
53 |
|
54 | allDone(): boolean;
|
55 |
|
56 | /** Listen to orchestrator internals
|
57 | * @param event Event name to listen to:
|
58 | * - start: from start() method, shows you the task sequence
|
59 | * - stop: from stop() method, the queue finished successfully
|
60 | * - err: from stop() method, the queue was aborted due to a task error
|
61 | * - task_start: from _runTask() method, task was started
|
62 | * - task_stop: from _runTask() method, task completed successfully
|
63 | * - task_err: from _runTask() method, task errored
|
64 | * - task_not_found: from start() method, you're trying to start a task that doesn't exist
|
65 | * - task_recursion: from start() method, there are recursive dependencies in your task list
|
66 | * @param cb Passes single argument: e: event details
|
67 | */
|
68 | on(event: Orchestrator.EventNames, cb: (e: Orchestrator.OnCallbackEvent) => any): this;
|
69 |
|
70 | /** Listen to all orchestrator events from one callback
|
71 | * @param cb Passes single argument: e: event details
|
72 | */
|
73 | onAll(cb: (e: Orchestrator.OnAllCallbackEvent) => any): void;
|
74 |
|
75 | // probably supposed to be private methods, but still available on Orchestrator prototype
|
76 | _resetTask(task: Orchestrator.Task): void;
|
77 |
|
78 | _resetAllTasks(): void;
|
79 |
|
80 | _resetSpecificTasks(names: string[]): void;
|
81 |
|
82 | _runStep(): void;
|
83 |
|
84 | _readyToRunTask(task: Orchestrator.Task): boolean;
|
85 |
|
86 | _stopTask(task: Orchestrator.Task, meta: Orchestrator.Meta): void;
|
87 |
|
88 | _emitTaskDone(task: Orchestrator.Task, message: string, err?: any): void;
|
89 |
|
90 | _runTask(task: Orchestrator.Task): void;
|
91 | }
|
92 |
|
93 | declare namespace Orchestrator {
|
94 | type Strings = string | string[];
|
95 |
|
96 | /** The method export generated by orchestrator/lib/runTask.js */
|
97 | type RunTask = (task: Orchestrator.TaskFunc, done: (err: any, meta: Orchestrator.Meta) => void) => void;
|
98 |
|
99 |
|
100 | type Sequencify = (tasks: Array<{ dep: string[]; }>, names: string[]) => {
|
101 | sequence: string[];
|
102 | missingTasks: string[];
|
103 | recursiveDependencies: string[];
|
104 | };
|
105 |
|
106 | |
107 |
|
108 | type TaskFunc = (callback: (err?: any) => void) => Q.Promise<any> | stream.Stream | any;
|
109 |
|
110 | interface AddMethod {
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | (name: string, fn?: TaskFunc): Orchestrator;
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | (name: string, deps?: string[], fn?: TaskFunc): Orchestrator;
|
126 | }
|
127 |
|
128 | |
129 |
|
130 | interface StartMethod {
|
131 | |
132 |
|
133 |
|
134 |
|
135 | (tasks: Strings, cb?: (error?: any) => any): Orchestrator;
|
136 | |
137 |
|
138 |
|
139 |
|
140 | (...tasks: Strings[]): Orchestrator;
|
141 |
|
142 | (task1: Strings, task2: Strings, cb?: (error?: any) => any): Orchestrator;
|
143 | (task1: Strings, task2: Strings, task3: Strings, cb?: (error?: any) => any): Orchestrator;
|
144 | (task1: Strings, task2: Strings, task3: Strings, task4: Strings, cb?: (error?: any) => any): Orchestrator;
|
145 | (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, cb?: (error?: any) => any): Orchestrator;
|
146 | (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, task6: Strings, cb?: (error?: any) => any): Orchestrator;
|
147 | }
|
148 |
|
149 | interface OnCallbackEvent {
|
150 | message: string;
|
151 | task: string;
|
152 | err: any;
|
153 | duration?: number | undefined;
|
154 | }
|
155 |
|
156 | interface OnAllCallbackEvent extends OnCallbackEvent {
|
157 | src: string;
|
158 | }
|
159 |
|
160 | interface Task {
|
161 | fn: TaskFunc;
|
162 | dep: string[];
|
163 | name: string;
|
164 | done?: boolean | undefined;
|
165 | duration?: number | undefined;
|
166 | hrDuration?: [number, number] | undefined;
|
167 | running?: boolean | undefined;
|
168 | }
|
169 |
|
170 | interface Meta {
|
171 | duration: number;
|
172 | hrDuration: [number, number];
|
173 | runMethod: ("callback" | "catch" | "promise" | "stream" | "sync");
|
174 | }
|
175 |
|
176 | type EventNames = ("start" | "stop" | "err" | "task_start" | "task_stop" | "task_err" | "task_not_found" | "task_recursion");
|
177 | }
|
178 |
|
179 | export = Orchestrator;
|