import * as dntShim from "./_dnt.shims.js"; import { createChannel, createSignal, each, type Operation, race, resource, spawn, type Stream, until, } from "effection"; import { createApi } from "@effectionx/context-api"; export const initStdin = resource>( function* (provide) { let channel = createChannel(); // todo, use node/process directly. let iterator = dntShim.Deno.stdin.readable[Symbol.asyncIterator](); yield* spawn(function* () { let next = yield* until(iterator.next()); while (!next.done) { yield* channel.send(next.value); next = yield* until(iterator.next()); } yield* channel.close(); }); yield* race([provide(channel), drain(channel)]); }, ); export const Stdin = createApi("@statical/stdin", { *useStdin() { return createSignal() as Stream; }, }); export const { useStdin } = Stdin.operations; function* drain(stream: Stream): Operation { for (let _ of yield* each(stream)) { yield* each.next(); } }