Skip to content

Commit

Permalink
Define effects in useEffect call
Browse files Browse the repository at this point in the history
Workaround for biomejs/biome#3080
  • Loading branch information
wojtekmaj committed Jun 10, 2024
1 parent 638fa3e commit ce6db03
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 408 deletions.
102 changes: 52 additions & 50 deletions packages/react-pdf/src/Document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -485,51 +485,53 @@ const Document = forwardRef(function Document(
}
}

function resetDocument() {
pdfDispatch({ type: 'RESET' });
}

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(resetDocument, [pdfDispatch, source]);

function loadDocument() {
if (!source) {
return;
}

const documentInitParams: Source = {
...source,
...options,
};

const destroyable = pdfjs.getDocument(documentInitParams);
if (onLoadProgress) {
destroyable.onProgress = onLoadProgress;
}
if (onPassword) {
destroyable.onPassword = onPassword;
}
const loadingTask = destroyable;

loadingTask.promise
.then((nextPdf) => {
pdfDispatch({ type: 'RESOLVE', value: nextPdf });
})
.catch((error) => {
if (loadingTask.destroyed) {
return;
}
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on source change
useEffect(
function resetDocument() {
pdfDispatch({ type: 'RESET' });
},
[pdfDispatch, source],
);

pdfDispatch({ type: 'REJECT', error });
});
// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
useEffect(
function loadDocument() {
if (!source) {
return;
}

return () => {
loadingTask.destroy();
};
}
const documentInitParams: Source = {
...source,
...options,
};

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(loadDocument, [options, pdfDispatch, source]);
const destroyable = pdfjs.getDocument(documentInitParams);
if (onLoadProgress) {
destroyable.onProgress = onLoadProgress;
}
if (onPassword) {
destroyable.onPassword = onPassword;
}
const loadingTask = destroyable;

loadingTask.promise
.then((nextPdf) => {
pdfDispatch({ type: 'RESOLVE', value: nextPdf });
})
.catch((error) => {
if (loadingTask.destroyed) {
return;
}

pdfDispatch({ type: 'REJECT', error });
});

return () => {
loadingTask.destroy();
};
},
[options, pdfDispatch, source],
);

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

function setupLinkService() {
linkService.current.setViewer(viewer.current);
linkService.current.setExternalLinkRel(externalLinkRel);
linkService.current.setExternalLinkTarget(externalLinkTarget);
}

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(setupLinkService, [externalLinkRel, externalLinkTarget]);
useEffect(
function setupLinkService() {
linkService.current.setViewer(viewer.current);
linkService.current.setExternalLinkRel(externalLinkRel);
linkService.current.setExternalLinkTarget(externalLinkTarget);
},
[externalLinkRel, externalLinkTarget],
);

const registerPage = useCallback((pageIndex: number, ref: HTMLDivElement) => {
pages.current[pageIndex] = ref;
Expand Down
57 changes: 29 additions & 28 deletions packages/react-pdf/src/Outline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,36 @@ export default function Outline(props: OutlineProps) {
}
}

function resetOutline() {
outlineDispatch({ type: 'RESET' });
}

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(resetOutline, [outlineDispatch, pdf]);

function loadOutline() {
if (!pdf) {
// Impossible, but TypeScript doesn't know that
return;
}

const cancellable = makeCancellable(pdf.getOutline());
const runningTask = cancellable;

cancellable.promise
.then((nextOutline) => {
outlineDispatch({ type: 'RESOLVE', value: nextOutline });
})
.catch((error) => {
outlineDispatch({ type: 'REJECT', error });
});

return () => cancelRunningTask(runningTask);
}
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on pdf change
useEffect(
function resetOutline() {
outlineDispatch({ type: 'RESET' });
},
[outlineDispatch, pdf],
);

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(loadOutline, [outlineDispatch, pdf]);
useEffect(
function loadOutline() {
if (!pdf) {
// Impossible, but TypeScript doesn't know that
return;
}

const cancellable = makeCancellable(pdf.getOutline());
const runningTask = cancellable;

cancellable.promise
.then((nextOutline) => {
outlineDispatch({ type: 'RESOLVE', value: nextOutline });
})
.catch((error) => {
outlineDispatch({ type: 'REJECT', error });
});

return () => cancelRunningTask(runningTask);
},
[outlineDispatch, pdf],
);

// biome-ignore lint/correctness/useExhaustiveDependencies: Ommitted callbacks so they are not called every time they change
useEffect(() => {
Expand Down
80 changes: 41 additions & 39 deletions packages/react-pdf/src/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,21 +387,22 @@ export default function Page(props: PageProps) {
return scaleWithDefault * pageScale;
}, [height, page, rotate, scaleProps, width]);

function hook() {
return () => {
if (!isProvided(pageIndex)) {
// Impossible, but TypeScript doesn't know that
return;
}

if (_enableRegisterUnregisterPage && unregisterPage) {
unregisterPage(pageIndex);
}
};
}

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(hook, [_enableRegisterUnregisterPage, pdf, pageIndex, unregisterPage]);
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on pdf change
useEffect(
function hook() {
return () => {
if (!isProvided(pageIndex)) {
// Impossible, but TypeScript doesn't know that
return;
}

if (_enableRegisterUnregisterPage && unregisterPage) {
unregisterPage(pageIndex);
}
};
},
[_enableRegisterUnregisterPage, pdf, pageIndex, unregisterPage],
);

/**
* Called when a page is loaded successfully
Expand Down Expand Up @@ -442,34 +443,35 @@ export default function Page(props: PageProps) {
}
}

function resetPage() {
pageDispatch({ type: 'RESET' });
}

// biome-ignore lint/correctness/useExhaustiveDependencies: See https://github.com/biomejs/biome/issues/3080
useEffect(resetPage, [pageDispatch, pdf, pageIndex]);

function loadPage() {
if (!pdf || !pageNumber) {
return;
}
// biome-ignore lint/correctness/useExhaustiveDependencies: useEffect intentionally triggered on pdf and pageIndex change
useEffect(
function resetPage() {
pageDispatch({ type: 'RESET' });
},
[pageDispatch, pdf, pageIndex],
);

const cancellable = makeCancellable(pdf.getPage(pageNumber));
const runningTask = cancellable;
useEffect(
function loadPage() {
if (!pdf || !pageNumber) {
return;
}

cancellable.promise
.then((nextPage) => {
pageDispatch({ type: 'RESOLVE', value: nextPage });
})
.catch((error) => {
pageDispatch({ type: 'REJECT', error });
});
const cancellable = makeCancellable(pdf.getPage(pageNumber));
const runningTask = cancellable;

return () => cancelRunningTask(runningTask);
}
cancellable.promise
.then((nextPage) => {
pageDispatch({ type: 'RESOLVE', value: nextPage });
})
.catch((error) => {
pageDispatch({ type: 'REJECT', error });
});

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

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

0 comments on commit ce6db03

Please sign in to comment.