1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import * as events from "events";
|
10 | import * as Q from "q";
|
11 | import * as stream from "stream";
|
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 | (
|
146 | task1: Strings,
|
147 | task2: Strings,
|
148 | task3: Strings,
|
149 | task4: Strings,
|
150 | task5: Strings,
|
151 | cb?: (error?: any) => any,
|
152 | ): Orchestrator;
|
153 | (
|
154 | task1: Strings,
|
155 | task2: Strings,
|
156 | task3: Strings,
|
157 | task4: Strings,
|
158 | task5: Strings,
|
159 | task6: Strings,
|
160 | cb?: (error?: any) => any,
|
161 | ): Orchestrator;
|
162 | }
|
163 |
|
164 | interface OnCallbackEvent {
|
165 | message: string;
|
166 | task: string;
|
167 | err: any;
|
168 | duration?: number | undefined;
|
169 | }
|
170 |
|
171 | interface OnAllCallbackEvent extends OnCallbackEvent {
|
172 | src: string;
|
173 | }
|
174 |
|
175 | interface Task {
|
176 | fn: TaskFunc;
|
177 | dep: string[];
|
178 | name: string;
|
179 | done?: boolean | undefined;
|
180 | duration?: number | undefined;
|
181 | hrDuration?: [number, number] | undefined;
|
182 | running?: boolean | undefined;
|
183 | }
|
184 |
|
185 | interface Meta {
|
186 | duration: number;
|
187 | hrDuration: [number, number];
|
188 | runMethod: "callback" | "catch" | "promise" | "stream" | "sync";
|
189 | }
|
190 |
|
191 | type EventNames =
|
192 | | "start"
|
193 | | "stop"
|
194 | | "err"
|
195 | | "task_start"
|
196 | | "task_stop"
|
197 | | "task_err"
|
198 | | "task_not_found"
|
199 | | "task_recursion";
|
200 | }
|
201 |
|
202 | export = Orchestrator;
|