task.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { RuntimeContext } from './execution/runtime-context'
  7. import { Progress } from './execution/progress'
  8. import { ExecuteObservable, ExecuteObservableChild, ExecuteInContext } from './execution/observable';
  9. import { SyncRuntimeContext } from './execution/synchronous';
  10. import { idFactory } from 'mol-util/id-factory';
  11. /** A "named function wrapper" with built in "computation tree progress tracking". */
  12. interface Task<T> {
  13. /** run the task without observation */
  14. run(): Promise<T>,
  15. /** run the task with the specified observer, default updateRate is 250ms */
  16. run(observer: Progress.Observer, updateRateMs?: number): Promise<T>,
  17. /**
  18. * Run a child task that adds a new node to the progress tree. Allows to passing the progress so
  19. * that the progress tree can be kept in a "good state" without having to separately call update.
  20. */
  21. runAsChild(ctx: RuntimeContext, progress?: string | Partial<RuntimeContext.ProgressUpdate>): Promise<T>
  22. /** Run the task on the specified context. */
  23. runInContext(ctx: RuntimeContext): Promise<T>
  24. readonly id: number,
  25. readonly name: string
  26. }
  27. namespace Task {
  28. class Impl<T> implements Task<T> {
  29. readonly id: number;
  30. run(observer?: Progress.Observer, updateRateMs?: number): Promise<T> {
  31. if (observer) return ExecuteObservable(this, observer, updateRateMs as number || 250);
  32. return this.f(SyncRuntimeContext);
  33. }
  34. runAsChild(ctx: RuntimeContext, progress?: string | Partial<RuntimeContext.ProgressUpdate>): Promise<T> {
  35. if (ctx.isSynchronous) return this.f(SyncRuntimeContext);
  36. return ExecuteObservableChild(ctx, this, progress as string | Partial<RuntimeContext.ProgressUpdate>);
  37. }
  38. runInContext(ctx: RuntimeContext): Promise<T> {
  39. if (ctx.isSynchronous) return this.f(SyncRuntimeContext);
  40. return ExecuteInContext(ctx, this);
  41. }
  42. constructor(public name: string, public f: (ctx: RuntimeContext) => Promise<T>, public onAbort?: () => void) {
  43. this.id = getNextId();
  44. }
  45. }
  46. export interface Aborted { isAborted: true, reason: string }
  47. export function isAbort(e: any): e is Aborted { return !!e && !!e.isAborted; }
  48. export function Aborted(reason: string): Aborted { return { isAborted: true, reason }; }
  49. export function create<T>(name: string, f: (ctx: RuntimeContext) => Promise<T>, onAbort?: () => void): Task<T> {
  50. return new Impl(name, f, onAbort);
  51. }
  52. export function constant<T>(name: string, value: T): Task<T> { return create(name, async ctx => value); }
  53. export function fail(name: string, reason: string): Task<any> { return create(name, async ctx => { throw new Error(reason); }); }
  54. export interface Progress {
  55. taskId: number,
  56. taskName: string,
  57. startedTime: number,
  58. message: string,
  59. canAbort: boolean,
  60. isIndeterminate: boolean,
  61. current: number,
  62. max: number
  63. }
  64. const getNextId = idFactory(0, 0x3fffffff)
  65. }
  66. export { Task }