mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-19 06:54:23 +01:00
implement parseTextNote
This commit is contained in:
@@ -1,30 +1,48 @@
|
||||
import { createSignal, createEffect } from 'solid-js';
|
||||
import type { Event as NostrEvent } from 'nostr-tools/event';
|
||||
import type { Filter } from 'nostr-tools/filter';
|
||||
import type { SubscriptionOptions } from 'nostr-tools/relay';
|
||||
import usePool from '@/clients/usePool';
|
||||
|
||||
type UseSubscriptionProps = {
|
||||
export type UseSubscriptionProps = {
|
||||
relayUrls: string[];
|
||||
filters: Filter[];
|
||||
options?: SubscriptionOptions;
|
||||
};
|
||||
|
||||
const useSubscription = ({ relayUrls, filters, options }: UseSubscriptionProps) => {
|
||||
const sortEvents = (events: NostrEvent[]) => events.sort((a, b) => b.created_at - a.created_at);
|
||||
|
||||
const useSubscription = (propsProvider: () => UseSubscriptionProps) => {
|
||||
const pool = usePool();
|
||||
const [events, setEvents] = createSignal<Event[]>([]);
|
||||
const [events, setEvents] = createSignal<NostrEvent[]>([]);
|
||||
|
||||
createEffect(() => {
|
||||
const sub = pool().sub(relayUrls, filters, options);
|
||||
const tempEvents: Event[] = [];
|
||||
const { relayUrls, filters, options } = propsProvider();
|
||||
|
||||
sub.on('event', (event: Event) => {
|
||||
tempEvents.push(event);
|
||||
const sub = pool().sub(relayUrls, filters, options);
|
||||
let eose = false;
|
||||
const storedEvents: NostrEvent[] = [];
|
||||
|
||||
sub.on('event', (event: NostrEvent) => {
|
||||
if (!eose) {
|
||||
storedEvents.push(event);
|
||||
} else {
|
||||
setEvents((prevEvents) => sortEvents([event, ...prevEvents]));
|
||||
}
|
||||
});
|
||||
|
||||
sub.on('eose', () => {
|
||||
eose = true;
|
||||
setEvents(sortEvents(storedEvents));
|
||||
});
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
const newEvents = tempEvents.splice(0, tempEvents.length);
|
||||
setEvents((prevEvents) => [...newEvents, ...prevEvents]);
|
||||
}, 500);
|
||||
if (eose) {
|
||||
clearInterval(intervalId);
|
||||
return;
|
||||
}
|
||||
setEvents(sortEvents(storedEvents));
|
||||
}, 100);
|
||||
|
||||
return () => {
|
||||
sub.unsub();
|
||||
|
||||
Reference in New Issue
Block a user