Last active
June 10, 2023 02:46
-
-
Save syusui-s/4fd82023f49eeeb2d15cd630121e59c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NostrEvent extends Event { | |
constructor(event) { | |
super('event'); | |
this.event = event; | |
} | |
} | |
const createBatchSubscription = () => { | |
let timer = null; | |
let filters = []; | |
const exec = () => { | |
const currentFilters = filters; | |
filters = []; | |
const mergedFilter = mergeFilters(currentFilters.map(({ filter }) => filter)); | |
const sub = pool.sub(relays, mergedFilter); | |
sub.on('event', (event) => { | |
currentFilters.forEach(({ filter, eventTarget }) => { | |
if (matchFilter(filter, event)) { | |
eventTarget.dispatchEvent(new NostrEvent(event)); | |
} | |
}); | |
}); | |
}; | |
const sub = (filter) => { | |
const eventTarget = new EventTarget(); | |
filters.push({ filter, eventTarget }); | |
if (timer == null) { | |
setTimeout(() => { | |
timer = null; | |
exec(); | |
}, 3000); | |
} | |
return eventTarget; | |
}; | |
return sub; | |
}; | |
const subEvent = createBatchSubscription(); | |
export const getEvent = (eventId) => subEvent({ kinds: [1], ids: [eventId] }); | |
const subReposts = createBatchSubscription(); | |
export const getReposts = (eventId) => subReposts({ kinds: [6], '#e': [eventId] }); | |
const subReactions = createBatchSubscription(); | |
export const getReactions = (eventId) => subReactions({ kinds: [7], '#e': [eventId] }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment