Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.13] Updating usePolling to cleanup after unmount #1601

Merged
merged 1 commit into from
Mar 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5036,6 +5036,15 @@ exports[`AssociatedObjectsTab Component renders tab with no databases or objects
cacheType="databases"
>
<EuiEmptyPrompt
actions={
<EuiButton
iconSide="right"
iconType="popout"
onClick={[Function]}
>
Query Workbench
</EuiButton>
}
body={
<React.Fragment>
<EuiText>
Expand All @@ -5046,34 +5055,10 @@ exports[`AssociatedObjectsTab Component renders tab with no databases or objects
Add databases and tables to your data source or use Query Workbench
</p>
</EuiText>
<EuiLink
external={true}
onClick={[Function]}
>
Learn more
</EuiLink>
</React.Fragment>
}
button={
<EuiButton
iconSide="right"
iconType="popout"
onClick={[Function]}
>
Query Workbench
</EuiButton>
}
>
<div
button={
<EuiButton
iconSide="right"
iconType="popout"
onClick={[Function]}
>
Query Workbench
</EuiButton>
}
className="euiEmptyPrompt"
>
<EuiTextColor
Expand All @@ -5098,23 +5083,96 @@ exports[`AssociatedObjectsTab Component renders tab with no databases or objects
</p>
</div>
</EuiText>
<EuiLink
external={true}
onClick={[Function]}
>
<button
className="euiLink euiLink--primary"
disabled={false}
onClick={[Function]}
type="button"
>
Learn more
</button>
</EuiLink>
</div>
</EuiText>
</span>
</EuiTextColor>
<EuiSpacer
size="l"
>
<div
className="euiSpacer euiSpacer--l"
/>
</EuiSpacer>
<EuiButton
iconSide="right"
iconType="popout"
onClick={[Function]}
>
<EuiButtonDisplay
baseClassName="euiButton"
disabled={false}
element="button"
iconSide="right"
iconType="popout"
isDisabled={false}
onClick={[Function]}
type="button"
>
<button
className="euiButton euiButton--primary"
disabled={false}
onClick={[Function]}
style={
Object {
"minWidth": undefined,
}
}
type="button"
>
<EuiButtonContent
className="euiButton__content"
iconSide="right"
iconType="popout"
textProps={
Object {
"className": "euiButton__text",
}
}
>
<span
className="euiButtonContent euiButtonContent--iconRight euiButton__content"
>
<EuiIcon
className="euiButtonContent__icon"
color="inherit"
size="m"
type="popout"
>
<EuiIconBeaker
aria-hidden={true}
className="euiIcon euiIcon--medium euiIcon--inherit euiIcon-isLoading euiButtonContent__icon"
focusable="false"
role="img"
style={null}
>
<svg
aria-hidden={true}
className="euiIcon euiIcon--medium euiIcon--inherit euiIcon-isLoading euiButtonContent__icon"
focusable="false"
height={16}
role="img"
style={null}
viewBox="0 0 16 16"
width={16}
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M5.277 10.088c.02.014.04.03.057.047.582.55 1.134.812 1.666.812.586 0 1.84-.293 3.713-.88L9 6.212V2H7v4.212l-1.723 3.876Zm-.438.987L3.539 14h8.922l-1.32-2.969C9.096 11.677 7.733 12 7 12c-.74 0-1.463-.315-2.161-.925ZM6 2H5V1h6v1h-1v4l3.375 7.594A1 1 0 0 1 12.461 15H3.54a1 1 0 0 1-.914-1.406L6 6V2Z"
/>
</svg>
</EuiIconBeaker>
</EuiIcon>
<span
className="euiButton__text"
>
Query Workbench
</span>
</span>
</EuiButtonContent>
</button>
</EuiButtonDisplay>
</EuiButton>
</div>
</EuiEmptyPrompt>
</AssociatedObjectsTabEmpty>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

export interface AssociatedObjectsTabProps {
datasource: DatasourceDetails;
cacheLoadingHooks: any;

Check warning on line 50 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
selectedDatabase: string;
setSelectedDatabase: React.Dispatch<React.SetStateAction<string>>;
}
Expand Down Expand Up @@ -160,7 +160,8 @@
if (datasource.name) {
const datasourceCache = CatalogCacheManager.getOrCreateDataSource(datasource.name);
if (
datasourceCache.status === CachedDataSourceStatus.Empty &&
(datasourceCache.status === CachedDataSourceStatus.Empty ||
datasourceCache.status === CachedDataSourceStatus.Failed) &&
!isCatalogCacheFetching(databasesLoadStatus)
) {
startLoadingDatabases(datasource.name);
Expand All @@ -169,7 +170,7 @@
setIsFirstTimeLoading(false);
}
}
}, [datasource.name]);

Check warning on line 173 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has missing dependencies: 'databasesLoadStatus' and 'startLoadingDatabases'. Either include them or remove the dependency array

// Retrieve from cache upon load success
useEffect(() => {
Expand Down Expand Up @@ -209,7 +210,8 @@
datasource.name
);
if (
databaseCache.status === CachedDataSourceStatus.Empty &&
(databaseCache.status === CachedDataSourceStatus.Empty ||
databaseCache.status === CachedDataSourceStatus.Failed) &&
!isCatalogCacheFetching(tablesLoadStatus)
) {
startLoadingTables(datasource.name, selectedDatabase);
Expand All @@ -218,7 +220,9 @@
setCachedTables(databaseCache.tables);
}
if (
(accelerationsCache.status === CachedDataSourceStatus.Empty || isRefreshing) &&
(accelerationsCache.status === CachedDataSourceStatus.Empty ||
accelerationsCache.status === CachedDataSourceStatus.Failed ||
isRefreshing) &&
!isCatalogCacheFetching(accelerationsLoadStatus)
) {
startLoadingAccelerations(datasource.name);
Expand All @@ -227,7 +231,7 @@
setCachedAccelerations(accelerationsCache.accelerations);
}
}
}, [datasource.name, selectedDatabase, databaseSelectorOptions]);

Check warning on line 234 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has missing dependencies: 'accelerationsLoadStatus', 'isRefreshing', 'startLoadingAccelerations', 'startLoadingTables', and 'tablesLoadStatus'. Either include them or remove the dependency array

// Retrieve from tables cache upon load success
useEffect(() => {
Expand Down Expand Up @@ -277,7 +281,7 @@
};
})
);
}, [cachedDatabases]);

Check warning on line 284 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'selectedDatabase'. Either include it or remove the dependency array

useEffect(() => {
setSelectedDatabase((prevState) => {
Expand All @@ -287,7 +291,7 @@
}
return prevState;
});
}, [databaseSelectorOptions]);

Check warning on line 294 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'setSelectedDatabase'. Either include it or remove the dependency array

useEffect(() => {
const tableObjects: AssociatedObject[] = cachedTables.map((table: CachedTable) => {
Expand Down Expand Up @@ -316,7 +320,7 @@
columns: undefined,
}));
setAssociatedObjects([...tableObjects, ...accelerationObjects]);
}, [selectedDatabase, cachedTables, cachedAccelerations]);

Check warning on line 323 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View workflow job for this annotation

GitHub Actions / Lint

React Hook useEffect has a missing dependency: 'datasource.name'. Either include it or remove the dependency array

const renderCreateAccelerationFlyout = getRenderCreateAccelerationFlyout();

Expand Down Expand Up @@ -363,7 +367,11 @@
<AssociatedObjectsTabFailure type="objects" />
) : (
<>
{cachedTables.length > 0 || cachedAccelerations.length > 0 ? (
{cachedTables.length > 0 ||
cachedAccelerations.filter(

Check warning on line 371 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx#L371

Added line #L371 was not covered by tests
(acceleration: CachedAcceleration) =>
acceleration.database === selectedDatabase

Check warning on line 373 in public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/associated_objects/associated_objects_tab.tsx#L373

Added line #L373 was not covered by tests
).length > 0 ? (
<AssociatedObjectsTable
datasourceName={datasource.name}
associatedObjects={associatedObjects}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { EuiButton, EuiEmptyPrompt, EuiLink, EuiText } from '@elastic/eui';
import { EuiButton, EuiEmptyPrompt, EuiText } from '@elastic/eui';
import React from 'react';
import { LoadCacheType } from '../../../../../../../common/types/data_connections';
import { coreRefs } from '../../../../../../framework/core_refs';
Expand Down Expand Up @@ -51,12 +51,9 @@ export const AssociatedObjectsTabEmpty: React.FC<AssociatedObjectsTabEmptyProps>
<h4>{titleText}</h4>
<p>{bodyText}</p>
</EuiText>
<EuiLink onClick={() => console.log()} external>
Learn more
</EuiLink>
</>
}
button={QueryWorkbenchButton}
actions={QueryWorkbenchButton}
/>
);
};
12 changes: 11 additions & 1 deletion public/components/hooks/use_polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { useState, useRef } from 'react';
import { useState, useRef, useEffect } from 'react';

type FetchFunction<T, P = void> = (params?: P) => Promise<T>;

Expand Down Expand Up @@ -70,7 +70,7 @@
data: T | null;
loading: boolean;
error: Error | null;
startPolling: (params?: any) => void;

Check warning on line 73 in public/components/hooks/use_polling.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
stopPolling: () => void;
}

Expand All @@ -85,6 +85,7 @@
const [error, setError] = useState<Error | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const intervalRef = useRef<NodeJS.Timeout | string | number | undefined>(undefined);
const unmounted = useRef<boolean>(false);

const shouldPoll = useRef(false);

Expand All @@ -96,6 +97,9 @@
}
}, interval);
intervalRef.current = intervalId;
if (unmounted.current) {
clearInterval(intervalId);

Check warning on line 101 in public/components/hooks/use_polling.ts

View check run for this annotation

Codecov / codecov/patch

public/components/hooks/use_polling.ts#L101

Added line #L101 was not covered by tests
}
};

const stopPolling = () => {
Expand Down Expand Up @@ -124,5 +128,11 @@
}
};

useEffect(() => {
return () => {
unmounted.current = true;
};
}, []);

return { data, loading, error, startPolling, stopPolling };
}
Loading