-
Notifications
You must be signed in to change notification settings - Fork 132
wip injectable stream #408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev-1.0
Are you sure you want to change the base?
Changes from all commits
9767294
12127f1
708b9ae
7a523c5
465b4ec
257b467
4a62e54
5165585
60d47c5
e9111f7
141f4cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Mutex } from '@livekit/mutex'; | ||
import { mergeReadableStreams } from '@std/streams'; | ||
import type { ReadableStream } from 'node:stream/web'; | ||
import { IdentityTransform } from './identity_transform.js'; | ||
|
||
export class InjectableStream<T> { | ||
private source: ReadableStream<T>; | ||
private identityStream: IdentityTransform<T>; | ||
private mergedStream: ReadableStream<T>; | ||
private injectMutex = new Mutex(); | ||
private writer: WritableStreamDefaultWriter<T>; | ||
private closed = false; | ||
|
||
constructor(source: ReadableStream<T>) { | ||
this.source = source; | ||
this.identityStream = new IdentityTransform<T>(); | ||
this.mergedStream = mergeReadableStreams<T>(this.source, this.identityStream.readable); | ||
this.writer = this.identityStream.writable.getWriter(); | ||
} | ||
|
||
async inject(value: T) { | ||
const unlock = await this.injectMutex.lock(); | ||
|
||
if (this.closed) { | ||
throw new Error('Cannot inject into a closed stream'); | ||
} | ||
|
||
try { | ||
await this.writer.write(value); | ||
} finally { | ||
unlock(); | ||
} | ||
} | ||
|
||
async close() { | ||
const unlock = await this.injectMutex.lock(); | ||
|
||
if (this.closed) { | ||
return; | ||
} | ||
|
||
try { | ||
// this will not cancel the source stream but instead keep the readable open until the source finishes | ||
this.writer.releaseLock(); | ||
await this.identityStream.writable.close(); | ||
this.closed = true; | ||
} finally { | ||
unlock(); | ||
} | ||
} | ||
|
||
async cancel(reason?: any) { | ||
await this.close(); | ||
await Promise.all([ | ||
this.mergedStream.cancel(reason), | ||
this.identityStream.writable.abort(reason), | ||
]); | ||
} | ||
|
||
get readable() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we also consider supporting "eject" (read single value from stream) operation? Basically making stream also have a queue-like API with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure if there's a concrete use case? I'd be in favor of implementing additional features (generally) only once we know we have a defined use case for it. |
||
return this.mergedStream; | ||
} | ||
} | ||
|
||
// // Copied from @std/streams/merge-readable-streams.ts to avoid incompetible ReadableStream types | ||
// export function mergeReadableStreams<T>( | ||
// ...streams: ReadableStream<T>[] | ||
// ): ReadableStream<T> { | ||
// const resolvePromises = streams.map(() => Promise.withResolvers<void>()); | ||
// return new ReadableStream<T>({ | ||
// start(controller) { | ||
// let mustClose = false; | ||
// Promise.all(resolvePromises.map(({ promise }) => promise)) | ||
// .then(() => { | ||
// controller.close(); | ||
// }) | ||
// .catch((error) => { | ||
// mustClose = true; | ||
// controller.error(error); | ||
// }); | ||
// for (const [index, stream] of streams.entries()) { | ||
// (async () => { | ||
// try { | ||
// for await (const data of stream) { | ||
// if (mustClose) { | ||
// break; | ||
// } | ||
// controller.enqueue(data); | ||
// } | ||
// resolvePromises[index]!.resolve(); | ||
// } catch (error) { | ||
// resolvePromises[index]!.reject(error); | ||
// } | ||
// })(); | ||
// } | ||
// }, | ||
// }); | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@toubatbrian might be worth taking over this PR and working on the releasing the lock on the readable source once we close the stream.
Some tests would be good to.