Skip to content
Snippets Groups Projects
Commit abcb1f54 authored by David Sehnal's avatar David Sehnal
Browse files

working on mol-task

parent 3d29b7e4
Branches
Tags
No related merge requests found
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
* @author David Sehnal <david.sehnal@gmail.com> * @author David Sehnal <david.sehnal@gmail.com>
*/ */
import { Task, ExecuteSynchronous } from 'mol-task' import { Task, Run } from 'mol-task'
async function test() { async function test() {
const t = Task.create('test', async () => 1); const t = Task.create('test', async () => 1);
const r = await ExecuteSynchronous(t); const r = await Run(t);
console.log(r); console.log(r);
} }
......
...@@ -6,22 +6,7 @@ ...@@ -6,22 +6,7 @@
import Task from '../task' import Task from '../task'
import RuntimeContext from './runtime-context' import RuntimeContext from './runtime-context'
import Progress from './progress'
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 }
}
class ObservableExecutor { class ObservableExecutor {
async run<T>(task: Task<T>): Promise<T> { async run<T>(task: Task<T>): Promise<T> {
...@@ -31,7 +16,7 @@ class ObservableExecutor { ...@@ -31,7 +16,7 @@ class ObservableExecutor {
try { try {
return await task.__f(ctx); return await task.__f(ctx);
} catch (e) { } catch (e) {
if (e === Task.Aborted) task.__onAbort(); if (Task.isAborted(e)) task.__onAbort();
throw e; throw e;
} }
} }
...@@ -44,7 +29,7 @@ class ObservableExecutor { ...@@ -44,7 +29,7 @@ class ObservableExecutor {
class ObservableRuntimeContext implements RuntimeContext { class ObservableRuntimeContext implements RuntimeContext {
id: number = 0; id: number = 0;
requiresUpdate: boolean = false; requiresUpdate: boolean = false;
update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> { update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> {
return 0 as any; return 0 as any;
} }
runChild<T>(progress: Partial<RuntimeContext.ProgressUpdate>, task: Task<T>): Promise<T> { runChild<T>(progress: Partial<RuntimeContext.ProgressUpdate>, task: Task<T>): Promise<T> {
...@@ -60,4 +45,4 @@ namespace ExecuteObservable { ...@@ -60,4 +45,4 @@ namespace ExecuteObservable {
export let PRINT_ERRORS_TO_CONSOLE = false; export let PRINT_ERRORS_TO_CONSOLE = false;
} }
export { ExecuteObservable, Progress } export default ExecuteObservable
\ No newline at end of file \ No newline at end of file
/**
* 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
...@@ -7,5 +7,14 @@ ...@@ -7,5 +7,14 @@
import Task from './task' import Task from './task'
import RuntimeContext from './execution/runtime-context' import RuntimeContext from './execution/runtime-context'
import ExecuteSynchronous from './execution/synchronous' import ExecuteSynchronous from './execution/synchronous'
import ExecuteObservable from './execution/observable'
import Progress from './execution/progress'
export { Task, RuntimeContext, ExecuteSynchronous } function Run<T>(task: Task<T>): Promise<T>;
\ No newline at end of file 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
...@@ -6,15 +6,20 @@ ...@@ -6,15 +6,20 @@
import RuntimeContext from './execution/runtime-context' import RuntimeContext from './execution/runtime-context'
// Run(t, ?observer, ?updateRate) to execute
interface Task<T> { interface Task<T> {
readonly id: number, readonly id: number,
readonly name: string, readonly name: string,
// Do not call this directly, use Run.
readonly __f: (ctx: RuntimeContext) => Promise<T>, readonly __f: (ctx: RuntimeContext) => Promise<T>,
// Do not call this directly, use Run.
readonly __onAbort: (() => void) | undefined readonly __onAbort: (() => void) | undefined
} }
namespace Task { 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> { export function create<T>(name: string, f: (ctx: RuntimeContext) => Promise<T>, onAbort?: () => void): Task<T> {
return { id: nextId(), name, __f: f, __onAbort: onAbort }; return { id: nextId(), name, __f: f, __onAbort: onAbort };
...@@ -33,29 +38,15 @@ namespace Task { ...@@ -33,29 +38,15 @@ namespace Task {
export type Progress = IndeterminateProgress | DeterminateProgress export type Progress = IndeterminateProgress | DeterminateProgress
interface ProgressBase { interface ProgressBase {
runtimeId: number, rootTaskId: number,
taskId: number, taskId: number,
message: string, message: string,
elapsedMs: { real: number, cpu: number }, elapsedMs: { real: number, cpu: number },
canAbort: boolean, canAbort: boolean
children?: Progress[]
} }
export interface IndeterminateProgress extends ProgressBase { isIndeterminate: true } export interface IndeterminateProgress extends ProgressBase { isIndeterminate: true }
export interface DeterminateProgress extends ProgressBase { isIndeterminate: false, current: number, max: number } 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 export default Task
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment