mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-17 14:04:21 +01:00
initial
This commit is contained in:
38
src/clients/useSubscription.ts
Normal file
38
src/clients/useSubscription.ts
Normal 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;
|
||||
Reference in New Issue
Block a user