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

working on mol-task

parent 3d29b7e4
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
......
......@@ -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
/**
* 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 @@
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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment