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

[WALL] Lubega / WALL-4120 / APIProvider memory leak console error #17140

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion packages/api-v2/src/APIProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ const APIProvider = ({ children }: PropsWithChildren<TAPIProviderProps>) => {
if (reconnect) {
connectionRef.current = initializeConnection(
() => {
reconnectTimerId = setTimeout(() => setReconnect(true), 500);
reconnectTimerId = setTimeout(() => {
if (isMounted.current) {
setReconnect(true);
}
}, 500);
},
() => {
if (!connectionRef.current) {
Expand Down
18 changes: 14 additions & 4 deletions packages/api/src/hooks/useRemoteConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { ObjectUtils } from '@deriv-com/utils';
import initData from '../remote_config.json';

Expand All @@ -17,22 +17,32 @@ const remoteConfigQuery = async function () {

function useRemoteConfig(enabled = false) {
const [data, setData] = useState(initData);
const isMounted = useRef(false);

useEffect(() => {
enabled &&
isMounted.current = true;

return () => {
isMounted.current = false;
};
}, []);

useEffect(() => {
if (enabled) {
remoteConfigQuery()
.then(async res => {
const resHash = await ObjectUtils.hashObject(res);
const dataHash = await ObjectUtils.hashObject(data);
if (resHash !== dataHash) {
if (resHash !== dataHash && isMounted.current) {
setData(res);
}
})
.catch(error => {
// eslint-disable-next-line no-console
console.log('Remote Config error: ', error);
});
}, [enabled]);
}
}, [enabled, data]);

return { data };
}
Expand Down
1 change: 1 addition & 0 deletions packages/appstore/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const default_plugins = [
new Dotenv(),
new DefinePlugin({
'process.env.TRUSTPILOT_API_KEY': JSON.stringify(process.env.TRUSTPILOT_API_KEY),
'process.env.REMOTE_CONFIG_URL': JSON.stringify(process.env.REMOTE_CONFIG_URL),
}),
new IgnorePlugin({ resourceRegExp: /^\.\/locale$/, contextRegExp: /moment$/ }),
new CircularDependencyPlugin({ exclude: /node_modules/, failOnError: true }),
Expand Down
10 changes: 7 additions & 3 deletions packages/hooks/src/useGrowthbookGetFeatureValue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState, useEffect } from 'react';
import { Analytics } from '@deriv-com/analytics';
import { getFeatureFlag } from '@deriv/utils';
import { useIsMounted } from 'usehooks-ts';

interface UseGrowthbookGetFeatureValueArgs<T> {
featureFlag: string;
Expand All @@ -14,6 +15,7 @@ const useGrowthbookGetFeatureValue = <T extends string | boolean>({
const resolvedDefaultValue: T = defaultValue !== undefined ? defaultValue : (false as T);
const [featureFlagValue, setFeatureFlagValue] = useState<boolean>(false);
const [isGBLoaded, setIsGBLoaded] = useState(false);
const isMounted = useIsMounted();

// Required for debugging Growthbook, this will be removed after this is added in the Analytics directly.
if (typeof window !== 'undefined') {
Expand All @@ -23,12 +25,14 @@ const useGrowthbookGetFeatureValue = <T extends string | boolean>({
useEffect(() => {
const fetchFeatureFlag = async () => {
const is_enabled = await getFeatureFlag(featureFlag, resolvedDefaultValue);
setFeatureFlagValue(is_enabled);
setIsGBLoaded(true);
if (isMounted()) {
setFeatureFlagValue(is_enabled);
setIsGBLoaded(true);
}
};

fetchFeatureFlag();
}, []);
}, [featureFlag, resolvedDefaultValue, isMounted]);

return [featureFlagValue, isGBLoaded];
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import useEmblaCarousel, { EmblaCarouselType } from 'embla-carousel-react';
import { useHover } from 'usehooks-ts';
import { useHover, useIsMounted } from 'usehooks-ts';
import { useAllWalletAccounts, useAuthorize } from '@deriv/api-v2';
import { LabelPairedChevronLeftLgFillIcon, LabelPairedChevronRightLgFillIcon } from '@deriv/quill-icons';
import { Localize } from '@deriv-com/translations';
Expand All @@ -26,16 +26,22 @@ const WalletsAddMoreCarousel: React.FC = () => {
});
const hoverRef = useRef<HTMLDivElement>(null);
const isHover = useHover(hoverRef);
const isMounted = useIsMounted();
const [prevBtnEnabled, setPrevBtnEnabled] = useState(false);
const [nextBtnEnabled, setNextBtnEnabled] = useState(false);

const scrollPrev = useCallback(() => walletsAddMoreEmblaAPI?.scrollPrev(), [walletsAddMoreEmblaAPI]);
const scrollNext = useCallback(() => walletsAddMoreEmblaAPI?.scrollNext(), [walletsAddMoreEmblaAPI]);

const onSelect = useCallback((emblaApi: EmblaCarouselType) => {
setPrevBtnEnabled(emblaApi.canScrollPrev());
setNextBtnEnabled(emblaApi.canScrollNext());
}, []);
const onSelect = useCallback(
(emblaApi: EmblaCarouselType) => {
if (isMounted()) {
setPrevBtnEnabled(emblaApi.canScrollPrev());
setNextBtnEnabled(emblaApi.canScrollNext());
}
},
[isMounted]
);

useEffect(() => {
if (!walletsAddMoreEmblaAPI) return;
Expand Down
Loading