*Add a streams api for use with rpc

This commit is contained in:
Derrick Hammer 2022-08-04 23:41:40 -04:00
parent e0b91b02c0
commit 26e12d19a4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 33 additions and 0 deletions

33
src/streams.ts Normal file
View File

@ -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<number, AsyncIterable<Uint8Array>>();
export function getStream(id: number): AsyncIterable<Uint8Array> | boolean {
if (!streams.has(id)) {
return false;
}
return streams.get(id) as AsyncIterable<Uint8Array>;
}
export function addStream(stream: AsyncIterable<Uint8Array>): number {
const id = nextId();
streams.set(id, stream);
return id;
}