import { Stream, StreamWithSend } from "./Stream"; import { Vertex, Source } from "./Vertex"; import { Transaction } from "./Transaction"; export class IOAction { /*! * Convert a function that performs asynchronous I/O taking input A * and returning a value of type B into an I/O action of type * (sa : Stream) => Stream */ static fromAsync(performIO : (a : A, result : (b : B) => void) => void) : (sa : Stream) => Stream { return (sa : Stream) => { const out = new StreamWithSend(null); out.setVertex__(new Vertex("map", 0, [ new Source( sa.getVertex__(), () => { return sa.listen_(out.getVertex__(), (a : A) => { performIO(a, (b : B) => { Transaction.run(() => { out.send_(b); }); }); }, false); } ) ] )); return out; } } }