From 26e12d19a4df6954f03e8605033e2736f9d2150c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 4 Aug 2022 23:41:40 -0400 Subject: [PATCH] *Add a streams api for use with rpc --- src/streams.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/streams.ts diff --git a/src/streams.ts b/src/streams.ts new file mode 100644 index 0000000..9f465c0 --- /dev/null +++ b/src/streams.ts @@ -0,0 +1,33 @@ +function idFactory(start = 1, step = 1, limit = 2 ** 32) { + let id = start; + + return function nextId() { + const nextId = id; + id += step; + if (id >= limit) id = start; + return nextId; + }; +} + +export interface StreamFileResponse { + data?: Uint8Array; + done: boolean; +} + +const nextId = idFactory(1); +const streams = new Map>(); + +export function getStream(id: number): AsyncIterable | boolean { + if (!streams.has(id)) { + return false; + } + + return streams.get(id) as AsyncIterable; +} + +export function addStream(stream: AsyncIterable): number { + const id = nextId(); + streams.set(id, stream); + + return id; +}