feat: 💨

This commit is contained in:
codytseng
2025-07-27 22:21:19 +08:00
parent 4286551b12
commit 5baa0b0ce3
2 changed files with 14 additions and 22 deletions

View File

@@ -72,7 +72,7 @@ type TNostrContext = {
const NostrContext = createContext<TNostrContext | undefined>(undefined) const NostrContext = createContext<TNostrContext | undefined>(undefined)
let lastPublishedSeenNotificationsAtEventAt = -1 const lastPublishedSeenNotificationsAtEventAtMap = new Map<string, number>()
export const useNostr = () => { export const useNostr = () => {
const context = useContext(NostrContext) const context = useContext(NostrContext)
@@ -691,12 +691,14 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
}, 5_000) }, 5_000)
// Prevent too frequent requests for signing seen notifications events // Prevent too frequent requests for signing seen notifications events
const lastPublishedSeenNotificationsAtEventAt =
lastPublishedSeenNotificationsAtEventAtMap.get(account.pubkey) ?? -1
if ( if (
lastPublishedSeenNotificationsAtEventAt < 0 || lastPublishedSeenNotificationsAtEventAt < 0 ||
now - lastPublishedSeenNotificationsAtEventAt > 10 * 60 // 10 minutes now - lastPublishedSeenNotificationsAtEventAt > 10 * 60 // 10 minutes
) { ) {
await publish(createSeenNotificationsAtDraftEvent()) await publish(createSeenNotificationsAtDraftEvent())
lastPublishedSeenNotificationsAtEventAt = now lastPublishedSeenNotificationsAtEventAtMap.set(account.pubkey, now)
} }
} }

View File

@@ -12,30 +12,21 @@ export type TPollResults = {
updatedAt: number updatedAt: number
} }
class PollResultsService { type TFetchPollResultsParams = {
static instance: PollResultsService
private pollResultsMap: Map<string, TPollResults> = new Map()
private pollResultsSubscribers = new Map<string, Set<() => void>>()
private loader = new DataLoader<
{
pollEventId: string pollEventId: string
relays: string[] relays: string[]
validPollOptionIds: string[] validPollOptionIds: string[]
isMultipleChoice: boolean isMultipleChoice: boolean
endsAt?: number endsAt?: number
},
TPollResults | undefined
>(
async (params) => {
const pollMap = new Map<
string,
{
relays: string[]
validPollOptionIds: string[]
isMultipleChoice: boolean
endsAt?: number
} }
>()
class PollResultsService {
static instance: PollResultsService
private pollResultsMap: Map<string, TPollResults> = new Map()
private pollResultsSubscribers = new Map<string, Set<() => void>>()
private loader = new DataLoader<TFetchPollResultsParams, TPollResults | undefined>(
async (params) => {
const pollMap = new Map<string, Omit<TFetchPollResultsParams, 'pollEventId'>>()
params.forEach(({ pollEventId, relays, validPollOptionIds, isMultipleChoice, endsAt }) => { params.forEach(({ pollEventId, relays, validPollOptionIds, isMultipleChoice, endsAt }) => {
if (!pollMap.has(pollEventId)) { if (!pollMap.has(pollEventId)) {
@@ -97,7 +88,6 @@ class PollResultsService {
isMultipleChoice: boolean, isMultipleChoice: boolean,
endsAt?: number endsAt?: number
) { ) {
console.log('Fetching poll results for:', pollEventId)
const filter: Filter = { const filter: Filter = {
kinds: [ExtendedKind.POLL_RESPONSE], kinds: [ExtendedKind.POLL_RESPONSE],
'#e': [pollEventId], '#e': [pollEventId],