Skip to content

Commit

Permalink
replace lodash and update props name
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed Mar 5, 2024
1 parent d6e0e44 commit 23b4aa4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,5 @@ export {
WORKSPACE_TYPE,
cleanWorkspaceId,
} from '../utils';

export { debounce } from './utils';
55 changes: 55 additions & 0 deletions src/core/public/utils/debounce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { debounce } from './debounce';

describe('debounce', () => {
let fn: Function;
beforeEach(() => {
fn = jest.fn();
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
});

test('it should call the debounced fn once at the end of the quiet time', () => {
const debounced = debounce(fn, 1000);

for (let i = 0; i < 100; i++) {
debounced(i);
}

jest.advanceTimersByTime(1001);
expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledWith(99);
});

test("with a leading invocation, it should call the debounced fn once, if the time doens't pass", () => {
const debounced = debounce(fn, 1000, true);

for (let i = 0; i < 100; i++) {
debounced(i);
}

jest.advanceTimersByTime(999);

expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledWith(0);
});

test('with a leading invocation, it should call the debounced fn twice (at the beginning and at the end)', () => {
const debounced = debounce(fn, 1000, true);

for (let i = 0; i < 100; i++) {
debounced(i);
}

jest.advanceTimersByTime(1500);

expect(fn).toBeCalledTimes(2);
expect(fn).toBeCalledWith(0);
expect(fn).toBeCalledWith(99);
});
});
23 changes: 23 additions & 0 deletions src/core/public/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @param func The function to be debounced.
* @param delay The time in milliseconds to wait before invoking the function again after the last invocation.
* @param leading An optional parameter that, when true, allows the function to be invoked immediately upon the first call.
*/
export const debounce = (func: Function, delay: number, leading?: boolean) => {
let timerId: NodeJS.Timeout;

return (...args: any) => {
if (!timerId && leading) {
func(...args);
}
clearTimeout(timerId);

timerId = setTimeout(() => func(...args), delay);
};
};
1 change: 1 addition & 0 deletions src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

export { shareWeakReplay } from './share_weak_replay';
export { Sha256 } from './crypto';
export { debounce } from './debounce';
export { MountWrapper, mountReactNode } from './mount';
export {
WORKSPACE_PATH_PREFIX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import { WorkspaceClient } from '../../workspace_client';
interface DeleteWorkspaceModalProps {
onClose: () => void;
selectedWorkspace?: WorkspaceAttribute | null;
ifNavigate: boolean;
shouldNavigate: boolean;
}

export function DeleteWorkspaceModal(props: DeleteWorkspaceModalProps) {
const [value, setValue] = useState('');
const { onClose, selectedWorkspace, ifNavigate } = props;
const { onClose, selectedWorkspace, shouldNavigate } = props;
const {
services: { application, notifications, http, workspaceClient },
} = useOpenSearchDashboards<{ workspaceClient: WorkspaceClient }>();
Expand All @@ -55,7 +55,7 @@ export function DeleteWorkspaceModal(props: DeleteWorkspaceModalProps) {
}),
});
onClose();
if (http && application && ifNavigate) {
if (http && application && shouldNavigate) {
const homeUrl = application.getUrlForApp('home', {
path: '/',
absolute: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import {
import useObservable from 'react-use/lib/useObservable';
import { of } from 'rxjs';
import { i18n } from '@osd/i18n';
import { debounce } from 'lodash';
import { debounce } from '../../../../../core/public';
import { WorkspaceAttribute } from '../../../../../core/public';

import { useOpenSearchDashboards } from '../../../../../plugins/opensearch_dashboards_react/public';
import { switchWorkspace, updateWorkspace } from '../utils/workspace';

Expand Down Expand Up @@ -212,7 +211,7 @@ export const WorkspaceList = () => {
<DeleteWorkspaceModal
selectedWorkspace={delectedWorkspace}
onClose={() => setDelectedWorkspace(null)}
ifNavigate={false}
shouldNavigate={false}
/>
)}
</EuiPage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const WorkspaceActionsMenu = ({ workspace }: Props) => {
<DeleteWorkspaceModal
selectedWorkspace={workspace}
onClose={() => setDeleteWorkspaceModalVisible(false)}
ifNavigate={false}
shouldNavigate={false}
/>
)}
<EuiPopover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const WorkspaceUpdater = () => {
<DeleteWorkspaceModal
selectedWorkspace={currentWorkspace}
onClose={() => setDeleteWorkspaceModalVisible(false)}
ifNavigate={true}
shouldNavigate={true}
/>
</EuiPanel>
)}
Expand Down

0 comments on commit 23b4aa4

Please sign in to comment.