fix: abort and unsub if we unmount inside the loop, and check after the loop as well

This commit is contained in:
Derrick Hammer 2023-10-12 04:40:08 -04:00
parent e574100bb5
commit 03434b4329
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 11 additions and 0 deletions

View File

@ -58,12 +58,21 @@ const LumeProvider = ({ children }) => {
}, []); }, []);
const fetchAndUpdateNetworks = async () => { const fetchAndUpdateNetworks = async () => {
const unsub = () => {
statusUnsubs.current.forEach((unsub) => unsub());
statusUnsubs.current = new Map<any, any>();
};
try { try {
const types = await networkRegistry.getTypes(); const types = await networkRegistry.getTypes();
const newNetworksMap = new Map(); const newNetworksMap = new Map();
const newStatusUnsubs = new Map(); const newStatusUnsubs = new Map();
for (const type of types) { for (const type of types) {
if (!isMounted.current) {
unsub();
return;
}
const list = await networkRegistry.getNetworksByType(type); const list = await networkRegistry.getNetworksByType(type);
for (const module of list) { for (const module of list) {
const client = createNetworkClient(module); const client = createNetworkClient(module);
@ -101,6 +110,8 @@ const LumeProvider = ({ children }) => {
...prevLume, ...prevLume,
networks: Array.from(newNetworksMap.values()), networks: Array.from(newNetworksMap.values()),
})); }));
} else {
unsub();
} }
} catch (error) { } catch (error) {
if (isMounted.current) { if (isMounted.current) {