Skip to content

Commit

Permalink
fix stream being reused while destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
LuKks committed Mar 30, 2023
1 parent 9f92709 commit 65ab3ba
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions dht.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ const DHT_RELAY_ADDRESS = 'wss://dht1-relay.leet.ar:49443'

const DHTContext = createContext()

export const DHT = ({ children, url, stream, keyPair, ...options }) => {
export const DHT = ({ children, url, keyPair, ...options }) => {
const [dht, setDHT] = useState(null)

useEffect(() => {
let ws

if (!stream) {
ws = new window.WebSocket(url || DHT_RELAY_ADDRESS)
stream = new Stream(true, ws)
}
const ws = new window.WebSocket(url || DHT_RELAY_ADDRESS)
const stream = new Stream(true, ws)

keyPair = keyPair || DHTRelay.keyPair(primaryKey)
const dht = new DHTRelay(stream, { keyPair, ...options })
setDHT(dht)

return () => dht.destroy().catch(safetyCatch)
}, [stream, keyPair])
const relay = new DHTRelay(stream, { keyPair, ...options })
setDHT(relay)

return () => {
relay.destroy().catch(safetyCatch)
}
}, [keyPair])

return React.createElement(
DHTContext.Provider,
Expand Down

0 comments on commit 65ab3ba

Please sign in to comment.