-
Notifications
You must be signed in to change notification settings - Fork 173
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: main
Are you sure you want to change the base?
Changes from 3 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,48 @@ | ||
| import { Mutex } from '@livekit/mutex'; | ||
| import { mergeReadableStreams } from '@std/streams'; | ||
| 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(); | ||
|
|
||
| constructor(source: ReadableStream<T>) { | ||
| this.source = source; | ||
| this.identityStream = new IdentityTransform<T>(); | ||
| this.mergedStream = mergeReadableStreams<T>(this.source, this.identityStream.readable); | ||
| } | ||
|
|
||
| async inject(value: T) { | ||
| const unlock = await this.injectMutex.lock(); | ||
| try { | ||
| const writer = this.identityStream.writable.getWriter(); | ||
| await writer.write(value); | ||
| await writer.close(); | ||
| } finally { | ||
| unlock(); | ||
| } | ||
| } | ||
|
|
||
| async close() { | ||
| const unlock = await this.injectMutex.lock(); | ||
| try { | ||
| // this will not cancel the source stream but instead keep the readable open until the source finishes | ||
| await this.identityStream.writable.close(); | ||
| } finally { | ||
| unlock(); | ||
| } | ||
| } | ||
|
|
||
| async cancel(reason?: any) { | ||
| await Promise.all([ | ||
| this.mergedStream.cancel(reason), | ||
| this.identityStream.writable.abort(reason), | ||
| ]); | ||
| } | ||
|
|
||
| get readable() { | ||
|
Contributor
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
Contributor
Author
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; | ||
| } | ||
| } | ||
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.