refactor relay pool and profile load

This commit is contained in:
KoalaSat 2023-01-23 15:58:56 +01:00
parent 3f9601faf8
commit 3107a03193
No known key found for this signature in database
GPG Key ID: 2F7F61C6146AB157
3 changed files with 4 additions and 13 deletions

View File

@ -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()
}
}

View File

@ -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', [
{

View File

@ -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<string, string[]>
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<Event | null> = async (event) => {