From c94c876b15c7474f9d8a2a710bc633c2dfce5e1c Mon Sep 17 00:00:00 2001 From: Michel dos Santos Kuguio Date: Thu, 7 Jan 2021 21:37:18 -0300 Subject: [PATCH] refactor(code): fix problem with memory --- src/index.tsx | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 05c2617..77b9a29 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -48,29 +48,33 @@ export function createServiceRodneyBroadcast( const [data, setData] = useState(null as any); const [reciverId, setReciverId] = useState(undefined); const register = useCallback(async () => { - const idxRegister = await RodneyBroadcast.register( - filterName, - actionNames.join(';'), - eventName - ); - setReciverId(idxRegister); - }, []); - useEffect(() => { if (reciverId === undefined) { - register(); + const idxRegister = await RodneyBroadcast.register( + filterName, + actionNames.join(';'), + eventName + ); + setReciverId(idxRegister); DeviceEventEmitter.addListener(eventName, function (map) { setData(map); }); } + }, [reciverId]); + const unregister = useCallback(async () => { + if (reciverId !== undefined) { + await RodneyBroadcast.unregister(reciverId); + setReciverId(undefined); + DeviceEventEmitter.removeListener(eventName, function () { + console.log('Remove event'); + }); + } + }, [reciverId]); + useEffect(() => { + register(); return () => { - if (reciverId !== undefined) { - RodneyBroadcast.unregister(reciverId); - setReciverId(undefined); - } - // @ts-ignore - DeviceEventEmitter.removeListener(eventName); + unregister(); }; - }, [reciverId, register]); + }, [register, unregister]); const clear = useCallback(async () => { setData(null);