Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ce6db03

Browse files
committedJun 10, 2024
Define effects in useEffect call
Workaround for biomejs/biome#3080
1 parent 638fa3e commit ce6db03

File tree

8 files changed

+410
-408
lines changed

8 files changed

+410
-408
lines changed
 

‎packages/react-pdf/src/Document.tsx

+52-50
Original file line numberDiff line numberDiff line change
@@ -485,51 +485,53 @@ const Document = forwardRef(function Document(
485485
}
486486
}
487487

488-
function resetDocument() {
489-
pdfDispatch({ type: 'RESET' });
490-
}
491-
492-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
493-
useEffect(resetDocument, [pdfDispatch, source]);
494-
495-
function loadDocument() {
496-
if (!source) {
497-
return;
498-
}
499-
500-
const documentInitParams: Source = {
501-
...source,
502-
...options,
503-
};
504-
505-
const destroyable = pdfjs.getDocument(documentInitParams);
506-
if (onLoadProgress) {
507-
destroyable.onProgress = onLoadProgress;
508-
}
509-
if (onPassword) {
510-
destroyable.onPassword = onPassword;
511-
}
512-
const loadingTask = destroyable;
513-
514-
loadingTask.promise
515-
.then((nextPdf) => {
516-
pdfDispatch({ type: 'RESOLVE', value: nextPdf });
517-
})
518-
.catch((error) => {
519-
if (loadingTask.destroyed) {
520-
return;
521-
}
488+
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on source change
489+
useEffect(
490+
function resetDocument() {
491+
pdfDispatch({ type: 'RESET' });
492+
},
493+
[pdfDispatch, source],
494+
);
522495

523-
pdfDispatch({ type: 'REJECT', error });
524-
});
496+
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
497+
useEffect(
498+
function loadDocument() {
499+
if (!source) {
500+
return;
501+
}
525502

526-
return () => {
527-
loadingTask.destroy();
528-
};
529-
}
503+
const documentInitParams: Source = {
504+
...source,
505+
...options,
506+
};
530507

531-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
532-
useEffect(loadDocument, [options, pdfDispatch, source]);
508+
const destroyable = pdfjs.getDocument(documentInitParams);
509+
if (onLoadProgress) {
510+
destroyable.onProgress = onLoadProgress;
511+
}
512+
if (onPassword) {
513+
destroyable.onPassword = onPassword;
514+
}
515+
const loadingTask = destroyable;
516+
517+
loadingTask.promise
518+
.then((nextPdf) => {
519+
pdfDispatch({ type: 'RESOLVE', value: nextPdf });
520+
})
521+
.catch((error) => {
522+
if (loadingTask.destroyed) {
523+
return;
524+
}
525+
526+
pdfDispatch({ type: 'REJECT', error });
527+
});
528+
529+
return () => {
530+
loadingTask.destroy();
531+
};
532+
},
533+
[options, pdfDispatch, source],
534+
);
533535

