Skip to content

Commit 916b58a

Browse files
committed
Add a helper function to resolve pending requests in src/display/transport_stream.js and src/display/network.js
Currently the same identical code is duplicated four times per file, which seems completely unnecessary. Note that the function isn't placed in `src/display/network_utils.js`, since that file isn't included in MOZCENTRAL builds.
1 parent 2d643ef commit 916b58a

2 files changed

Lines changed: 29 additions & 39 deletions

File tree

src/display/network.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getResponseOrigin,
2828
validateRangeRequestCapabilities,
2929
} from "./network_utils.js";
30+
import { endRequests } from "./transport_stream.js";
3031

3132
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
3233
throw new Error(
@@ -174,6 +175,8 @@ class PDFNetworkStream extends BasePDFStream {
174175
}
175176

176177
class PDFNetworkStreamReader extends BasePDFStreamReader {
178+
#endRequests = endRequests.bind(this);
179+
177180
_cachedChunks = [];
178181

179182
_done = false;
@@ -254,13 +257,9 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
254257
this._cachedChunks.push(chunk);
255258
}
256259
this._done = true;
257-
if (this._cachedChunks.length > 0) {
258-
return;
259-
}
260-
for (const capability of this._requests) {
261-
capability.resolve({ value: undefined, done: true });
260+
if (this._cachedChunks.length === 0) {
261+
this.#endRequests();
262262
}
263-
this._requests.length = 0;
264263
}
265264

266265
#onError(status) {
@@ -301,17 +300,16 @@ class PDFNetworkStreamReader extends BasePDFStreamReader {
301300
cancel(reason) {
302301
this._done = true;
303302
this._headersCapability.reject(reason);
304-
for (const capability of this._requests) {
305-
capability.resolve({ value: undefined, done: true });
306-
}
307-
this._requests.length = 0;
303+
this.#endRequests();
308304

309305
this._stream._abortRequest(this._fullRequestXhr);
310306
this._fullRequestXhr = null;
311307
}
312308
}
313309

314310
class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
311+
#endRequests = endRequests.bind(this);
312+
315313
onClosed = null;
316314

317315
_done = false;
@@ -353,10 +351,7 @@ class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
353351
this._queuedChunk = chunk;
354352
}
355353
this._done = true;
356-
for (const capability of this._requests) {
357-
capability.resolve({ value: undefined, done: true });
358-
}
359-
this._requests.length = 0;
354+
this.#endRequests();
360355
this.onClosed?.();
361356
}
362357

@@ -388,10 +383,7 @@ class PDFNetworkStreamRangeReader extends BasePDFStreamRangeReader {
388383

389384
cancel(reason) {
390385
this._done = true;
391-
for (const capability of this._requests) {
392-
capability.resolve({ value: undefined, done: true });
393-
}
394-
this._requests.length = 0;
386+
this.#endRequests();
395387

396388
this._stream._abortRequest(this._requestXhr);
397389
this.onClosed?.();

src/display/transport_stream.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ function getArrayBuffer(val) {
2929
: new Uint8Array(val).buffer;
3030
}
3131

32+
function endRequests() {
33+
for (const capability of this._requests) {
34+
capability.resolve({ value: undefined, done: true });
35+
}
36+
this._requests.length = 0;
37+
}
38+
3239
class PDFDataTransportStream extends BasePDFStream {
3340
_progressiveDone = false;
3441

@@ -112,6 +119,8 @@ class PDFDataTransportStream extends BasePDFStream {
112119
}
113120

114121
class PDFDataTransportStreamReader extends BasePDFStreamReader {
122+
#endRequests = endRequests.bind(this);
123+
115124
_done = false;
116125

117126
_queuedChunks = null;
@@ -179,26 +188,21 @@ class PDFDataTransportStreamReader extends BasePDFStreamReader {
179188

180189
cancel(reason) {
181190
this._done = true;
182-
for (const capability of this._requests) {
183-
capability.resolve({ value: undefined, done: true });
184-
}
185-
this._requests.length = 0;
191+
this.#endRequests();
186192
}
187193

188194
progressiveDone() {
189195
this._done ||= true;
190196

191-
if (this._queuedChunks.length > 0) {
192-
return;
197+
if (this._queuedChunks.length === 0) {
198+
this.#endRequests();
193199
}
194-
for (const capability of this._requests) {
195-
capability.resolve({ value: undefined, done: true });
196-
}
197-
this._requests.length = 0;
198200
}
199201
}
200202

201203
class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader {
204+
#endRequests = endRequests.bind(this);
205+
202206
onDone = null;
203207

204208
_begin = -1;
@@ -221,13 +225,10 @@ class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader {
221225
if (this._requests.length === 0) {
222226
this._queuedChunk = chunk;
223227
} else {
224-
const firstCapability = this._requests.shift();
225-
firstCapability.resolve({ value: chunk, done: false });
228+
const capability = this._requests.shift();
229+
capability.resolve({ value: chunk, done: false });
226230

227-
for (const capability of this._requests) {
228-
capability.resolve({ value: undefined, done: true });
229-
}
230-
this._requests.length = 0;
231+
this.#endRequests();
231232
}
232233
this._done = true;
233234
this.onDone?.();
@@ -249,12 +250,9 @@ class PDFDataTransportStreamRangeReader extends BasePDFStreamRangeReader {
249250

250251
cancel(reason) {
251252
this._done = true;
252-
for (const capability of this._requests) {
253-
capability.resolve({ value: undefined, done: true });
254-
}
255-
this._requests.length = 0;
253+
this.#endRequests();
256254
this.onDone?.();
257255
}
258256
}
259257

260-
export { PDFDataTransportStream };
258+
export { endRequests, PDFDataTransportStream };

0 commit comments

Comments
 (0)