From afb4813d1c4288011c93383d28c4445e9af28596 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 29 Oct 2024 13:21:25 +0100 Subject: [PATCH] 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. --- src/display/api.js | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 6c0b9d2a8fd5c..211c37862c867 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2646,32 +2646,27 @@ class WorkerTransport { }; }); - messageHandler.on("ReaderHeadersReady", data => { - const headersCapability = Promise.withResolvers(); - const fullReader = this._fullReader; - fullReader.headersReady.then(() => { - // If stream or range are disabled, it's our only way to report - // loading progress. - if (!fullReader.isStreamingSupported || !fullReader.isRangeSupported) { - if (this._lastProgress) { - loadingTask.onProgress?.(this._lastProgress); - } - fullReader.onProgress = evt => { - loadingTask.onProgress?.({ - loaded: evt.loaded, - total: evt.total, - }); - }; - } + messageHandler.on("ReaderHeadersReady", async data => { + await this._fullReader.headersReady; - headersCapability.resolve({ - isStreamingSupported: fullReader.isStreamingSupported, - isRangeSupported: fullReader.isRangeSupported, - contentLength: fullReader.contentLength, - }); - }, headersCapability.reject); + const { isStreamingSupported, isRangeSupported, contentLength } = + this._fullReader; + + // If stream or range are disabled, it's our only way to report + // loading progress. + if (!isStreamingSupported || !isRangeSupported) { + if (this._lastProgress) { + loadingTask.onProgress?.(this._lastProgress); + } + this._fullReader.onProgress = evt => { + loadingTask.onProgress?.({ + loaded: evt.loaded, + total: evt.total, + }); + }; + } - return headersCapability.promise; + return { isStreamingSupported, isRangeSupported, contentLength }; }); messageHandler.on("GetRangeReader", (data, sink) => {