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

mol-comp prototype tweak

parent 562cb9ac
No related branches found
No related tags found
No related merge requests found
type ExecutionContext = { getRuntimeCtx(c: Computation<any>): RuntimeContext, subscribe(o: (p: string, compId: number) => void): void } type ExecutionContext = {
run<T>(c: Computation<T>, params?: { updateRateMs: number }): Promise<T>,
subscribe(o: (p: string, compId: number) => void): void
}
namespace ExecutionContext {
// export interface Synchronous extends ExecutionContext {
// run<T>(c: Computation<T>, params?: { updateRateMs: number }): Promise<T>,
// }
type RuntimeContext = { yield(name: string): Promise<void> | void, createChildContext(): RuntimeContext } // export interface Observable extends ExecutionContext {
// run<T>(c: Computation<T>, params?: { updateRateMs: number }): Promise<T>,
// }
export const Sync: ExecutionContext = 0 as any;
}
interface RuntimeContext extends ExecutionContext {
yield(name: string): Promise<void> | void
}
// if no context is specified, use the synchronous one.
type Computation<T> = { (ctx?: RuntimeContext): Promise<T>, _id: number } type Computation<T> = { (ctx?: RuntimeContext): Promise<T>, _id: number }
function create<T>(c: (ctx: RuntimeContext) => Promise<T>): Computation<T> { return 0 as any; } 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 MultistepFn<P, T> = (params: P, step: (s: number) => Promise<void> | void, ctx: RuntimeContext) => Promise<T>
type ComputationProvider<P, T> = (params: P) => Computation<T> type ComputationProvider<P, T> = (params: P) => Computation<T>
...@@ -40,7 +58,7 @@ function readLines(str: string): Computation<string[]> { ...@@ -40,7 +58,7 @@ function readLines(str: string): Computation<string[]> {
const prependHiToLines = MultistepComputation('Hi prepend', ['Parse input', 'Prepend Hi'], async (p: string, step, ctx) => { const prependHiToLines = MultistepComputation('Hi prepend', ['Parse input', 'Prepend Hi'], async (p: string, step, ctx) => {
await step(0); await step(0);
const lines = await readLines(p)(ctx); const lines = await ctx.run(readLines(p));
await step(1); await step(1);
const ret = lines.map(l => 'Hi ' + l); const ret = lines.map(l => 'Hi ' + l);
return ret; return ret;
...@@ -48,6 +66,6 @@ const prependHiToLines = MultistepComputation('Hi prepend', ['Parse input', 'Pre ...@@ -48,6 +66,6 @@ const prependHiToLines = MultistepComputation('Hi prepend', ['Parse input', 'Pre
(async function() { (async function() {
const r = await prependHiToLines('1\n2')(); const r = await ExecutionContext.Sync.run(prependHiToLines('1\n2'), { updateRateMs: 150 });
console.log(r) console.log(r)
}()) }())
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