Add Target.onwrite callback

This commit is contained in:
Vanilagy
2025-09-02 17:34:09 +02:00
parent 55dd8cdb4c
commit 9d47b3c5dc
4 changed files with 32 additions and 5 deletions
+12 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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;
}