move to correct structure

This commit is contained in:
JOLIMAITRE Matthieu 2023-12-09 00:08:48 +01:00
parent 44825c4c31
commit 54c4e91af8
12 changed files with 118 additions and 1 deletions

View file

@ -1,43 +0,0 @@
import { TextLineStream } from "https://deno.land/std@0.208.0/streams/mod.ts";
export function channel<T>() {
const inner = {
queue: [] as T[],
resolver: null as (() => void) | null,
};
const send = (item: T) => {
inner.queue.push(item);
if (inner.resolver === null) return;
inner.resolver();
inner.resolver = null;
};
const receive = async () => {
if (inner.queue.length < 1) await new Promise<void>((res) => inner.resolver = res);
const [head] = inner.queue.splice(0, 1);
return head;
};
return { send, receive };
}
export function* range(from: number, to: number) {
while (from < to) yield from++;
}
export async function exists(path: string) {
try {
await Deno.stat(path);
return true;
} catch (_) {
return false;
}
}
export async function next<T>(readable: ReadableStream<T>) {
for await (const item of readable) return item;
}
export function lines(readable: ReadableStream<Uint8Array>) {
return readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
}