From 3ca00b5f846dfa5478a87e540015ef2742a5882b Mon Sep 17 00:00:00 2001 From: Joseph R Miles Date: Thu, 26 Aug 2021 13:18:40 -0700 Subject: [PATCH] fix: restructured useWebRtc hook to better utilize useMemo --- src/feature/provider/hook/useWebRtc.ts | 29 ++++++++++++++------------ 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/feature/provider/hook/useWebRtc.ts b/src/feature/provider/hook/useWebRtc.ts index 7042129..cf07310 100644 --- a/src/feature/provider/hook/useWebRtc.ts +++ b/src/feature/provider/hook/useWebRtc.ts @@ -10,20 +10,23 @@ export const useWebRtc = (room: string): WebrtcProvider => { const existingProvider = providers.get(WebrtcProvider)?.get(room) as WebrtcProvider | undefined - const provider = React.useMemo( - () => new WebrtcProvider(room, doc), - [doc, room] - ) + return React.useMemo( + () => { + if (existingProvider !== undefined) { + console.log(providers, existingProvider) + return existingProvider + } else { + const provider = new WebrtcProvider(room, doc) - if (existingProvider !== undefined) { - return existingProvider - } else { - if (!providers.has(WebrtcProvider)) { - providers.set(WebrtcProvider, new Map()) - } + if (!providers.has(WebrtcProvider)) { + providers.set(WebrtcProvider, new Map()) + } - providers.get(WebrtcProvider)?.set(room, provider) + providers.get(WebrtcProvider)?.set(room, provider) - return provider - } + return provider + } + }, + [existingProvider] + ) }