From 37412722666e573f07eb7ca0d65725037c9b5bc8 Mon Sep 17 00:00:00 2001 From: Naumov Alexey Date: Fri, 11 Feb 2022 15:32:30 +0300 Subject: [PATCH 1/2] feat(plugin-query-manager-aministration): CB-1628 add auto refresh --- webapp/packages/core-blocks/src/index.ts | 1 + .../packages/core-blocks/src/useInterval.ts | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 webapp/packages/core-blocks/src/useInterval.ts diff --git a/webapp/packages/core-blocks/src/index.ts b/webapp/packages/core-blocks/src/index.ts index f151658adb..0877798cd8 100644 --- a/webapp/packages/core-blocks/src/index.ts +++ b/webapp/packages/core-blocks/src/index.ts @@ -139,6 +139,7 @@ export * from './useObservableRef'; export * from './useStateDelay'; export * from './useErrorDetails'; export * from './useAppLoadingScreen'; +export * from './useInterval'; export * from './BlocksLocaleService'; export * from './Snackbars/NotificationMark'; export * from './Snackbars/SnackbarMarkups/SnackbarWrapper'; diff --git a/webapp/packages/core-blocks/src/useInterval.ts b/webapp/packages/core-blocks/src/useInterval.ts new file mode 100644 index 0000000000..ccd298b59a --- /dev/null +++ b/webapp/packages/core-blocks/src/useInterval.ts @@ -0,0 +1,30 @@ +/* + * CloudBeaver - Cloud Database Manager + * Copyright (C) 2020-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +import { useEffect, useRef } from 'react'; + +export function useInterval(callback: () => void, delay: number | null) { + const savedCallback = useRef<() => void>(); + + useEffect(() => { + savedCallback.current = callback; + }, [callback]); + + useEffect(() => { + if (delay === null) { + return; + } + + const tick = () => { + savedCallback.current?.(); + }; + + const id = setInterval(tick, delay); + return () => clearInterval(id); + }, [delay]); +} \ No newline at end of file From 499a58f7d783600f16b9088e2b551db507399325 Mon Sep 17 00:00:00 2001 From: Naumov Alexey Date: Fri, 11 Feb 2022 16:26:31 +0300 Subject: [PATCH 2/2] feat(plugin-query-manager-aministration): CB-1628 useRef -> useObjectRef --- webapp/packages/core-blocks/src/useInterval.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/webapp/packages/core-blocks/src/useInterval.ts b/webapp/packages/core-blocks/src/useInterval.ts index ccd298b59a..9ea24be22f 100644 --- a/webapp/packages/core-blocks/src/useInterval.ts +++ b/webapp/packages/core-blocks/src/useInterval.ts @@ -6,14 +6,12 @@ * you may not use this file except in compliance with the License. */ -import { useEffect, useRef } from 'react'; +import { useEffect } from 'react'; + +import { useObjectRef } from './useObjectRef'; export function useInterval(callback: () => void, delay: number | null) { - const savedCallback = useRef<() => void>(); - - useEffect(() => { - savedCallback.current = callback; - }, [callback]); + const state = useObjectRef({ callback }); useEffect(() => { if (delay === null) { @@ -21,7 +19,7 @@ export function useInterval(callback: () => void, delay: number | null) { } const tick = () => { - savedCallback.current?.(); + state.callback(); }; const id = setInterval(tick, delay);