123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- interface TaskState {
- }
- namespace TaskState {
- export interface Pending { kind: 'Pending' }
- export interface Running { kind: 'Running', }
- export interface Progress {
- message: string,
- isIndeterminate: boolean,
- current: number,
- max: number,
- elapsedMs: number
- }
- }
- type ExecutionContext = {
- run<T>(c: Computation<T>, params?: { updateRateMs: number }): Promise<T>,
- subscribe(o: (p: string, compId: number) => void): void,
- requestAbort(compId: number): void
- }
- namespace ExecutionContext {
-
-
-
-
-
-
- export const Sync: ExecutionContext = 0 as any;
- }
- interface RuntimeContext {
- run<T>(c: Computation<T>, params?: { updateRateMs: number }): Promise<T>,
- yield(name: string): Promise<void> | void
- }
- interface Computation<T> { (ctx: RuntimeContext): Promise<T>, _id: number }
- function create<T>(c: (ctx: RuntimeContext) => Promise<T>): Computation<T> { return 0 as any; }
- function constant<T>(c: T) { return create(async ctx => c); }
- type MultistepFn<P, T> = (params: P, step: (s: number) => Promise<void> | void, ctx: RuntimeContext) => Promise<T>
- type ComputationProvider<P, T> = (params: P) => Computation<T>
- function MultistepComputation<P, T>(name: string, steps: string[], f: MultistepFn<P, T>): ComputationProvider<P, T> {
- return params => create(async ctx => f(params, n => ctx.yield(steps[n]), ctx));
- }
- type UniformlyChunkedFn<S> = (chunkSize: number, state: S, totalCount?: number) => number
- type UniformlyChunkedProvider<S> = (ctx: RuntimeContext, state: S) => Promise<S>
- function UniformlyChunked<S>(label: string, initialChunk: number, f: UniformlyChunkedFn<S>): UniformlyChunkedProvider<S> {
-
- return 0 as any;
- }
- type LineReaderState = { str: string, position: number, lines: string[] }
- const uniformPart = UniformlyChunked('Reading lines', 1000000, (size, state: LineReaderState) => {
- state.position += size;
- state.lines.push('');
- return 0 ;
- });
- function readLines(str: string): Computation<string[]> {
- return create(async ctx => {
- const state = (await uniformPart(ctx, { str, position: 0, lines: [] }));
- return state.lines;
- });
- }
- const prependHiToLines = MultistepComputation('Hi prepend', ['Parse input', 'Prepend Hi'], async (p: string, step, ctx) => {
- await step(0);
- const lines = await readLines(p)(ctx);
- await step(1);
- const ret = lines.map(l => 'Hi ' + l);
- return ret;
- });
- (async function() {
- const r = await ExecutionContext.Sync.run(prependHiToLines('1\n2'), { updateRateMs: 150 });
- console.log(r)
- }())
|