Skip to content

Commit

Permalink
fix useCoreEvent to trigger initial change
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKks committed Mar 30, 2023
1 parent 65ab3ba commit a0a0326
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const Core = ({ children, storage, publicKey, ...options }) => {

return () => {
core.off('ready', onready)
// + should setCore(null, core close)?
core.close().catch(safetyCatch)
}
}, [storage, publicKey, ...deps])
Expand Down Expand Up @@ -72,14 +71,32 @@ export const useCoreWatch = (events = EVENTS) => {
const { core } = useCore()
const [onwatch, setUpdated] = useState(0)

const length = useRef(0)
const peers = useRef(0)

useEffect(() => {
length.current = 0
peers.current = 0
}, [core])

useEffect(() => {
if (!core) return

const onchange = () => setUpdated(i => i + 1)
const onchange = () => {
length.current = core.length
peers.current = core.peers.length

setUpdated(i => i + 1)
}

for (const event of events) core.on(event, onchange)

// Try to trigger the initial change
if (events.includes('ready') && core.opened) onchange()
if (events.includes('close') && core.closed) onchange()
else if (events.includes('close') && core.closed) onchange()
else if (events.includes('append') && length < core.length) onchange()
else if (events.includes('peer-add') && peers < core.peers.length) onchange()
else if (events.includes('peer-remove') && peers > core.peers.length) onchange()

return () => {
for (const event of events) core.off(event, onchange)
Expand Down

0 comments on commit a0a0326

Please sign in to comment.