move to correct structure
This commit is contained in:
parent
44825c4c31
commit
54c4e91af8
12 changed files with 118 additions and 1 deletions
43
instance/src/lib/utils.ts
Normal file
43
instance/src/lib/utils.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
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());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue