mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-27 10:53:50 +02:00
Add Target.onwrite callback
This commit is contained in:
@@ -197,7 +197,18 @@ output.state; // => 'pending' | 'started' | 'canceled' | 'finalizing' | 'finaliz
|
||||
|
||||
## Output targets
|
||||
|
||||
The _output target_ determines where the data created by the `Output` will be written. This library offers two targets:
|
||||
The _output target_ determines where the data created by the `Output` will be written. This library offers a couple of targets.
|
||||
|
||||
---
|
||||
|
||||
All targets have an optional `onwrite` callback you can set to monitor which byte regions are being written to:
|
||||
```ts
|
||||
target.onwrite = (start, end) => {
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
You can use this to track the size of the output file as it grows. But be warned, this function is chatty and gets called *extremely* frequently.
|
||||
|
||||
### `BufferTarget`
|
||||
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ export abstract class Source {
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Called each time data is retrieved from the source. Will be called with the retrieved range. */
|
||||
/** Called each time data is retrieved from the source. Will be called with the retrieved range (end exclusive). */
|
||||
onread: ((start: number, end: number) => unknown) | null = null;
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -19,6 +19,14 @@ export abstract class Target {
|
||||
|
||||
/** @internal */
|
||||
abstract _createWriter(): Writer;
|
||||
|
||||
/**
|
||||
* Called each time data is written to the target. Will be called with the byte range into which data was written.
|
||||
*
|
||||
* Use this callback to track the size of the output file as it grows. But be warned, this function is chatty and
|
||||
* gets called *extremely* often.
|
||||
*/
|
||||
onwrite: ((start: number, end: number) => unknown) | null = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,6 +121,6 @@ export class StreamTarget extends Target {
|
||||
export class NullTarget extends Target {
|
||||
/** @internal */
|
||||
_createWriter() {
|
||||
return new NullTargetWriter();
|
||||
return new NullTargetWriter(this);
|
||||
}
|
||||
}
|
||||
|
||||
+10
-2
@@ -6,7 +6,7 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { BufferTarget, StreamTarget, StreamTargetChunk } from './target';
|
||||
import { BufferTarget, NullTarget, StreamTarget, StreamTargetChunk } from './target';
|
||||
import { assert } from './misc';
|
||||
|
||||
export abstract class Writer {
|
||||
@@ -156,8 +156,9 @@ export class BufferTargetWriter extends Writer {
|
||||
this.ensureSize(this.pos + data.byteLength);
|
||||
|
||||
this.bytes.set(data, this.pos);
|
||||
this.pos += data.byteLength;
|
||||
this.target.onwrite?.(this.pos, this.pos + data.byteLength);
|
||||
|
||||
this.pos += data.byteLength;
|
||||
this.maxPos = Math.max(this.maxPos, this.pos);
|
||||
}
|
||||
|
||||
@@ -250,6 +251,8 @@ export class StreamTargetWriter extends Writer {
|
||||
data: data.slice(),
|
||||
start: this.pos,
|
||||
});
|
||||
this.target.onwrite?.(this.pos, this.pos + data.byteLength);
|
||||
|
||||
this.pos += data.byteLength;
|
||||
|
||||
this.lastWriteEnd = Math.max(this.lastWriteEnd, this.pos);
|
||||
@@ -463,8 +466,13 @@ export class StreamTargetWriter extends Writer {
|
||||
export class NullTargetWriter extends Writer {
|
||||
private pos = 0;
|
||||
|
||||
constructor(private target: NullTarget) {
|
||||
super();
|
||||
}
|
||||
|
||||
write(data: Uint8Array) {
|
||||
this.maybeTrackWrites(data);
|
||||
this.target.onwrite?.(this.pos, this.pos + data.byteLength);
|
||||
this.pos += data.byteLength;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user