Skip to content

Commit d25f13d

Browse files
committed
Report loading progress "automatically" when using the PDFDataTransportStream class, and remove the PDFDataRangeTransport.prototype.onDataProgress method
This is consistent with the other `BasePDFStream` implementations, and simplifies the API surface of the `PDFDataRangeTransport` class (note the changes in the viewer). Given that the `onDataProgress` method was changed to a no-op this won't affect third-party users, assuming there even are any since this code was written specifically for the Firefox PDF Viewer.
1 parent e4cd317 commit d25f13d

7 files changed

Lines changed: 66 additions & 44 deletions

File tree

src/display/api.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,6 @@ class PDFDataRangeTransport {
639639

640640
#progressiveReadListeners = [];
641641

642-
#progressListeners = [];
643-
644642
#rangeListeners = [];
645643

646644
/**
@@ -659,6 +657,18 @@ class PDFDataRangeTransport {
659657
this.initialData = initialData;
660658
this.progressiveDone = progressiveDone;
661659
this.contentDispositionFilename = contentDispositionFilename;
660+
661+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
662+
Object.defineProperty(this, "onDataProgress", {
663+
value: () => {
664+
deprecated(
665+
"`PDFDataRangeTransport.prototype.onDataProgress` - method was " +
666+
"removed, since loading progress is now reported automatically " +
667+
"through the `PDFDataTransportStream` class (and related code)."
668+
);
669+
},
670+
});
671+
}
662672
}
663673

664674
/**
@@ -668,13 +678,6 @@ class PDFDataRangeTransport {
668678
this.#rangeListeners.push(listener);
669679
}
670680

671-
/**
672-
* @param {function} listener
673-
*/
674-
addProgressListener(listener) {
675-
this.#progressListeners.push(listener);
676-
}
677-
678681
/**
679682
* @param {function} listener
680683
*/
@@ -699,18 +702,6 @@ class PDFDataRangeTransport {
699702
}
700703
}
701704

702-
/**
703-
* @param {number} loaded
704-
* @param {number|undefined} total
705-
*/
706-
onDataProgress(loaded, total) {
707-
this.#capability.promise.then(() => {
708-
for (const listener of this.#progressListeners) {
709-
listener(loaded, total);
710-
}
711-
});
712-
}
713-
714705
/**
715706
* @param {Uint8Array|null} chunk
716707
*/

src/display/fetch_stream.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ class PDFFetchStreamReader extends BasePDFStreamReader {
135135
return { value, done };
136136
}
137137
this._loaded += value.byteLength;
138-
this.onProgress?.({
139-
loaded: this._loaded,
140-
total: this._contentLength,
141-
});
138+
this._callOnProgress();
142139

143140
return { value: getArrayBuffer(value), done: false };
144141
}

src/display/node_stream.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,8 @@ class PDFNodeStreamReader extends BasePDFStreamReader {
129129
if (done) {
130130
return { value, done };
131131
}
132-
this._loaded += value.length;
133-
this.onProgress?.({
134-
loaded: this._loaded,
135-
total: this._contentLength,
136-
});
132+
this._loaded += value.byteLength;
133+
this._callOnProgress();
137134

138135
return { value: getArrayBuffer(value), done: false };
139136
}

src/display/transport_stream.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ class PDFDataTransportStream extends BasePDFStream {
5353
this.#onReceiveData(begin, chunk);
5454
});
5555

56-
pdfDataRangeTransport.addProgressListener((loaded, total) => {
57-
if (total !== undefined) {
58-
this._fullReader?.onProgress?.({ loaded, total });
59-
}
60-
});
61-
6256
pdfDataRangeTransport.addProgressiveReadListener(chunk => {
6357
this.#onReceiveData(/* begin = */ undefined, chunk);
6458
});
@@ -144,6 +138,16 @@ class PDFDataTransportStreamReader extends BasePDFStreamReader {
144138
this._filename = contentDispositionFilename;
145139
}
146140
this._headersCapability.resolve();
141+
142+
// Report loading progress when there is `initialData`, and `_enqueue` has
143+
// not been invoked, but with a small delay to give an `onProgress` callback
144+
// a chance to be registered first.
145+
const loaded = this._loaded;
146+
Promise.resolve().then(() => {
147+
if (loaded > 0 && this._loaded === loaded) {
148+
this._callOnProgress();
149+
}
150+
});
147151
}
148152

149153
_enqueue(chunk) {
@@ -157,6 +161,7 @@ class PDFDataTransportStreamReader extends BasePDFStreamReader {
157161
this._queuedChunks.push(chunk);
158162
}
159163
this._loaded += chunk.byteLength;
164+
this._callOnProgress();
160165
}
161166

