Skip to content

Commit fa69d9a

Browse files
committed
Inline the helper method in PDFLinkService.goToDestination
We no longer need the helper method to *potentially* call itself once data is available, and can instead take full advantage of async/await by inlining the code.
1 parent 3052e99 commit fa69d9a

File tree

1 file changed

+35
-47
lines changed

1 file changed

+35
-47
lines changed

web/pdf_link_service.js

+35-47
Original file line numberDiff line numberDiff line change
@@ -131,44 +131,59 @@ class PDFLinkService {
131131
return this.pdfDocument ? this.pdfViewer.isInPresentationMode : false;
132132
}
133133

134-
#goToDestinationHelper(rawDest, namedDest = null, explicitDest) {
134+
/**
135+
* This method will, when available, also update the browser history.
136+
*
137+
* @param {string|Array} dest - The named, or explicit, PDF destination.
138+
*/
139+
async goToDestination(dest) {
140+
if (!this.pdfDocument) {
141+
return;
142+
}
143+
let namedDest, explicitDest, pageNumber;
144+
if (typeof dest === "string") {
145+
namedDest = dest;
146+
explicitDest = await this.pdfDocument.getDestination(dest);
147+
} else {
148+
namedDest = null;
149+
explicitDest = await dest;
150+
}
151+
if (!Array.isArray(explicitDest)) {
152+
console.error(
153+
`goToDestination: "${explicitDest}" is not a valid destination array, for dest="${dest}".`
154+
);
155+
return;
156+
}
135157
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
136-
const destRef = explicitDest[0];
137-
let pageNumber;
158+
const [destRef] = explicitDest;
138159

139160
if (typeof destRef === "object" && destRef !== null) {
140161
pageNumber = this._cachedPageNumber(destRef);
141162

142163
if (!pageNumber) {
143164
// Fetch the page reference if it's not yet available. This could
144165
// only occur during loading, before all pages have been resolved.
145-
this.pdfDocument
146-
.getPageIndex(destRef)
147-
.then(pageIndex => {
148-
this.cachePageRef(pageIndex + 1, destRef);
149-
this.#goToDestinationHelper(rawDest, namedDest, explicitDest);
150-
})
151-
.catch(() => {
152-
console.error(
153-
`PDFLinkService.#goToDestinationHelper: "${destRef}" is not ` +
154-
`a valid page reference, for dest="${rawDest}".`
155-
);
156-
});
157-
return;
166+
try {
167+
pageNumber = (await this.pdfDocument.getPageIndex(destRef)) + 1;
168+
this.cachePageRef(pageNumber, destRef);
169+
} catch {
170+
console.error(
171+
`goToDestination: "${destRef}" is not a valid page reference, for dest="${dest}".`
172+
);
173+
return;
174+
}
158175
}
159176
} else if (Number.isInteger(destRef)) {
160177
pageNumber = destRef + 1;
161178
} else {
162179
console.error(
163-
`PDFLinkService.#goToDestinationHelper: "${destRef}" is not ` +
164-
`a valid destination reference, for dest="${rawDest}".`
180+
`goToDestination: "${destRef}" is not a valid destination reference, for dest="${dest}".`
165181
);
166182
return;
167183
}
168184
if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
169185
console.error(
170-
`PDFLinkService.#goToDestinationHelper: "${pageNumber}" is not ` +
171-
`a valid page number, for dest="${rawDest}".`
186+
`goToDestination: "${pageNumber}" is not a valid page number, for dest="${dest}".`
172187
);
173188
return;
174189
}
@@ -187,33 +202,6 @@ class PDFLinkService {
187202
});
188203
}
189204

190-
/**
191-
* This method will, when available, also update the browser history.
192-
*
193-
* @param {string|Array} dest - The named, or explicit, PDF destination.
194-
*/
195-
async goToDestination(dest) {
196-
if (!this.pdfDocument) {
197-
return;
198-
}
199-
let namedDest, explicitDest;
200-
if (typeof dest === "string") {
201-
namedDest = dest;
202-
explicitDest = await this.pdfDocument.getDestination(dest);
203-
} else {
204-
namedDest = null;
205-
explicitDest = await dest;
206-
}
207-
if (!Array.isArray(explicitDest)) {
208-
console.error(
209-
`PDFLinkService.goToDestination: "${explicitDest}" is not ` +
210-
`a valid destination array, for dest="${dest}".`
211-
);
212-
return;
213-
}
214-
this.#goToDestinationHelper(dest, namedDest, explicitDest);
215-
}
216-
217205
/**
218206
* This method will, when available, also update the browser history.
219207
*

0 commit comments

Comments
 (0)