diff --git a/src/examples/computation.ts b/src/examples/computation.ts index f82d6051aa1af4258cb06f945672476802466458..053ccb514e280272abe277ff9fc839ebf0a122c1 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 5750520653c10ae2c67e5d4d0d3755154b22eb54..f06abbf0c63a3f6bbb33b977c118256472f68512 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 0000000000000000000000000000000000000000..cda2cf15ebeff6a12ecaf4cfb6297761e2583bc2 --- /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 b8da6ea57ab4793b702e3ef0eebc2f545f8c8c14..d9626148cff8d0fd8f9053ee36fa54580aadaa76 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 9b1644ee32b9f49028d89ee845c6613e005c9c60..e0e5174a99ce79b11cb12ee30cc7194f4bdfc40e 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