From abcb1f5481ff72d53bead137f9a02c7cddc02a49 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Thu, 7 Dec 2017 20:46:37 +0100
Subject: [PATCH] working on mol-task

---
 src/examples/computation.ts          |  4 ++--
 src/mol-task/execution/observable.ts | 23 ++++-------------------
 src/mol-task/execution/progress.ts   | 25 +++++++++++++++++++++++++
 src/mol-task/index.ts                | 11 ++++++++++-
 src/mol-task/task.ts                 | 25 ++++++++-----------------
 5 files changed, 49 insertions(+), 39 deletions(-)
 create mode 100644 src/mol-task/execution/progress.ts

diff --git a/src/examples/computation.ts b/src/examples/computation.ts
index f82d6051a..053ccb514 100644
--- a/src/examples/computation.ts
+++ b/src/examples/computation.ts
@@ -4,11 +4,11 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import { Task, ExecuteSynchronous } from 'mol-task'
+import { Task, Run } from 'mol-task'
 
 async function test() {
     const t = Task.create('test', async () => 1);
-    const r = await ExecuteSynchronous(t);
+    const r = await Run(t);
     console.log(r);
 }
 
diff --git a/src/mol-task/execution/observable.ts b/src/mol-task/execution/observable.ts
index 575052065..f06abbf0c 100644
--- a/src/mol-task/execution/observable.ts
+++ b/src/mol-task/execution/observable.ts
@@ -6,22 +6,7 @@
 
 import Task from '../task'
 import RuntimeContext from './runtime-context'
-
-interface Progress {
-    taskId: number,
-    elapsedMs: { real: number, cpu: number },
-    tree: Progress.Node,
-    tryAbort?: () => void
-}
-
-namespace Progress {
-    export interface Node {
-        readonly progress: Task.Progress,
-        readonly children: ReadonlyArray<Node>
-    }
-
-    export interface Observer { (progress: Progress): void }
-}
+import Progress from './progress'
 
 class ObservableExecutor {
     async run<T>(task: Task<T>): Promise<T> {
@@ -31,7 +16,7 @@ class ObservableExecutor {
         try {
             return await task.__f(ctx);
         } catch (e) {
-            if (e === Task.Aborted) task.__onAbort();
+            if (Task.isAborted(e)) task.__onAbort();
             throw e;
         }
     }
@@ -44,7 +29,7 @@ class ObservableExecutor {
 class ObservableRuntimeContext implements RuntimeContext {
     id: number = 0;
     requiresUpdate: boolean = false;
-    update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> { 
+    update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> {
         return 0 as any;
     }
     runChild<T>(progress: Partial<RuntimeContext.ProgressUpdate>, task: Task<T>): Promise<T> {
@@ -60,4 +45,4 @@ namespace ExecuteObservable {
     export let PRINT_ERRORS_TO_CONSOLE = false;
 }
 
-export { ExecuteObservable, Progress }
\ No newline at end of file
+export default ExecuteObservable
\ No newline at end of file
diff --git a/src/mol-task/execution/progress.ts b/src/mol-task/execution/progress.ts
new file mode 100644
index 000000000..cda2cf15e
--- /dev/null
+++ b/src/mol-task/execution/progress.ts
@@ -0,0 +1,25 @@
+/**
+ * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import Task from '../task'
+
+interface Progress {
+    taskId: number,
+    elapsedMs: { real: number, cpu: number },
+    tree: Progress.Node,
+    tryAbort?: (reason?: string) => void
+}
+
+namespace Progress {
+    export interface Node {
+        readonly progress: Task.Progress,
+        readonly children: ReadonlyArray<Node>
+    }
+
+    export interface Observer { (progress: Progress): void }
+}
+
+export default Progress
\ No newline at end of file
diff --git a/src/mol-task/index.ts b/src/mol-task/index.ts
index b8da6ea57..d9626148c 100644
--- a/src/mol-task/index.ts
+++ b/src/mol-task/index.ts
@@ -7,5 +7,14 @@
 import Task from './task'
 import RuntimeContext from './execution/runtime-context'
 import ExecuteSynchronous from './execution/synchronous'
+import ExecuteObservable from './execution/observable'
+import Progress from './execution/progress'
 
-export { Task, RuntimeContext, ExecuteSynchronous }
\ No newline at end of file
+function Run<T>(task: Task<T>): Promise<T>;
+function Run<T>(task: Task<T>, observer: Progress.Observer, updateRateMs?: number): Promise<T>;
+function Run<T>(task: Task<T>, observer?: Progress.Observer, updateRateMs?: number): Promise<T> {
+    if (observer) return ExecuteObservable(task, observer, updateRateMs || 250);
+    return ExecuteSynchronous(task);
+}
+
+export { Task, RuntimeContext, Progress, Run }
\ No newline at end of file
diff --git a/src/mol-task/task.ts b/src/mol-task/task.ts
index 9b1644ee3..e0e5174a9 100644
--- a/src/mol-task/task.ts
+++ b/src/mol-task/task.ts
@@ -6,15 +6,20 @@
 
 import RuntimeContext from './execution/runtime-context'
 
+// Run(t, ?observer, ?updateRate) to execute
 interface Task<T> {
     readonly id: number,
     readonly name: string,
+    // Do not call this directly, use Run.
     readonly __f: (ctx: RuntimeContext) => Promise<T>,
+    // Do not call this directly, use Run.
     readonly __onAbort: (() => void) | undefined
 }
 
 namespace Task {
-    export const Aborted = 'Aborted.';
+    export interface Aborted { isAborted: true, reason: string }
+    export function isAborted(e: any): e is Aborted { return !!e && !!e.isAborted; }
+    export function Aborted(reason: string): Aborted { return { isAborted: true, reason }; }
 
     export function create<T>(name: string, f: (ctx: RuntimeContext) => Promise<T>, onAbort?: () => void): Task<T> {
         return { id: nextId(), name, __f: f, __onAbort: onAbort };
@@ -33,29 +38,15 @@ namespace Task {
     export type Progress = IndeterminateProgress | DeterminateProgress
 
     interface ProgressBase {
-        runtimeId: number,
+        rootTaskId: number,
         taskId: number,
         message: string,
         elapsedMs: { real: number, cpu: number },
-        canAbort: boolean,
-        children?: Progress[]
+        canAbort: boolean
     }
 
     export interface IndeterminateProgress extends ProgressBase { isIndeterminate: true }
     export interface DeterminateProgress extends ProgressBase { isIndeterminate: false, current: number, max: number }
-
-    export interface State {
-        runtimeId: number,
-        taskId: number,
-
-        message: string,
-        elapsedMs: number,
-        canAbort: boolean,
-        isIndeterminate: boolean,
-        current: number,
-        max: number,
-        children?: State[]
-    }
 }
 
 export default Task
\ No newline at end of file
-- 
GitLab