Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
node-version: [20.x]
node-version: [24.x]

steps:
- uses: actions/checkout@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/imodel-browser-react",
"comment": "Fix AbortController aborting in-flight fetch in React 18 Strict Mode",
"type": "patch"
}
],
"packageName": "@itwin/imodel-browser-react"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { renderHook } from "@testing-library/react-hooks";
import { rest } from "msw";
import { act } from "react";
import { act, StrictMode } from "react";

import { server } from "../../tests/mocks/server";
import { DataStatus } from "../../types";
Expand All @@ -15,7 +15,10 @@ describe("useIModelData hook", () => {
beforeAll(() => server.listen());
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => server.resetHandlers());
afterEach(() => {
server.resetHandlers();
jest.restoreAllMocks();
});
// Clean up after the tests are finished.
afterAll(() => {
server.close();
Expand Down Expand Up @@ -311,6 +314,24 @@ describe("useIModelData hook", () => {
expect(watcher).toHaveBeenCalledTimes(2);
});

it("completes fetch when mounted inside React.StrictMode", async () => {
const { result, waitForValueToChange } = renderHook(
() => useIModelData({ iTwinId: "iTwinId", accessToken: "accessToken" }),
{ wrapper: StrictMode }
);

await waitForValueToChange(
() => result.current.status === DataStatus.Complete,
{ timeout: 3000 }
);

expect(result.current.status).toEqual(DataStatus.Complete);
expect(result.current.iModels).toContainEqual({
id: "fakeId",
displayName: "fakeName",
});
});

it.each([
{
missing: "accessToken",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import React, { useEffect } from "react";
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";

import {
AccessTokenProvider,
Expand Down Expand Up @@ -48,20 +54,21 @@ export const useIModelData = ({
onLoadMore,
onRefetch,
}: IModelDataHookOptions) => {
const [needsUpdate, setNeedsUpdate] = React.useState(true);
const [iModels, setIModels] = React.useState<IModelFull[]>([]);
const [status, setStatus] = React.useState<DataStatus>();
const [page, setPage] = React.useState(0);
const [morePagesAvailable, setMorePagesAvailable] = React.useState(true);
const [abortController, setAbortController] = React.useState<
AbortController | undefined
>(undefined);
const [needsUpdate, setNeedsUpdate] = useState(true);
const [iModels, setIModels] = useState<IModelFull[]>([]);
const [status, setStatus] = useState<DataStatus>();
const [page, setPage] = useState(0);
const [morePagesAvailable, setMorePagesAvailable] = useState(true);
const abortControllerRef = useRef<AbortController | undefined>(
undefined
);
const activeRequestRef = useRef<symbol | undefined>(undefined);

const sortType =
sortOptions && ["name", "createdDateTime"].includes(sortOptions.sortType)
? sortOptions.sortType
: undefined; //Only available sort-by API at the moment.
const [previousSortOptions, setPreviousSortOptions] = React.useState<
const [previousSortOptions, setPreviousSortOptions] = useState<
IModelSortOptions | undefined
>(sortOptions && { ...sortOptions });
const sortDescending = sortOptions?.descending;
Expand All @@ -72,7 +79,7 @@ export const useIModelData = ({
);

// For recents and favorites, apply client-side filtering based on searchText
const filteredIModels = React.useMemo(() => {
const filteredIModels = useMemo(() => {
if (
!searchText?.trim() ||
(requestType !== "recents" && requestType !== "favorites")
Expand All @@ -95,10 +102,7 @@ export const useIModelData = ({
setPreviousSortOptions(sortOptions);
}

// cleanup the abort controller when unmounting
useEffect(() => () => abortController?.abort(), [abortController]);

const reset = React.useCallback(() => {
const reset = useCallback(() => {
if (dataMode === "external") {
return;
}
Expand All @@ -110,7 +114,7 @@ export const useIModelData = ({
setNeedsUpdate(true);
}, [dataMode]);

const fetchMore = React.useCallback(() => {
const fetchMore = useCallback(() => {
if (dataMode === "external") {
return;
}
Expand All @@ -128,7 +132,7 @@ export const useIModelData = ({
setNeedsUpdate(true);
}, [dataMode, needsUpdate, status, morePagesAvailable]);

React.useEffect(() => {
useEffect(() => {
if (dataMode === "external") {
return;
}
Expand All @@ -152,7 +156,7 @@ export const useIModelData = ({
reset,
]);

React.useEffect(() => {
useEffect(() => {
if (dataMode === "external") {
return;
}
Expand All @@ -176,14 +180,14 @@ export const useIModelData = ({
]);

// Main function
React.useEffect(() => {
useEffect(() => {
if (dataMode === "external" || !needsUpdate) {
return;
}

setNeedsUpdate(false);
abortController?.abort();
setAbortController(undefined);
abortControllerRef.current?.abort();
abortControllerRef.current = undefined;

if (!accessToken || !iTwinId) {
setStatus(
Expand All @@ -202,6 +206,9 @@ export const useIModelData = ({
// Otherwise, fetch from server
setStatus(DataStatus.Fetching);

const requestId = Symbol();
activeRequestRef.current = requestId;

const { abortController: newAbortController, fetchIModels } =
createFetchIModelsFn(
iTwinId,
Expand All @@ -215,37 +222,33 @@ export const useIModelData = ({
apiOverrides?.serverEnvironmentPrefix,
requestType
);
setAbortController(newAbortController);
abortControllerRef.current = newAbortController;

fetchIModels()
.then(({ iModels, morePagesAvailable }) => {
if (activeRequestRef.current !== requestId) return;
setMorePagesAvailable(morePagesAvailable);
setIModels((prev) =>
page === 0 ? [...iModels] : [...prev, ...iModels]
);
setStatus(DataStatus.Complete);
})
.catch((e) => {
if (e.name === "AbortError") {
// Aborting because unmounting is not an error, swallow.
if (activeRequestRef.current !== requestId || e.name === "AbortError")
return;
}
setIModels([]);
setMorePagesAvailable(false);
setStatus(DataStatus.FetchFailed);
console.error(e);
});
}, [
dataMode,
abortController,
accessToken,
apiOverrides?.data,
apiOverrides?.serverEnvironmentPrefix,
iModels,
iTwinId,
maxCount,
pageSize,
morePagesAvailable,
needsUpdate,
page,
requestType,
Expand All @@ -255,6 +258,15 @@ export const useIModelData = ({
sortType,
]);

// Abort any in-flight request and invalidate stale results on unmount
useEffect(
() => () => {
activeRequestRef.current = undefined;
abortControllerRef.current?.abort();
},
[]
);

if (dataMode === "external") {
return {
iModels: apiOverrides?.data ?? [],
Expand Down
4 changes: 2 additions & 2 deletions rush.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
"rushVersion": "5.112.2",
"pnpmVersion": "7.32.2",
"nodeSupportedVersionRange": "^18.12.0 || ^20.9.0 || ^22.11.0",
"pnpmVersion": "7.33.7",
"nodeSupportedVersionRange": "^18.12.0 || ^20.9.0 || ^22.11.0 || ^24.11.0",
"projectFolderMinDepth": 2,
"projectFolderMaxDepth": 3,
"repository": {
Expand Down
Loading