Skip to content

Commit

Permalink
fix: reduce initialization requests in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Nov 9, 2022
1 parent 1bedc9e commit 1892674
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
7 changes: 6 additions & 1 deletion packages/widget/src/hooks/useInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { useEffect } from 'react';
import { name, version } from '../config/version';
import { useTools } from './useTools';

let checkedPackageUpdates = false;

export const useInitializer = () => {
useTools();
useEffect(() => {
checkPackageUpdates(name, version);
if (!checkedPackageUpdates) {
checkedPackageUpdates = true;
checkPackageUpdates(name, version);
}
}, []);
};
36 changes: 16 additions & 20 deletions packages/widget/src/hooks/useTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@ type FormattedTool<T, K extends keyof T> = Record<string, Pick<T, K>>;
export const useTools = () => {
const lifi = useLiFi();
const { bridges, exchanges } = useWidgetConfig();
const { data } = useQuery(
['tools'],
({ signal }) => lifi.getTools(undefined, { signal }),
{
onSuccess(data) {
const { initializeTools } = useSettingsStore.getState();
initializeTools(
'Bridges',
data.bridges
.filter((bridge) => isItemAllowed(bridge.key, bridges))
.map((bridge) => bridge.key),
);
initializeTools(
'Exchanges',
data.exchanges
.filter((exchange) => isItemAllowed(exchange.key, exchanges))
.map((exchange) => exchange.key),
);
},
const { data } = useQuery(['tools'], () => lifi.getTools(), {
onSuccess(data) {
const { initializeTools } = useSettingsStore.getState();
initializeTools(
'Bridges',
data.bridges
.filter((bridge) => isItemAllowed(bridge.key, bridges))
.map((bridge) => bridge.key),
);
initializeTools(
'Exchanges',
data.exchanges
.filter((exchange) => isItemAllowed(exchange.key, exchanges))
.map((exchange) => exchange.key),
);
},
);
});

const formattedTools = useMemo(
() => ({
Expand Down

0 comments on commit 1892674

Please sign in to comment.