From 3107a03193bd6fd7cad5c4d868c7df803c819b15 Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Mon, 23 Jan 2023 15:58:56 +0100 Subject: [PATCH] refactor relay pool and profile load --- frontend/Contexts/RelayPoolContext.tsx | 5 ++--- frontend/Pages/ProfileLoadPage/index.tsx | 2 +- frontend/lib/nostr/RelayPool/intex.ts | 10 +--------- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/frontend/Contexts/RelayPoolContext.tsx b/frontend/Contexts/RelayPoolContext.tsx index 8d41066..df94870 100644 --- a/frontend/Contexts/RelayPoolContext.tsx +++ b/frontend/Contexts/RelayPoolContext.tsx @@ -67,10 +67,9 @@ export const RelayPoolContextProvider = ({ if (database && publicKey) { DeviceEventEmitter.addListener('WebsocketEvent', debouncedEventIdHandler) DeviceEventEmitter.addListener('WebsocketConfirmation', debouncedConfirmationHandler) - const initRelayPool = new RelayPool([], privateKey) - initRelayPool.connect(publicKey, (eventId: string) => setLastEventId(eventId)) + const initRelayPool = new RelayPool(privateKey) + initRelayPool.connect(publicKey, () => setRelayPoolReady(true)) setRelayPool(initRelayPool) - setRelayPoolReady(true) loadRelays() } } diff --git a/frontend/Pages/ProfileLoadPage/index.tsx b/frontend/Pages/ProfileLoadPage/index.tsx index cd97beb..606a205 100644 --- a/frontend/Pages/ProfileLoadPage/index.tsx +++ b/frontend/Pages/ProfileLoadPage/index.tsx @@ -64,7 +64,7 @@ export const ProfileLoadPage: React.FC = () => { if (database && publicKey) { getUsers(database, {}).then((results) => { if (results && results.length > 0) { - setContactsCount(results.length) + setContactsCount(results.filter((user) => user.contact).length) const authors = [...results.map((user: User) => user.id), publicKey] relayPool?.subscribe('profile-load-notes', [ { diff --git a/frontend/lib/nostr/RelayPool/intex.ts b/frontend/lib/nostr/RelayPool/intex.ts index 0767817..1799bb3 100644 --- a/frontend/lib/nostr/RelayPool/intex.ts +++ b/frontend/lib/nostr/RelayPool/intex.ts @@ -18,19 +18,13 @@ export interface RelayMessage { } class RelayPool { - constructor(relaysUrls: string[], privateKey?: string) { + constructor(privateKey?: string) { this.privateKey = privateKey - this.relays = relaysUrls this.subscriptions = {} - - this.relays.forEach((relayUrl) => { - this.add(relayUrl) - }) } private readonly privateKey?: string private subscriptions: Record - public relays: string[] private readonly send: (message: object) => void = async (message) => { const tosend = JSON.stringify(message) @@ -47,7 +41,6 @@ class RelayPool { callback = () => {}, ) => { RelayPoolModule.add(relayUrl, callback) - this.relays.push(relayUrl) } public readonly remove: (relayUrl: string, callback?: () => void) => void = async ( @@ -55,7 +48,6 @@ class RelayPool { callback = () => {}, ) => { RelayPoolModule.remove(relayUrl, callback) - this.relays = this.relays.filter((relay) => relay === relayUrl) } public readonly sendEvent: (event: Event) => Promise = async (event) => {