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

Updating usePolling to cleanup after unmount #1598

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -5046,34 +5046,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,19 +5074,6 @@ 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>
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,16 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { EuiButton, EuiEmptyPrompt, EuiLink, EuiText } from '@elastic/eui';
import { EuiEmptyPrompt, EuiText } from '@elastic/eui';
import React from 'react';
import { LoadCacheType } from '../../../../../../../common/types/data_connections';
import { coreRefs } from '../../../../../../framework/core_refs';

interface AssociatedObjectsTabEmptyProps {
cacheType: LoadCacheType;
}

export const AssociatedObjectsTabEmpty: React.FC<AssociatedObjectsTabEmptyProps> = (props) => {
const { cacheType } = props;
const { application } = coreRefs;

const QueryWorkbenchButton = (
<EuiButton
iconSide="right"
onClick={() => application!.navigateToApp('opensearch-query-workbench')}
iconType="popout"
>
Query Workbench
</EuiButton>
);

let titleText;
let bodyText;
Expand All @@ -51,12 +39,8 @@ export const AssociatedObjectsTabEmpty: React.FC<AssociatedObjectsTabEmptyProps>
<h4>{titleText}</h4>
<p>{bodyText}</p>
</EuiText>
<EuiLink onClick={() => console.log()} external>
Learn more
</EuiLink>
</>
}
button={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