Skip to content

Commit

Permalink
fix: restructured useWebRtc hook to better utilize useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Aug 26, 2021
1 parent 52a01e0 commit 3ca00b5
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/feature/provider/hook/useWebRtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
}

0 comments on commit 3ca00b5

Please sign in to comment.