Skip to content

Commit

Permalink
Add custom hook to refetch data when websocket reconnects
Browse files Browse the repository at this point in the history
Add a custom React hook that can be used to perform an action
when the websocket is reconnected (but not on initial connection).
This is useful in our containers so we can avoid refetching when
the websocket disconnects (default behaviour if we use `webSocketConnected`
as a dependency for `useEffect`).

This avoid unnecessary and unwanted requests while keeping the container
logic relatively simple. In future with `react-query` or similar libraries
we can easily change the central `useWebSocketReconnected` hook as needed
without having to update each container.

Continue refactoring containers to be functional components, using
the new hook as appropriate.
  • Loading branch information
AlanGreene authored and tekton-robot committed May 4, 2021
1 parent 0128c26 commit ea51272
Show file tree
Hide file tree
Showing 26 changed files with 1,199 additions and 1,094 deletions.
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@storybook/theming": "^6.2.1",
"@svgr/webpack": "^5.5.0",
"@testing-library/react": "^11.2.2",
"@testing-library/react-hooks": "5.1.2",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.2",
"babel-plugin-react-intl": "^8.2.18",
Expand Down
34 changes: 34 additions & 0 deletions packages/utils/src/utils/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2021 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { useEffect, useRef } from 'react';

export function usePrevious(value) {
const ref = useRef();

useEffect(() => {
ref.current = value;
}, [value]);

return ref.current;
}

export function useWebSocketReconnected(callback, webSocketConnected) {
const prevWebSocketConnected = usePrevious(webSocketConnected);

useEffect(() => {
if (prevWebSocketConnected === false && webSocketConnected) {
callback();
}
}, [prevWebSocketConnected, webSocketConnected]);
}
52 changes: 52 additions & 0 deletions packages/utils/src/utils/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2021 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { renderHook } from '@testing-library/react-hooks';

import { usePrevious, useWebSocketReconnected } from './hooks';

describe('hooks', () => {
it('usePrevious', () => {
let initialValue = 0;
const { result, rerender } = renderHook(() => usePrevious(initialValue));

initialValue = 10;
rerender();
expect(result.current).toEqual(0);

initialValue = 1;
rerender();
expect(result.current).toEqual(10);
});

it('useWebSocketReconnected', () => {
const callback = jest.fn();
let webSocketConnected;

const { rerender } = renderHook(() =>
useWebSocketReconnected(callback, webSocketConnected)
);
expect(callback).not.toHaveBeenCalled();
webSocketConnected = true;
rerender();
expect(callback).not.toHaveBeenCalled();
webSocketConnected = false;
rerender();
expect(callback).not.toHaveBeenCalled();
// callback is only called if websocket was previously disconnected
// i.e. doesn't get called on initial connection
webSocketConnected = true;
rerender();
expect(callback).toHaveBeenCalled();
});
});
3 changes: 2 additions & 1 deletion packages/utils/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2020 The Tekton Authors
Copyright 2019-2021 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -16,6 +16,7 @@ import { getStatus } from './status';

export { default as buildGraphData } from './buildGraphData';
export * from './constants';
export * from './hooks';
export { paths, urls } from './router';
export { getStatus } from './status';

Expand Down
17 changes: 14 additions & 3 deletions src/containers/ClusterInterceptors/ClusterInterceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import React, { useEffect } from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { injectIntl } from 'react-intl';
import { getFilters, getTitle, urls } from '@tektoncd/dashboard-utils';
import {
getFilters,
getTitle,
urls,
useWebSocketReconnected
} from '@tektoncd/dashboard-utils';
import { FormattedDate, Table } from '@tektoncd/dashboard-components';

import { ListPageLayout } from '..';
Expand All @@ -42,9 +47,15 @@ function ClusterInterceptors(props) {
document.title = getTitle({ page: 'ClusterInterceptors' });
}, []);

useEffect(() => {
function fetchData() {
fetchClusterInterceptors({ filters });
}, [JSON.stringify(filters), webSocketConnected]);
}

useEffect(() => {
fetchData();
}, [JSON.stringify(filters)]);

useWebSocketReconnected(fetchData, webSocketConnected);

function getError() {
if (error) {
Expand Down
17 changes: 14 additions & 3 deletions src/containers/ClusterTasks/ClusterTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import {
TrashCan16 as DeleteIcon,
Playlist16 as RunsIcon
} from '@carbon/icons-react';
import { getFilters, getTitle, urls } from '@tektoncd/dashboard-utils';
import {
getFilters,
getTitle,
urls,
useWebSocketReconnected
} from '@tektoncd/dashboard-utils';

import { ListPageLayout } from '..';
import { fetchClusterTasks as fetchClusterTasksActionCreator } from '../../actions/tasks';
Expand Down Expand Up @@ -62,9 +67,15 @@ function ClusterTasksContainer(props) {
document.title = getTitle({ page: 'ClusterTasks' });
}, []);

useEffect(() => {
function fetchData() {
fetchClusterTasks({ filters });
}, [JSON.stringify(filters), webSocketConnected]);
}

useEffect(() => {
fetchData();
}, [JSON.stringify(filters)]);

useWebSocketReconnected(fetchData, webSocketConnected);

function getError() {
if (error) {
Expand Down
Loading

0 comments on commit ea51272

Please sign in to comment.