fix: types

This commit is contained in:
Shusui MOYATANI
2023-11-29 01:01:58 +09:00
parent 4e7ce8a613
commit 658363bf5a
5 changed files with 25 additions and 18 deletions

View File

@@ -8,20 +8,20 @@ import useStats from '@/nostr/useStats';
import ObservableTask from '@/utils/batch/ObservableTask';
import useBatch from '@/utils/batch/useBatch';
type ProfileTask = { type: 'Profile'; pubkey: string };
type EventTask = { type: 'Event'; eventId: string };
type ReactionsTask = { type: 'Reactions'; mentionedEventId: string };
type ZapReceiptsTask = { type: 'ZapReceipts'; mentionedEventId: string };
type RepostsTask = { type: 'Reposts'; mentionedEventId: string };
type FollowingsTask = { type: 'Followings'; pubkey: string };
type ParameterizedReplaceableEventTask = {
export type ProfileTask = { type: 'Profile'; pubkey: string };
export type EventTask = { type: 'Event'; eventId: string };
export type ReactionsTask = { type: 'Reactions'; mentionedEventId: string };
export type ZapReceiptsTask = { type: 'ZapReceipts'; mentionedEventId: string };
export type RepostsTask = { type: 'Reposts'; mentionedEventId: string };
export type FollowingsTask = { type: 'Followings'; pubkey: string };
export type ParameterizedReplaceableEventTask = {
type: 'ParameterizedReplaceableEvent';
kind: number;
author: string;
identifier: string;
};
type TaskArgs = [
export type TaskArgs = [
ProfileTask,
EventTask,
FollowingsTask,

View File

@@ -3,7 +3,7 @@ import { createMemo } from 'solid-js';
import { createQuery, type CreateQueryResult } from '@tanstack/solid-query';
import { Event as NostrEvent } from 'nostr-tools';
import { registerTask, BatchedEventsTask } from '@/nostr/useBatchedEvents';
import { registerTask, BatchedEventsTask, EventTask } from '@/nostr/useBatchedEvents';
import timeout from '@/utils/timeout';
export type UseEventProps = {
@@ -24,7 +24,7 @@ const useEvent = (propsProvider: () => UseEventProps | null): UseEvent => {
const [, currentProps] = queryKey;
if (currentProps == null) return null;
const { eventId } = currentProps;
const task = new BatchedEventsTask({ type: 'Event', eventId });
const task = new BatchedEventsTask<EventTask>({ type: 'Event', eventId });
const promise = task.firstEventPromise().catch(() => {
throw new Error(`event not found: ${eventId}`);
});

View File

@@ -5,7 +5,7 @@ import { Event as NostrEvent } from 'nostr-tools';
import { genericEvent } from '@/nostr/event';
import { latestEventQuery } from '@/nostr/query';
import { BatchedEventsTask, registerTask } from '@/nostr/useBatchedEvents';
import { BatchedEventsTask, FollowingsTask, registerTask } from '@/nostr/useBatchedEvents';
type Following = {
pubkey: string;
@@ -56,7 +56,7 @@ export const fetchLatestFollowings = async (
{ pubkey }: UseFollowingsProps,
signal?: AbortSignal,
) => {
const task = new BatchedEventsTask({ type: 'Followings', pubkey });
const task = new BatchedEventsTask<FollowingsTask>({ type: 'Followings', pubkey });
registerTask({ task, signal });
const latestFollowings = await task.latestEventPromise();
@@ -74,7 +74,7 @@ const useFollowings = (propsProvider: () => UseFollowingsProps | null): UseFollo
taskProvider: ([, currentProps]) => {
if (currentProps == null) return null;
const { pubkey } = currentProps;
return new BatchedEventsTask({ type: 'Followings', pubkey });
return new BatchedEventsTask<FollowingsTask>({ type: 'Followings', pubkey });
},
queryClient,
}),

View File

@@ -4,7 +4,11 @@ import { createQuery, useQueryClient, type CreateQueryResult } from '@tanstack/s
import { Event as NostrEvent } from 'nostr-tools';
import { pickLatestEvent } from '@/nostr/event/comparator';
import { registerTask, BatchedEventsTask } from '@/nostr/useBatchedEvents';
import {
registerTask,
BatchedEventsTask,
ParameterizedReplaceableEventTask,
} from '@/nostr/useBatchedEvents';
import timeout from '@/utils/timeout';
// Parameterized Replaceable Event
@@ -34,7 +38,7 @@ const useParameterizedReplaceableEvent = (
if (currentProps == null) return Promise.resolve(null);
const { kind, author, identifier } = currentProps;
const task = new BatchedEventsTask({
const task = new BatchedEventsTask<ParameterizedReplaceableEventTask>({
type: 'ParameterizedReplaceableEvent',
kind,
author,

View File

@@ -5,7 +5,7 @@ import { Event as NostrEvent } from 'nostr-tools';
import { Profile, ProfileWithOtherProperties, safeParseProfile } from '@/nostr/event/Profile';
import { latestEventQuery } from '@/nostr/query';
import { BatchedEventsTask } from '@/nostr/useBatchedEvents';
import { BatchedEventsTask, ProfileTask } from '@/nostr/useBatchedEvents';
export type UseProfileProps = {
pubkey: string;
@@ -13,6 +13,7 @@ export type UseProfileProps = {
export type UseProfile = {
profile: () => ProfileWithOtherProperties | null;
event: () => NostrEvent | null | undefined;
invalidateProfile: () => Promise<void>;
query: CreateQueryResult<NostrEvent | null>;
};
@@ -39,7 +40,7 @@ const useProfile = (propsProvider: () => UseProfileProps | null): UseProfile =>
taskProvider: ([, currentProps]) => {
if (currentProps == null) return null;
const { pubkey } = currentProps;
return new BatchedEventsTask({ type: 'Profile', pubkey });
return new BatchedEventsTask<ProfileTask>({ type: 'Profile', pubkey });
},
queryClient,
}),
@@ -53,6 +54,8 @@ const useProfile = (propsProvider: () => UseProfileProps | null): UseProfile =>
},
);
const event = () => query.data;
const profile = createMemo((): Profile | null => {
if (query.data == null) return null;
const { content } = query.data;
@@ -61,7 +64,7 @@ const useProfile = (propsProvider: () => UseProfileProps | null): UseProfile =>
const invalidateProfile = (): Promise<void> => queryClient.invalidateQueries(genQueryKey());
return { profile, invalidateProfile, query };
return { profile, event, invalidateProfile, query };
};
export default useProfile;