From 17db9445aa9539c1b63342ada3d744c8315387e0 Mon Sep 17 00:00:00 2001 From: Tristan Edwards Date: Sun, 25 Dec 2022 21:17:02 +0100 Subject: [PATCH] Upgrade nostr-tools --- package.json | 2 +- src/index.tsx | 20 ++++++++++++++------ yarn.lock | 8 ++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 9398196..2f23789 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,6 @@ "typescript": "^4.9.4" }, "dependencies": { - "nostr-tools": "^1.0.0" + "nostr-tools": "^1.0.1" } } diff --git a/src/index.tsx b/src/index.tsx index 6a1b0a0..f0b5ce3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -12,6 +12,7 @@ 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 { @@ -19,6 +20,7 @@ interface NostrContextType { debug?: boolean connectedRelays: Relay[] onConnect: (_onConnectCallback?: OnConnectFunc) => void + onDisconnect: (_onDisconnectCallback?: OnDisconnectFunc) => void publish: (event: NostrEvent) => void } @@ -26,6 +28,7 @@ const NostrContext = createContext({ isLoading: true, connectedRelays: [], onConnect: () => null, + onDisconnect: () => null, publish: () => null, }) @@ -59,6 +62,7 @@ export function NostrProvider({ } let onConnectCallback: null | OnConnectFunc = null + let onDisconnectCallback: null | OnDisconnectFunc = null useEffect(() => { relayUrls.forEach(async (relayUrl) => { @@ -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]) @@ -95,6 +98,11 @@ export function NostrProvider({ onConnectCallback = _onConnectCallback } }, + onDisconnect: (_onDisconnectCallback?: OnDisconnectFunc) => { + if (_onDisconnectCallback) { + onDisconnectCallback = _onDisconnectCallback + } + }, } return {children} @@ -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 diff --git a/yarn.lock b/yarn.lock index f9fe359..acd7bbc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"