From 8a1945e638a64ac9985e558420b9d1d31148b691 Mon Sep 17 00:00:00 2001 From: tkQubo Date: Fri, 21 Aug 2015 04:05:44 +0900 Subject: [PATCH 1/4] Add Orchestrator --- orchestrator/orchestrator-test.ts | 106 ++++++++++++++++++++++++++ orchestrator/orchestrator.d.ts | 122 ++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 orchestrator/orchestrator-test.ts create mode 100644 orchestrator/orchestrator.d.ts diff --git a/orchestrator/orchestrator-test.ts b/orchestrator/orchestrator-test.ts new file mode 100644 index 000000000..af9af745c --- /dev/null +++ b/orchestrator/orchestrator-test.ts @@ -0,0 +1,106 @@ +/// +/// + +'use strict'; + +import Orchestrator from 'orchestrator'; + +var orchestrator = new Orchestrator(); + + +// API: + +// +// orchestrator.add(name[, deps][, function]); +// + +orchestrator.add('thing1', function() { + // do stuff +}); +orchestrator.add('thing2', function() { + // do stuff +}); +orchestrator.add('mytask', ['array', 'of', 'task', 'names'], function() { + // Do stuff +}); +orchestrator.add('thing2', function(callback){ + var err: any = null; + // do stuff + callback(err); +}); + + +var Q = require('q'); + +orchestrator.add('thing3', function(){ + var deferred = Q.defer(); + + // do async stuff + setTimeout(function () { + deferred.resolve(); + }, 1); + + return deferred.promise; +}); + + +//TODO: map-stream currently not on DefinitelyTyped +//var map = require('map-stream'); +// +//orchestrator.add('thing4', function(){ +// var stream = map(function (args, cb) { +// cb(null, args); +// }); +// // do stream stuff +// return stream; +//}); + +// +// orchestrator.hasTask(name); +// + +orchestrator.hasTask('thing1'); + +// +// orchestrator.start(tasks...[, cb]); +// + +orchestrator.start('thing1', 'thing2', 'thing3', 'thing4', function (err) { + // all done +}); +orchestrator.start(['thing1','thing2'], ['thing3','thing4']); + + +// +// orchestrator.stop() +// + +orchestrator.stop(); + +// +// orchestrator.on(event, cb); +// + +orchestrator.on('task_start', function (e) { + var message: string = e.message; + var task: string = e.task; + var err: any = e.err; +}); +orchestrator.on('task_stop', function (e) { + var message: string = e.message; + var task: string = e.task; + var duration: number = e.duration; +}); + +// +// orchestrator.onAll(cb); +// + +orchestrator.onAll(function (e) { + var message: string = e.message; + var task: string = e.task; + var err: any = e.err; + var src: string = e.src; +}); + + diff --git a/orchestrator/orchestrator.d.ts b/orchestrator/orchestrator.d.ts new file mode 100644 index 000000000..2948aa927 --- /dev/null +++ b/orchestrator/orchestrator.d.ts @@ -0,0 +1,122 @@ +// Type definitions for Orchestrator +// Project: https://github.com/orchestrator/orchestrator +// Definitions by: Qubo +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare type Strings = string|string[]; + +export interface AddMethodCallback { + /** + * Accept a callback + * @param callback + */ + (callback?: Function): any; + /** + * Return a promise + */ + (): Q.Promise; + /** + * Return a stream: (task is marked complete when stream ends) + */ + (): any; //TODO: stream type should be here e.g. map-stream +} + +/** + * Define a task + */ +export interface AddMethod { + /** + * Define a task + * @param name The name of the task. + * @param deps An array of task names to be executed and completed before your task will run. + * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: + *
    + *
  • Take in a callback
  • + *
  • Return a stream or a promise
  • + *
