mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-18 06:24:25 +01:00
update
This commit is contained in:
@@ -2,11 +2,10 @@ import { createMemo, type Accessor } from 'solid-js';
|
||||
import { type Event as NostrEvent } from 'nostr-tools/event';
|
||||
import { createQuery, type CreateQueryResult } from '@tanstack/solid-query';
|
||||
|
||||
import useConfig from '@/clients/useConfig';
|
||||
import useBatch, { type Task } from '@/clients/useBatch';
|
||||
import useSubscription from '@/clients/useSubscription';
|
||||
import useBatchedEvent from '@/clients/useBatchedEvent';
|
||||
|
||||
export type UseEventProps = {
|
||||
// TODO リレーURLを考慮したい
|
||||
relayUrls: string[];
|
||||
eventId: string;
|
||||
};
|
||||
@@ -16,36 +15,14 @@ export type UseEvent = {
|
||||
query: CreateQueryResult<NostrEvent>;
|
||||
};
|
||||
|
||||
const { exec } = useBatch<UseEventProps, NostrEvent>(() => {
|
||||
return {
|
||||
executor: (tasks) => {
|
||||
// TODO relayUrlsを考慮する
|
||||
const [config] = useConfig();
|
||||
const eventIdTaskMap = new Map<string, Task<UseEventProps, NostrEvent>>(
|
||||
tasks.map((task) => [task.args.eventId, task]),
|
||||
);
|
||||
const eventIds = Array.from(eventIdTaskMap.keys());
|
||||
|
||||
useSubscription(() => ({
|
||||
relayUrls: config().relayUrls,
|
||||
filters: [
|
||||
{
|
||||
ids: eventIds,
|
||||
kinds: [1],
|
||||
},
|
||||
],
|
||||
continuous: false,
|
||||
onEvent: (event: NostrEvent) => {
|
||||
if (event.id == null) return;
|
||||
const task = eventIdTaskMap.get(event.id);
|
||||
// possibly, the new event received
|
||||
if (task == null) return;
|
||||
task.resolve(event);
|
||||
},
|
||||
}));
|
||||
},
|
||||
};
|
||||
});
|
||||
const { exec } = useBatchedEvent<UseEventProps>(() => ({
|
||||
generateKey: ({ eventId }: UseEventProps) => eventId,
|
||||
mergeFilters: (args: UseEventProps[]) => {
|
||||
const eventIds = args.map((arg) => arg.eventId);
|
||||
return [{ kinds: [1], ids: eventIds }];
|
||||
},
|
||||
extractKey: (event: NostrEvent) => event.id,
|
||||
}));
|
||||
|
||||
const useEvent = (propsProvider: () => UseEventProps): UseEvent => {
|
||||
const props = createMemo(propsProvider);
|
||||
|
||||
Reference in New Issue
Block a user