534536
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
535537
useEffect(() => {
@@ -545,14 +547,14 @@ const Document = forwardRef(function Document(
545547
onLoadSuccess();
546548
}, [pdf]);
547549

548-
function setupLinkService() {
549-
linkService.current.setViewer(viewer.current);
550-
linkService.current.setExternalLinkRel(externalLinkRel);
551-
linkService.current.setExternalLinkTarget(externalLinkTarget);
552-
}
553-
554-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
555-
useEffect(setupLinkService, [externalLinkRel, externalLinkTarget]);
550+
useEffect(
551+
function setupLinkService() {
552+
linkService.current.setViewer(viewer.current);
553+
linkService.current.setExternalLinkRel(externalLinkRel);
554+
linkService.current.setExternalLinkTarget(externalLinkTarget);
555+
},
556+
[externalLinkRel, externalLinkTarget],
557+
);
556558

557559
const registerPage = useCallback((pageIndex: number, ref: HTMLDivElement) => {
558560
pages.current[pageIndex] = ref;

‎packages/react-pdf/src/Outline.tsx

+29-28
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,36 @@ export default function Outline(props: OutlineProps) {
115115
}
116116
}
117117

118-
function resetOutline() {
119-
outlineDispatch({ type: 'RESET' });
120-
}
121-
122-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
123-
useEffect(resetOutline, [outlineDispatch, pdf]);
124-
125-
function loadOutline() {
126-
if (!pdf) {
127-
// Impossible, but TypeScript doesn't know that
128-
return;
129-
}
130-
131-
const cancellable = makeCancellable(pdf.getOutline());
132-
const runningTask = cancellable;
133-
134-
cancellable.promise
135-
.then((nextOutline) => {
136-
outlineDispatch({ type: 'RESOLVE', value: nextOutline });
137-
})
138-
.catch((error) => {
139-
outlineDispatch({ type: 'REJECT', error });
140-
});
141-
142-
return () => cancelRunningTask(runningTask);
143-
}
118+
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on pdf change
119+
useEffect(
120+
function resetOutline() {
121+
outlineDispatch({ type: 'RESET' });
122+
},
123+
[outlineDispatch, pdf],
124+
);
144125

145-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
146-
useEffect(loadOutline, [outlineDispatch, pdf]);
126+
useEffect(
127+
function loadOutline() {
128+
if (!pdf) {
129+
// Impossible, but TypeScript doesn't know that
130+
return;
131+
}
132+
133+
const cancellable = makeCancellable(pdf.getOutline());
134+
const runningTask = cancellable;
135+
136+
cancellable.promise
137+
.then((nextOutline) => {
138+
outlineDispatch({ type: 'RESOLVE', value: nextOutline });
139+
})
140+
.catch((error) => {
141+
outlineDispatch({ type: 'REJECT', error });
142+
});
143+
144+
return () => cancelRunningTask(runningTask);
145+
},
146+
[outlineDispatch, pdf],
147+
);
147148

148149
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
149150
useEffect(() => {

‎packages/react-pdf/src/Page.tsx

+41-39
Original file line numberDiff line numberDiff line change
@@ -387,21 +387,22 @@ export default function Page(props: PageProps) {
387387
return scaleWithDefault * pageScale;
388388
}, [height, page, rotate, scaleProps, width]);
389389

390-
function hook() {
391-
return () => {
392-
if (!isProvided(pageIndex)) {
393-
// Impossible, but TypeScript doesn't know that
394-
return;
395-
}
396-
397-
if (_enableRegisterUnregisterPage && unregisterPage) {
398-
unregisterPage(pageIndex);
399-
}
400-
};
401-
}
402-
403-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
404-
useEffect(hook, [_enableRegisterUnregisterPage, pdf, pageIndex, unregisterPage]);
390+
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on pdf change
391+
useEffect(
392+
function hook() {
393+
return () => {
394+
if (!isProvided(pageIndex)) {
395+
// Impossible, but TypeScript doesn't know that
396+
return;
397+
}
398+
399+
if (_enableRegisterUnregisterPage && unregisterPage) {
400+
unregisterPage(pageIndex);
401+
}
402+
};
403+
},
404+
[_enableRegisterUnregisterPage, pdf, pageIndex, unregisterPage],
405+
);
405406

406407
/**
407408
* Called when a page is loaded successfully
@@ -442,34 +443,35 @@ export default function Page(props: PageProps) {
442443
}
443444
}
444445

445-
function resetPage() {
446-
pageDispatch({ type: 'RESET' });
447-
}
448-
449-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
450-
useEffect(resetPage, [pageDispatch, pdf, pageIndex]);
451-
452-
function loadPage() {
453-
if (!pdf || !pageNumber) {
454-
return;
455-
}
446+
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on pdf and pageIndex change
447+
useEffect(
448+
function resetPage() {
449+
pageDispatch({ type: 'RESET' });
450+
},
451+
[pageDispatch, pdf, pageIndex],
452+
);
456453

457-
const cancellable = makeCancellable(pdf.getPage(pageNumber));
458-
const runningTask = cancellable;
454+
useEffect(
455+
function loadPage() {
456+
if (!pdf || !pageNumber) {
457+
return;
458+
}
459459

460-
cancellable.promise
461-
.then((nextPage) => {
462-
pageDispatch({ type: 'RESOLVE', value: nextPage });
463-
})
464-
.catch((error) => {
465-
pageDispatch({ type: 'REJECT', error });
466-
});
460+
const cancellable = makeCancellable(pdf.getPage(pageNumber));
461+
const runningTask = cancellable;
467462

468-
return () => cancelRunningTask(runningTask);
469-
}
463+
cancellable.promise
464+
.then((nextPage) => {
465+
pageDispatch({ type: 'RESOLVE', value: nextPage });
466+
})
467+
.catch((error) => {
468+
pageDispatch({ type: 'REJECT', error });
469+
});
470470

471-
// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
472-
useEffect(loadPage, [pageDispatch, pdf, pageNumber]);
471+
return () => cancelRunningTask(runningTask);
472+
},
473+
[pageDispatch, pdf, pageNumber],
474+
);
473475

474476
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
475477
useEffect(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.