+ */ + (name: string, deps?: string[], fn?: AddMethodCallback|Function): Orchestrator; + /** + * Define a task + * @param name The name of the task. + * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: + *
    + *
  • Take in a callback
  • + *
  • Return a stream or a promise
  • + *
+ */ + (name: string, fn?: AddMethodCallback|Function): Orchestrator; +} + +/** + * Start running the tasks + */ +export interface StartMethod { + /** + * Start running the tasks + * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments. + * @param cb Callback to call after run completed. + */ + (tasks: Strings, cb?: (error?: any) => any): Orchestrator; + /** + * Start running the tasks + * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments. + * @param cb Callback to call after run completed. + */ + (...tasks: Strings[]/*, cb?: (error: any) => any */): Orchestrator; + //TODO: TypeScript 1.5.3 cannot express varargs followed by callback as a last argument... + (task1: Strings, task2: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, task4: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, task6: Strings, cb?: (error?: any) => any): Orchestrator; +} + +export interface OnCallbackEvent { + message: string; + task: string; + err: any; + duration?: number; +} + +export interface OnAllCallbackEvent extends OnCallbackEvent { + src: string; +} + +declare class Orchestrator { + add: AddMethod; + /** + * Have you defined a task with this name? + * @param name The task name to query + */ + hasTask(name: string): boolean; + start: StartMethod; + stop(): void; + + /** + * Listen to orchestrator internals + * @param event Event name to listen to: + *
    + *
  • start: from start() method, shows you the task sequence + *
  • stop: from stop() method, the queue finished successfully + *
  • err: from stop() method, the queue was aborted due to a task error + *
  • task_start: from _runTask() method, task was started + *
  • task_stop: from _runTask() method, task completed successfully + *
  • task_err: from _runTask() method, task errored + *
  • task_not_found: from start() method, you're trying to start a task that doesn't exist + *
  • task_recursion: from start() method, there are recursive dependencies in your task list + *
+ * @param cb Passes single argument: e: event details + */ + on(event: string, cb: (e: OnCallbackEvent) => any): Orchestrator; + + /** + * Listen to all orchestrator events from one callback + * @param cb Passes single argument: e: event details + */ + onAll(cb: (e: OnAllCallbackEvent) => any): void; +} + +export default Orchestrator; From 8cc0285d3428eed76c01a9c76b27238b59d7ace8 Mon Sep 17 00:00:00 2001 From: tkQubo Date: Fri, 21 Aug 2015 04:13:03 +0900 Subject: [PATCH 2/4] Add missing type annotations --- orchestrator/orchestrator-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/orchestrator/orchestrator-test.ts b/orchestrator/orchestrator-test.ts index af9af745c..047e00047 100644 --- a/orchestrator/orchestrator-test.ts +++ b/orchestrator/orchestrator-test.ts @@ -23,7 +23,7 @@ orchestrator.add('thing2', function() { orchestrator.add('mytask', ['array', 'of', 'task', 'names'], function() { // Do stuff }); -orchestrator.add('thing2', function(callback){ +orchestrator.add('thing2', function(callback: any){ var err: any = null; // do stuff callback(err); @@ -65,7 +65,7 @@ orchestrator.hasTask('thing1'); // orchestrator.start(tasks...[, cb]); // -orchestrator.start('thing1', 'thing2', 'thing3', 'thing4', function (err) { +orchestrator.start('thing1', 'thing2', 'thing3', 'thing4', function (err: any) { // all done }); orchestrator.start(['thing1','thing2'], ['thing3','thing4']); From 422b006d39dd37fc667ce5464967ee3ff6135092 Mon Sep 17 00:00:00 2001 From: tkQubo Date: Fri, 21 Aug 2015 12:06:21 +0900 Subject: [PATCH 3/4] Use external module --- orchestrator/orchestrator-test.ts | 3 +- orchestrator/orchestrator.d.ts | 229 +++++++++++++++--------------- 2 files changed, 119 insertions(+), 113 deletions(-) diff --git a/orchestrator/orchestrator-test.ts b/orchestrator/orchestrator-test.ts index 047e00047..6f1a4a79b 100644 --- a/orchestrator/orchestrator-test.ts +++ b/orchestrator/orchestrator-test.ts @@ -3,7 +3,7 @@ 'use strict'; -import Orchestrator from 'orchestrator'; +import Orchestrator = require('orchestrator'); var orchestrator = new Orchestrator(); @@ -104,3 +104,4 @@ orchestrator.onAll(function (e) { }); + diff --git a/orchestrator/orchestrator.d.ts b/orchestrator/orchestrator.d.ts index 2948aa927..24ebec8cb 100644 --- a/orchestrator/orchestrator.d.ts +++ b/orchestrator/orchestrator.d.ts @@ -7,116 +7,121 @@ declare type Strings = string|string[]; -export interface AddMethodCallback { - /** - * Accept a callback - * @param callback - */ - (callback?: Function): any; - /** - * Return a promise - */ - (): Q.Promise; - /** - * Return a stream: (task is marked complete when stream ends) - */ - (): any; //TODO: stream type should be here e.g. map-stream +declare module "orchestrator" { + class Orchestrator { + add: Orchestrator.AddMethod; + /** + * Have you defined a task with this name? + * @param name The task name to query + */ + hasTask(name: string): boolean; + start: Orchestrator.StartMethod; + stop(): void; + + /** + * Listen to orchestrator internals + * @param event Event name to listen to: + *
    + *
  • start: from start() method, shows you the task sequence + *
  • stop: from stop() method, the queue finished successfully + *
  • err: from stop() method, the queue was aborted due to a task error + *
  • task_start: from _runTask() method, task was started + *
  • task_stop: from _runTask() method, task completed successfully + *
  • task_err: from _runTask() method, task errored + *
  • task_not_found: from start() method, you're trying to start a task that doesn't exist + *
  • task_recursion: from start() method, there are recursive dependencies in your task list + *
+ * @param cb Passes single argument: e: event details + */ + on(event: string, cb: (e: Orchestrator.OnCallbackEvent) => any): Orchestrator; + + /** + * Listen to all orchestrator events from one callback + * @param cb Passes single argument: e: event details + */ + onAll(cb: (e: Orchestrator.OnAllCallbackEvent) => any): void; + } + + namespace Orchestrator { + interface AddMethodCallback { + /** + * Accept a callback + * @param callback + */ + (callback?: Function): any; + /** + * Return a promise + */ + (): Q.Promise; + /** + * Return a stream: (task is marked complete when stream ends) + */ + (): any; //TODO: stream type should be here e.g. map-stream + } + + /** + * Define a task + */ + interface AddMethod { + /** + * Define a task + * @param name The name of the task. + * @param deps An array of task names to be executed and completed before your task will run. + * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: + *
    + *
  • Take in a callback
  • + *
  • Return a stream or a promise
  • + *
+ */ + (name: string, deps?: string[], fn?: AddMethodCallback|Function): Orchestrator; + /** + * Define a task + * @param name The name of the task. + * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: + *
    + *
  • Take in a callback
  • + *
  • Return a stream or a promise
  • + *
+ */ + (name: string, fn?: AddMethodCallback|Function): Orchestrator; + } + + /** + * Start running the tasks + */ + interface StartMethod { + /** + * Start running the tasks + * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments. + * @param cb Callback to call after run completed. + */ + (tasks: Strings, cb?: (error?: any) => any): Orchestrator; + /** + * Start running the tasks + * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments. + * @param cb Callback to call after run completed. + */ + (...tasks: Strings[]/*, cb?: (error: any) => any */): Orchestrator; + //TODO: TypeScript 1.5.3 cannot express varargs followed by callback as a last argument... + (task1: Strings, task2: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, task4: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, cb?: (error?: any) => any): Orchestrator; + (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, task6: Strings, cb?: (error?: any) => any): Orchestrator; + } + + interface OnCallbackEvent { + message: string; + task: string; + err: any; + duration?: number; + } + + interface OnAllCallbackEvent extends OnCallbackEvent { + src: string; + } + + } + + export = Orchestrator; } - -/** - * Define a task - */ -export interface AddMethod { - /** - * Define a task - * @param name The name of the task. - * @param deps An array of task names to be executed and completed before your task will run. - * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: - *
    - *
  • Take in a callback
  • - *
  • Return a stream or a promise
  • - *
- */ - (name: string, deps?: string[], fn?: AddMethodCallback|Function): Orchestrator; - /** - * Define a task - * @param name The name of the task. - * @param fn The function that performs the task's operations. For asynchronous tasks, you need to provide a hint when the task is complete: - *
    - *
  • Take in a callback
  • - *
  • Return a stream or a promise
  • - *
- */ - (name: string, fn?: AddMethodCallback|Function): Orchestrator; -} - -/** - * Start running the tasks - */ -export interface StartMethod { - /** - * Start running the tasks - * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments. - * @param cb Callback to call after run completed. - */ - (tasks: Strings, cb?: (error?: any) => any): Orchestrator; - /** - * Start running the tasks - * @param tasks Tasks to be executed. You may pass any number of tasks as individual arguments. - * @param cb Callback to call after run completed. - */ - (...tasks: Strings[]/*, cb?: (error: any) => any */): Orchestrator; - //TODO: TypeScript 1.5.3 cannot express varargs followed by callback as a last argument... - (task1: Strings, task2: Strings, cb?: (error?: any) => any): Orchestrator; - (task1: Strings, task2: Strings, task3: Strings, cb?: (error?: any) => any): Orchestrator; - (task1: Strings, task2: Strings, task3: Strings, task4: Strings, cb?: (error?: any) => any): Orchestrator; - (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, cb?: (error?: any) => any): Orchestrator; - (task1: Strings, task2: Strings, task3: Strings, task4: Strings, task5: Strings, task6: Strings, cb?: (error?: any) => any): Orchestrator; -} - -export interface OnCallbackEvent { - message: string; - task: string; - err: any; - duration?: number; -} - -export interface OnAllCallbackEvent extends OnCallbackEvent { - src: string; -} - -declare class Orchestrator { - add: AddMethod; - /** - * Have you defined a task with this name? - * @param name The task name to query - */ - hasTask(name: string): boolean; - start: StartMethod; - stop(): void; - - /** - * Listen to orchestrator internals - * @param event Event name to listen to: - *
    - *
  • start: from start() method, shows you the task sequence - *
  • stop: from stop() method, the queue finished successfully - *
  • err: from stop() method, the queue was aborted due to a task error - *
  • task_start: from _runTask() method, task was started - *
  • task_stop: from _runTask() method, task completed successfully - *
  • task_err: from _runTask() method, task errored - *
  • task_not_found: from start() method, you're trying to start a task that doesn't exist - *
  • task_recursion: from start() method, there are recursive dependencies in your task list - *
- * @param cb Passes single argument: e: event details - */ - on(event: string, cb: (e: OnCallbackEvent) => any): Orchestrator; - - /** - * Listen to all orchestrator events from one callback - * @param cb Passes single argument: e: event details - */ - onAll(cb: (e: OnAllCallbackEvent) => any): void; -} - -export default Orchestrator; From 00db3ad4b0261ff513a7b688263fffb70bdfc972 Mon Sep 17 00:00:00 2001 From: tkQubo Date: Sat, 22 Aug 2015 00:49:45 +0900 Subject: [PATCH 4/4] Rename test file --- orchestrator/{orchestrator-test.ts => orchestrator-tests.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename orchestrator/{orchestrator-test.ts => orchestrator-tests.ts} (100%) diff --git a/orchestrator/orchestrator-test.ts b/orchestrator/orchestrator-tests.ts similarity index 100% rename from orchestrator/orchestrator-test.ts rename to orchestrator/orchestrator-tests.ts