This commit is contained in:
Shusui MOYATANI
2023-02-17 17:15:13 +09:00
commit 2aa85b3ed9
42 changed files with 16424 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import { createSignal, createEffect } from 'solid-js';
import type { Filter } from 'nostr-tools/filter';
import type { SubscriptionOptions } from 'nostr-tools/relay';
import usePool from '@/clients/usePool';
type UseSubscriptionProps = {
relayUrls: string[];
filters: Filter[];
options?: SubscriptionOptions;
};
const useSubscription = ({ relayUrls, filters, options }: UseSubscriptionProps) => {
const pool = usePool();
const [events, setEvents] = createSignal<Event[]>([]);
createEffect(() => {
const sub = pool().sub(relayUrls, filters, options);
const tempEvents: Event[] = [];
sub.on('event', (event: Event) => {
tempEvents.push(event);
});
const intervalId = setInterval(() => {
const newEvents = tempEvents.splice(0, tempEvents.length);
setEvents((prevEvents) => [...newEvents, ...prevEvents]);
}, 500);
return () => {
sub.unsub();
clearInterval(intervalId);
};
});
return { events };
};
export default useSubscription;