refactor: metadatacontroller as singleton
This commit is contained in:
parent
759a40a4f9
commit
1dfab7b82b
@ -37,7 +37,7 @@ import { setUserRobotImage } from '../../store/userRobotImage/action'
|
|||||||
import { Container } from '../Container'
|
import { Container } from '../Container'
|
||||||
import { ButtonIcon } from '../ButtonIcon'
|
import { ButtonIcon } from '../ButtonIcon'
|
||||||
|
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
|
|
||||||
export const AppBar = () => {
|
export const AppBar = () => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
@ -25,7 +25,7 @@ export class AuthController {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.nostrController = NostrController.getInstance()
|
this.nostrController = NostrController.getInstance()
|
||||||
this.metadataController = new MetadataController()
|
this.metadataController = MetadataController.getInstance()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,6 +22,7 @@ import {
|
|||||||
import { DEFAULT_LOOK_UP_RELAY_LIST } from '../utils/const'
|
import { DEFAULT_LOOK_UP_RELAY_LIST } from '../utils/const'
|
||||||
|
|
||||||
export class MetadataController extends EventEmitter {
|
export class MetadataController extends EventEmitter {
|
||||||
|
private static instance: MetadataController
|
||||||
private nostrController: NostrController
|
private nostrController: NostrController
|
||||||
private specialMetadataRelay = 'wss://purplepag.es'
|
private specialMetadataRelay = 'wss://purplepag.es'
|
||||||
private pendingFetches = new Map<string, Promise<Event | null>>() // Track pending fetches
|
private pendingFetches = new Map<string, Promise<Event | null>>() // Track pending fetches
|
||||||
@ -31,6 +32,13 @@ export class MetadataController extends EventEmitter {
|
|||||||
this.nostrController = NostrController.getInstance()
|
this.nostrController = NostrController.getInstance()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static getInstance(): MetadataController {
|
||||||
|
if (!MetadataController.instance) {
|
||||||
|
MetadataController.instance = new MetadataController()
|
||||||
|
}
|
||||||
|
return MetadataController.instance
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asynchronously checks for more recent metadata events authored by a specific key.
|
* Asynchronously checks for more recent metadata events authored by a specific key.
|
||||||
* If a more recent metadata event is found, it is handled and returned.
|
* If a more recent metadata event is found, it is handled and returned.
|
||||||
@ -119,7 +127,6 @@ export class MetadataController extends EventEmitter {
|
|||||||
// Check if the cached metadata is older than one day
|
// Check if the cached metadata is older than one day
|
||||||
if (isOlderThanOneDay(cachedMetadataEvent.cachedAt)) {
|
if (isOlderThanOneDay(cachedMetadataEvent.cachedAt)) {
|
||||||
// If older than one week, find the metadata from relays in background
|
// If older than one week, find the metadata from relays in background
|
||||||
|
|
||||||
this.checkForMoreRecentMetadata(hexKey, cachedMetadataEvent.event)
|
this.checkForMoreRecentMetadata(hexKey, cachedMetadataEvent.event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ export const MainLayout = () => {
|
|||||||
const hasSubscribed = useRef(false)
|
const hasSubscribed = useRef(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
dispatch(
|
dispatch(
|
||||||
|
@ -31,7 +31,7 @@ export const Nostr = () => {
|
|||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
const authController = new AuthController()
|
const authController = new AuthController()
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
const nostrController = NostrController.getInstance()
|
const nostrController = NostrController.getInstance()
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false)
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
|
@ -27,7 +27,7 @@ export const ProfilePage = () => {
|
|||||||
|
|
||||||
const { npub } = useParams()
|
const { npub } = useParams()
|
||||||
|
|
||||||
const metadataController = useMemo(() => new MetadataController(), [])
|
const metadataController = useMemo(() => MetadataController.getInstance(), [])
|
||||||
|
|
||||||
const [pubkey, setPubkey] = useState<string>()
|
const [pubkey, setPubkey] = useState<string>()
|
||||||
const [nostrJoiningBlock, setNostrJoiningBlock] =
|
const [nostrJoiningBlock, setNostrJoiningBlock] =
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
useTheme
|
useTheme
|
||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
import { UnsignedEvent, nip19, kinds, VerifiedEvent, Event } from 'nostr-tools'
|
import { UnsignedEvent, nip19, kinds, VerifiedEvent, Event } from 'nostr-tools'
|
||||||
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
import { Link, useParams } from 'react-router-dom'
|
import { Link, useParams } from 'react-router-dom'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
import { MetadataController, NostrController } from '../../../controllers'
|
import { MetadataController, NostrController } from '../../../controllers'
|
||||||
@ -41,7 +41,7 @@ export const ProfileSettingsPage = () => {
|
|||||||
|
|
||||||
const dispatch: Dispatch = useDispatch()
|
const dispatch: Dispatch = useDispatch()
|
||||||
|
|
||||||
const metadataController = useMemo(() => new MetadataController(), [])
|
const metadataController = MetadataController.getInstance()
|
||||||
const nostrController = NostrController.getInstance()
|
const nostrController = NostrController.getInstance()
|
||||||
|
|
||||||
const [pubkey, setPubkey] = useState<string>()
|
const [pubkey, setPubkey] = useState<string>()
|
||||||
|
@ -13,7 +13,7 @@ import { setRelayInfoAction } from '../store/actions'
|
|||||||
export const getNostrJoiningBlockNumber = async (
|
export const getNostrJoiningBlockNumber = async (
|
||||||
hexKey: string
|
hexKey: string
|
||||||
): Promise<NostrJoiningBlock | null> => {
|
): Promise<NostrJoiningBlock | null> => {
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
|
|
||||||
const relaySet = await metadataController.findRelayListMetadata(hexKey)
|
const relaySet = await metadataController.findRelayListMetadata(hexKey)
|
||||||
|
|
||||||
|
@ -365,7 +365,7 @@ export const getUsersAppData = async (): Promise<UserAppData | null> => {
|
|||||||
// Check if relayMap is undefined in the Redux store
|
// Check if relayMap is undefined in the Redux store
|
||||||
if (!relayMap) {
|
if (!relayMap) {
|
||||||
// If relayMap is not present, fetch relay list metadata
|
// If relayMap is not present, fetch relay list metadata
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
const relaySet = await metadataController
|
const relaySet = await metadataController
|
||||||
.findRelayListMetadata(usersPubkey)
|
.findRelayListMetadata(usersPubkey)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@ -835,7 +835,7 @@ const getUserAppDataFromBlossom = async (url: string, privateKey: string) => {
|
|||||||
*/
|
*/
|
||||||
export const subscribeForSigits = async (pubkey: string) => {
|
export const subscribeForSigits = async (pubkey: string) => {
|
||||||
// Instantiate the MetadataController to retrieve relay list metadata
|
// Instantiate the MetadataController to retrieve relay list metadata
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
const relaySet = await metadataController
|
const relaySet = await metadataController
|
||||||
.findRelayListMetadata(pubkey)
|
.findRelayListMetadata(pubkey)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@ -939,7 +939,7 @@ export const sendNotification = async (receiver: string, meta: Meta) => {
|
|||||||
const wrappedEvent = createWrap(unsignedEvent, receiver)
|
const wrappedEvent = createWrap(unsignedEvent, receiver)
|
||||||
|
|
||||||
// Instantiate the MetadataController to retrieve relay list metadata
|
// Instantiate the MetadataController to retrieve relay list metadata
|
||||||
const metadataController = new MetadataController()
|
const metadataController = MetadataController.getInstance()
|
||||||
const relaySet = await metadataController
|
const relaySet = await metadataController
|
||||||
.findRelayListMetadata(receiver)
|
.findRelayListMetadata(receiver)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user