Upgrade nostr-tools

This commit is contained in:
Tristan Edwards 2022-12-25 21:17:02 +01:00
parent 6ec6e889e3
commit 17db9445aa
3 changed files with 19 additions and 11 deletions

View File

@ -68,6 +68,6 @@
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
"dependencies": { "dependencies": {
"nostr-tools": "^1.0.0" "nostr-tools": "^1.0.1"
} }
} }

View File

@ -12,6 +12,7 @@ import { uniqBy } from "./utils"
export { dateToUnix } from "./utils" export { dateToUnix } from "./utils"
type OnConnectFunc = (relay: Relay) => void type OnConnectFunc = (relay: Relay) => void
type OnDisconnectFunc = (relay: Relay) => void
type OnEventFunc = (event: NostrEvent) => void type OnEventFunc = (event: NostrEvent) => void
interface NostrContextType { interface NostrContextType {
@ -19,6 +20,7 @@ interface NostrContextType {
debug?: boolean debug?: boolean
connectedRelays: Relay[] connectedRelays: Relay[]
onConnect: (_onConnectCallback?: OnConnectFunc) => void onConnect: (_onConnectCallback?: OnConnectFunc) => void
onDisconnect: (_onDisconnectCallback?: OnDisconnectFunc) => void
publish: (event: NostrEvent) => void publish: (event: NostrEvent) => void
} }
@ -26,6 +28,7 @@ const NostrContext = createContext<NostrContextType>({
isLoading: true, isLoading: true,
connectedRelays: [], connectedRelays: [],
onConnect: () => null, onConnect: () => null,
onDisconnect: () => null,
publish: () => null, publish: () => null,
}) })
@ -59,6 +62,7 @@ export function NostrProvider({
} }
let onConnectCallback: null | OnConnectFunc = null let onConnectCallback: null | OnConnectFunc = null
let onDisconnectCallback: null | OnDisconnectFunc = null
useEffect(() => { useEffect(() => {
relayUrls.forEach(async (relayUrl) => { relayUrls.forEach(async (relayUrl) => {
@ -74,14 +78,13 @@ export function NostrProvider({
relay.on("disconnect", () => { relay.on("disconnect", () => {
log(debug, "warn", `👋 nostr: Connection closed for ${relayUrl}`) log(debug, "warn", `👋 nostr: Connection closed for ${relayUrl}`)
onDisconnectCallback?.(relay)
setConnectedRelays((prev) => prev.filter((r) => r.url !== relayUrl)) 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", () => {
// relay.on("error", () => { log(debug, "error", `❌ nostr: Error connecting to ${relayUrl}!`)
// log(debug, "error", `❌ nostr: Error connecting to ${relayUrl}!`) })
// console.log(`Error connecting to ${relay.url}`)
// })
}) })
}, [onConnectCallback, relayUrls]) }, [onConnectCallback, relayUrls])
@ -95,6 +98,11 @@ export function NostrProvider({
onConnectCallback = _onConnectCallback onConnectCallback = _onConnectCallback
} }
}, },
onDisconnect: (_onDisconnectCallback?: OnDisconnectFunc) => {
if (_onDisconnectCallback) {
onDisconnectCallback = _onDisconnectCallback
}
},
} }
return <NostrContext.Provider value={value}>{children}</NostrContext.Provider> return <NostrContext.Provider value={value}>{children}</NostrContext.Provider>
@ -116,7 +124,7 @@ export function useNostrEvents({ filter }: { filter: Filter }) {
const subscribe = (relay: Relay) => { const subscribe = (relay: Relay) => {
log(debug, "info", "⬆️ nostr: Sending event filter:", filter) log(debug, "info", "⬆️ nostr: Sending event filter:", filter)
const sub = relay.sub([filter], {}) const sub = relay.sub([filter])
unsubscribe = sub.unsub unsubscribe = sub.unsub

View File

@ -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" resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA== integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==
nostr-tools@^1.0.0: nostr-tools@^1.0.1:
version "1.0.0" version "1.0.1"
resolved "https://registry.npmjs.org/nostr-tools/-/nostr-tools-1.0.0.tgz" resolved "https://registry.yarnpkg.com/nostr-tools/-/nostr-tools-1.0.1.tgz#acf21758fc745674ed047e7dc219b30baf489005"
integrity sha512-PEElBgGJVS6rCzb+ydqKDlX3ouHhin8z8R4fu00abgcjAZ+qGuIbWF2wr1+Wen5z4XOsv2e6KwmLJIU4Sj5c4A== integrity sha512-URBNadrVq4qSmndzd4clZqubze4y/LB8cdzzen9mNwlFh3ICDdWp7TCShTbLEZQYPPSVoOe2n13l77jzcVvH/w==
dependencies: dependencies:
"@noble/hashes" "^0.5.7" "@noble/hashes" "^0.5.7"
"@noble/secp256k1" "^1.7.0" "@noble/secp256k1" "^1.7.0"