mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 10:53:50 +02:00
Add BufferTarget.onFinalize, OutputOptions.onFinalize, ConcurrentRunner (#349)
* Add BufferTarget.onFinalize option for awaitable async action on finalize The `finalized` event fires synchronously and its return value is ignored, making it unsuitable for use cases where the muxer should wait (e.g. uploading the buffer to S3, R2, or other object stores that require a known Content-Length and therefore can't stream via StreamTarget). Adds a new `BufferTargetOptions` type with an `onFinalize` callback that the muxer awaits before resolving. Matches the existing callback pattern used by `HlsOutputFormatOptions.onSegment`, `onMaster`, etc. When used with PathedTarget, this provides proper backpressure: the next segment won't start being produced until the previous one has finished uploading, keeping memory bounded regardless of video length. - Adds `BufferTargetOptions` type, exported from the package root - `BufferTarget` constructor now accepts optional options (backward compatible) - `_finalize()` awaits `onFinalize` before emitting the `finalized` event - Adds runtime validation for non-function callbacks - Updates "Upload to a server" docs in writing-hls.md with S3 pattern - Documents `onFinalize` in writing-media-files.md BufferTarget section - Adds tests for callback invocation, async awaiting, backward compat, and validation * Add ConcurrentRunner, add OutputOptions.onFinalize, adjust docs accordingly, clean up tests * give me more control baby * forgot to tell about it * polish guide and js docs * vanilagy says remove this, docs will reveal it --------- Co-authored-by: Vanilagy <[email protected]>
This commit is contained in:
co-authored by
Vanilagy
parent
9a7120f5f4
commit
77e70f9090
+63
@@ -1235,3 +1235,66 @@ export class EventEmitter<TEvents extends Record<string, unknown>> {
|
||||
}
|
||||
|
||||
export const ceilToMultipleOfTwo = (value: number) => Math.ceil(value / 2) * 2;
|
||||
|
||||
/**
|
||||
* Utility class for running async functions in parallel up to a certain level of parallelism. Can be used to apply
|
||||
* backpressure only if the concurrency level would be exceeded.
|
||||
*
|
||||
* @group Miscellaneous
|
||||
* @public
|
||||
*/
|
||||
export class ConcurrentRunner {
|
||||
/** @internal */
|
||||
_queue: Promise<unknown>[] = [];
|
||||
/** @internal */
|
||||
_errored = false;
|
||||
|
||||
/**
|
||||
* The maximum number of in-flight promises. You can also think of it as the "high water mark".
|
||||
* You can set this value to dynamically change the level of parallelism.
|
||||
*/
|
||||
parallelism: number;
|
||||
|
||||
constructor(parallelism: number) {
|
||||
this.parallelism = parallelism;
|
||||
}
|
||||
|
||||
/** Whether any function has errored. The runner is effectively bricked if this is `true`, by design. */
|
||||
get errored() {
|
||||
return this._errored;
|
||||
}
|
||||
|
||||
/** The number of tasks currently running. */
|
||||
get inFlightCount() {
|
||||
return this._queue.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules an async function to be run. If the maximum allowed level of parallelism has not yet been reached,
|
||||
* the function will be executed immediately and `run()` will resolve immediately. Otherwise, the function will be
|
||||
* called as soon as any currently-running function finishes, and `run()` will only resolve then.
|
||||
*
|
||||
* Throws if the runner is errored.
|
||||
*/
|
||||
async run(fn: () => Promise<unknown>) {
|
||||
if (this._errored) {
|
||||
await Promise.race(this._queue); // Will surface the error
|
||||
}
|
||||
|
||||
while (this._queue.length >= this.parallelism) {
|
||||
await Promise.race(this._queue);
|
||||
}
|
||||
|
||||
const promise = fn();
|
||||
this._queue.push(promise);
|
||||
|
||||
void promise
|
||||
.then(() => removeItem(this._queue, promise))
|
||||
.catch(() => this._errored = true);
|
||||
}
|
||||
|
||||
/** Waits for all currently running functions to finish. Throws if the runner is errored. */
|
||||
async flush() {
|
||||
await Promise.all(this._queue);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user