Simplify the "ReaderHeadersReady" message-handler in the API

We can convert the handler to an `async` function, which removes the need to create a temporary Promise here.
Given the age of this code it shouldn't hurt to simplify it a little bit.
This commit is contained in:
Jonas Jenwald 2024-10-29 13:21:25 +01:00
parent 3a85479c67
commit afb4813d1c

View File

@ -2646,17 +2646,19 @@ class WorkerTransport {
}; };
}); });
messageHandler.on("ReaderHeadersReady", data => { messageHandler.on("ReaderHeadersReady", async data => {
const headersCapability = Promise.withResolvers(); await this._fullReader.headersReady;
const fullReader = this._fullReader;
fullReader.headersReady.then(() => { const { isStreamingSupported, isRangeSupported, contentLength } =
this._fullReader;
// If stream or range are disabled, it's our only way to report // If stream or range are disabled, it's our only way to report
// loading progress. // loading progress.
if (!fullReader.isStreamingSupported || !fullReader.isRangeSupported) { if (!isStreamingSupported || !isRangeSupported) {
if (this._lastProgress) { if (this._lastProgress) {
loadingTask.onProgress?.(this._lastProgress); loadingTask.onProgress?.(this._lastProgress);
} }
fullReader.onProgress = evt => { this._fullReader.onProgress = evt => {
loadingTask.onProgress?.({ loadingTask.onProgress?.({
loaded: evt.loaded, loaded: evt.loaded,
total: evt.total, total: evt.total,
@ -2664,14 +2666,7 @@ class WorkerTransport {
}; };
} }
headersCapability.resolve({ return { isStreamingSupported, isRangeSupported, contentLength };
isStreamingSupported: fullReader.isStreamingSupported,
isRangeSupported: fullReader.isRangeSupported,
contentLength: fullReader.contentLength,
});
}, headersCapability.reject);
return headersCapability.promise;
}); });
messageHandler.on("GetRangeReader", (data, sink) => { messageHandler.on("GetRangeReader", (data, sink) => {