162167
async read() {

src/shared/base_pdf_stream.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ class BasePDFStreamReader {
128128
this._stream = stream;
129129
}
130130

131+
_callOnProgress() {
132+
this.onProgress?.({ loaded: this._loaded, total: this._contentLength });
133+
}
134+
131135
/**
132136
* Gets a promise that is resolved when the headers and other metadata of
133137
* the PDF data stream are available.

test/unit/api_spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5180,6 +5180,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
51805180
it("should fetch document info and page using ranges", async function () {
51815181
const initialDataLength = 4000;
51825182
const subArrays = [];
5183+
let initialProgress = null;
51835184
let fetches = 0;
51845185

51855186
const data = await dataPromise;
@@ -5193,19 +5194,29 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
51935194
const chunk = new Uint8Array(data.subarray(begin, end));
51945195
subArrays.push(chunk);
51955196

5196-
transport.onDataProgress(initialDataLength);
51975197
transport.onDataRange(begin, chunk);
51985198
});
51995199
};
52005200

52015201
const loadingTask = getDocument({ range: transport });
5202+
loadingTask.onProgress = evt => {
5203+
initialProgress = evt;
5204+
loadingTask.onProgress = null;
5205+
};
5206+
52025207
const pdfDocument = await loadingTask.promise;
52035208
expect(pdfDocument.numPages).toEqual(14);
52045209

52055210
const pdfPage = await pdfDocument.getPage(10);
52065211
expect(pdfPage.rotate).toEqual(0);
52075212
expect(fetches).toBeGreaterThan(2);
52085213

5214+
expect(initialProgress).toEqual({
5215+
loaded: initialDataLength,
5216+
total: data.length,
5217+
percent: 0,
5218+
});
5219+
52095220
// Check that the TypedArrays were transferred.
52105221
for (const array of subArrays) {
52115222
expect(array.length).toEqual(0);
@@ -5217,6 +5228,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
52175228
it("should fetch document info and page using range and streaming", async function () {
52185229
const initialDataLength = 4000;
52195230
const subArrays = [];
5231+
let initialProgress = null;
52205232
let fetches = 0;
52215233

52225234
const data = await dataPromise;
@@ -5242,13 +5254,24 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
52425254
};
52435255

52445256
const loadingTask = getDocument({ range: transport });
5257+
loadingTask.onProgress = evt => {
5258+
initialProgress = evt;
5259+
loadingTask.onProgress = null;
5260+
};
5261+
52455262
const pdfDocument = await loadingTask.promise;
52465263
expect(pdfDocument.numPages).toEqual(14);
52475264

52485265
const pdfPage = await pdfDocument.getPage(10);
52495266
expect(pdfPage.rotate).toEqual(0);
52505267
expect(fetches).toEqual(1);
52515268

5269+
expect(initialProgress).toEqual({
5270+
loaded: initialDataLength,
5271+
total: data.length,
5272+
percent: 0,
5273+
});
5274+
52525275
await new Promise(resolve => {
52535276
waitSome(resolve);
52545277
});
@@ -5266,6 +5289,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
52665289
"using complete initialData",
52675290
async function () {
52685291
const subArrays = [];
5292+
let initialProgress = null;
52695293
let fetches = 0;
52705294

52715295
const data = await dataPromise;
@@ -5285,13 +5309,24 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
52855309
disableRange: true,
52865310
range: transport,
52875311
});
5312+
loadingTask.onProgress = evt => {
5313+
initialProgress = evt;
5314+
loadingTask.onProgress = null;
5315+
};
5316+
52885317
const pdfDocument = await loadingTask.promise;
52895318
expect(pdfDocument.numPages).toEqual(14);
52905319

52915320
const pdfPage = await pdfDocument.getPage(10);
52925321
expect(pdfPage.rotate).toEqual(0);
52935322
expect(fetches).toEqual(0);
52945323

5324+
expect(initialProgress).toEqual({
5325+
loaded: data.length,
5326+
total: data.length,
5327+
percent: 100,
5328+
});
5329+
52955330
// Check that the TypedArrays were transferred.
52965331
for (const array of subArrays) {
52975332
expect(array.length).toEqual(0);

web/firefoxcom.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -613,15 +613,8 @@ class ExternalServices extends BaseExternalServices {
613613
case "range":
614614
pdfDataRangeTransport.onDataRange(args.begin, args.chunk);
615615
break;
616-
case "rangeProgress":
617-
pdfDataRangeTransport.onDataProgress(args.loaded);
618-
break;
619616
case "progressiveRead":
620617
pdfDataRangeTransport.onDataProgressiveRead(args.chunk);
621-
622-
// Don't forget to report loading progress as well, since otherwise
623-
// the loadingBar won't update when `disableRange=true` is set.
624-
pdfDataRangeTransport.onDataProgress(args.loaded, args.total);
625618
break;
626619
case "progressiveDone":
627620
pdfDataRangeTransport?.onDataProgressiveDone();

0 commit comments

Comments
 (0)