mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-03 05:43:50 +02:00
Rely not only on FinalizationRegistry
This commit is contained in:
+21
-2
@@ -551,6 +551,17 @@ export class BlobSource extends Source {
|
|||||||
maxCacheSize: options.maxCacheSize ?? (8 * 2 ** 20 /* 8 MiB */),
|
maxCacheSize: options.maxCacheSize ?? (8 * 2 ** 20 /* 8 MiB */),
|
||||||
maxWorkerCount: 4,
|
maxWorkerCount: 4,
|
||||||
runWorker: this._runWorker.bind(this),
|
runWorker: this._runWorker.bind(this),
|
||||||
|
onIdleWorkerRemoved: (worker) => {
|
||||||
|
const reader = this._readers.get(worker);
|
||||||
|
|
||||||
|
if (reader) {
|
||||||
|
this._readers.delete(worker);
|
||||||
|
blobReaderRegistry?.unregister(worker);
|
||||||
|
|
||||||
|
// If we don't do this, memory leaks indefinitely
|
||||||
|
void reader.cancel().catch(() => {});
|
||||||
|
}
|
||||||
|
},
|
||||||
prefetchProfile: PREFETCH_PROFILES.fileSystem,
|
prefetchProfile: PREFETCH_PROFILES.fileSystem,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -588,13 +599,12 @@ export class BlobSource extends Source {
|
|||||||
if (
|
if (
|
||||||
'stream' in this._blob && !isWebKit()
|
'stream' in this._blob && !isWebKit()
|
||||||
&& this._options.useStreamReader !== false
|
&& this._options.useStreamReader !== false
|
||||||
&& blobReaderRegistry // Without it, we cannot guarantee cleanup of abandoned readers
|
|
||||||
) {
|
) {
|
||||||
// Get a reader of the blob starting at the required offset, and then keep it around
|
// Get a reader of the blob starting at the required offset, and then keep it around
|
||||||
const slice = this._blob.slice(worker.currentPos);
|
const slice = this._blob.slice(worker.currentPos);
|
||||||
reader = slice.stream().getReader();
|
reader = slice.stream().getReader();
|
||||||
|
|
||||||
blobReaderRegistry.register(worker, reader);
|
blobReaderRegistry?.register(worker, reader, worker);
|
||||||
} else {
|
} else {
|
||||||
// We'll need to use more primitive ways
|
// We'll need to use more primitive ways
|
||||||
reader = null;
|
reader = null;
|
||||||
@@ -1983,6 +1993,7 @@ class ReadOrchestrator {
|
|||||||
runWorker: (worker: ReadWorker) => Promise<void>;
|
runWorker: (worker: ReadWorker) => Promise<void>;
|
||||||
prefetchProfile: PrefetchProfile;
|
prefetchProfile: PrefetchProfile;
|
||||||
maxWorkerCount: number;
|
maxWorkerCount: number;
|
||||||
|
onIdleWorkerRemoved?: (worker: ReadWorker) => void;
|
||||||
}) {}
|
}) {}
|
||||||
|
|
||||||
read(
|
read(
|
||||||
@@ -2276,6 +2287,7 @@ class ReadOrchestrator {
|
|||||||
assert(oldestIndex !== null);
|
assert(oldestIndex !== null);
|
||||||
assert(oldestWorker.pendingSlices.length === 0);
|
assert(oldestWorker.pendingSlices.length === 0);
|
||||||
this.workers.splice(oldestIndex, 1);
|
this.workers.splice(oldestIndex, 1);
|
||||||
|
this.options.onIdleWorkerRemoved?.(oldestWorker);
|
||||||
} else {
|
} else {
|
||||||
return null; // All workers are still running, we can't create a new one
|
return null; // All workers are still running, we can't create a new one
|
||||||
}
|
}
|
||||||
@@ -2423,6 +2435,7 @@ class ReadOrchestrator {
|
|||||||
otherWorker.currentPos, otherWorker.targetPos, // These should typically be equal when the worker's idle
|
otherWorker.currentPos, otherWorker.targetPos, // These should typically be equal when the worker's idle
|
||||||
)) {
|
)) {
|
||||||
this.workers.splice(i, 1);
|
this.workers.splice(i, 1);
|
||||||
|
this.options.onIdleWorkerRemoved?.(otherWorker);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2499,6 +2512,7 @@ class ReadOrchestrator {
|
|||||||
|
|
||||||
worker.running = false;
|
worker.running = false;
|
||||||
this.workers.splice(index, 1);
|
this.workers.splice(index, 1);
|
||||||
|
this.options.onIdleWorkerRemoved?.(worker);
|
||||||
|
|
||||||
if (this.fileSize === null) {
|
if (this.fileSize === null) {
|
||||||
// We can now deduce the file size!
|
// We can now deduce the file size!
|
||||||
@@ -2610,6 +2624,11 @@ class ReadOrchestrator {
|
|||||||
|
|
||||||
worker.pendingSlices.length = 0;
|
worker.pendingSlices.length = 0;
|
||||||
worker.aborted = true;
|
worker.aborted = true;
|
||||||
|
|
||||||
|
if (!worker.running) {
|
||||||
|
// Running workers clean up after themselves when they notice the abort
|
||||||
|
this.options.onIdleWorkerRemoved?.(worker);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const queuedRead of this.queuedReads) {
|
for (const queuedRead of this.queuedReads) {
|
||||||
|
|||||||
Reference in New Issue
Block a user