This commit is contained in:
Shusui MOYATANI
2023-03-02 02:27:53 +09:00
parent b1aa63d6a3
commit 3ce64a449d
10 changed files with 133 additions and 129 deletions

View File

@@ -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);