Skip to content

Commit

Permalink
Upgrade nostr-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
t4t5 committed Dec 25, 2022
1 parent 6ec6e88 commit 17db944
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
"typescript": "^4.9.4"
},
"dependencies": {
"nostr-tools": "^1.0.0"
"nostr-tools": "^1.0.1"
}
}
20 changes: 14 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ import { uniqBy } from "./utils"
export { dateToUnix } from "./utils"

type OnConnectFunc = (relay: Relay) => void
type OnDisconnectFunc = (relay: Relay) => void
type OnEventFunc = (event: NostrEvent) => void

interface NostrContextType {
isLoading: boolean
debug?: boolean
connectedRelays: Relay[]
onConnect: (_onConnectCallback?: OnConnectFunc) => void
onDisconnect: (_onDisconnectCallback?: OnDisconnectFunc) => void
publish: (event: NostrEvent) => void
}

const NostrContext = createContext<NostrContextType>({
isLoading: true,
connectedRelays: [],
onConnect: () => null,
onDisconnect: () => null,
publish: () => null,
})

Expand Down Expand Up @@ -59,6 +62,7 @@ export function NostrProvider({
}

let onConnectCallback: null | OnConnectFunc = null
let onDisconnectCallback: null | OnDisconnectFunc = null

useEffect(() => {
relayUrls.forEach(async (relayUrl) => {
Expand All @@ -74,14 +78,13 @@ export function NostrProvider({

relay.on("disconnect", () => {
log(debug, "warn", `👋 nostr: Connection closed for ${relayUrl}`)
onDisconnectCallback?.(relay)
setConnectedRelays((prev) => prev.filter((r) => r.url !== relayUrl))
})

// Wait for this to be merged: https://github.com/fiatjaf/nostr-tools/pull/69
// relay.on("error", () => {
// log(debug, "error", `❌ nostr: Error connecting to ${relayUrl}!`)
// console.log(`Error connecting to ${relay.url}`)
// })
relay.on("error", () => {
log(debug, "error", `❌ nostr: Error connecting to ${relayUrl}!`)
})
})
}, [onConnectCallback, relayUrls])

Expand All @@ -95,6 +98,11 @@ export function NostrProvider({
onConnectCallback = _onConnectCallback
}
},
onDisconnect: (_onDisconnectCallback?: OnDisconnectFunc) => {
if (_onDisconnectCallback) {
onDisconnectCallback = _onDisconnectCallback
}
},
}

return <NostrContext.Provider value={value}>{children}</NostrContext.Provider>
Expand All @@ -116,7 +124,7 @@ export function useNostrEvents({ filter }: { filter: Filter }) {

const subscribe = (relay: Relay) => {
log(debug, "info", "⬆️ nostr: Sending event filter:", filter)
const sub = relay.sub([filter], {})
const sub = relay.sub([filter])

unsubscribe = sub.unsub

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4622,10 +4622,10 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

nostr-tools@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/nostr-tools/-/nostr-tools-1.0.0.tgz"
integrity sha512-PEElBgGJVS6rCzb+ydqKDlX3ouHhin8z8R4fu00abgcjAZ+qGuIbWF2wr1+Wen5z4XOsv2e6KwmLJIU4Sj5c4A==
nostr-tools@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.0.1.tgz#acf21758fc745674ed047e7dc219b30baf489005"
integrity sha512-URBNadrVq4qSmndzd4clZqubze4y/LB8cdzzen9mNwlFh3ICDdWp7TCShTbLEZQYPPSVoOe2n13l77jzcVvH/w==
dependencies:
"@noble/hashes" "^0.5.7"
"@noble/secp256k1" "^1.7.0"
Expand Down

0 comments on commit 17db944

Please sign in to comment.