refactor: Query emits Filters

This commit is contained in:
2024-01-09 12:51:33 +00:00
parent 18beed13c3
commit 4455651d47
40 changed files with 416 additions and 404 deletions

View File

@ -1,5 +1,5 @@
import { useMemo } from "react";
import { RequestBuilder, ReplaceableNoteStore, NostrLink, NoteCollection } from "@snort/system";
import { RequestBuilder, NostrLink } from "@snort/system";
import { useRequestBuilder } from "./useRequestBuilder";
export function useEventFeed(link: NostrLink) {
@ -9,7 +9,7 @@ export function useEventFeed(link: NostrLink) {
return b;
}, [link]);
return useRequestBuilder(ReplaceableNoteStore, sub);
return useRequestBuilder(sub);
}
export function useEventsFeed(id: string, links: Array<NostrLink>) {
@ -19,5 +19,5 @@ export function useEventsFeed(id: string, links: Array<NostrLink>) {
return b;
}, [id, links]);
return useRequestBuilder(NoteCollection, sub);
return useRequestBuilder(sub);
}

View File

@ -30,5 +30,5 @@ export function useReactions(
return rb.numFilters > 0 ? rb : null;
}, [ids]);
return useRequestBuilder(NoteCollection, sub);
return useRequestBuilder(sub);
}

View File

@ -6,14 +6,13 @@ import { SnortContext } from "./context";
/**
* Send a query to the relays and wait for data
*/
const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TStore["getSnapshotData"]>>(
type: { new (): TStore },
const useRequestBuilder = (
rb: RequestBuilder | null,
) => {
const system = useContext(SnortContext);
const subscribe = (onChanged: () => void) => {
if (rb) {
const q = system.Query<TStore>(type, rb);
const q = system.Query(rb);
q.on("event", onChanged);
q.uncancel();
return () => {
@ -25,14 +24,14 @@ const useRequestBuilder = <TStore extends NoteStore, TSnapshot = ReturnType<TSto
// noop
};
};
const getState = (): StoreSnapshot<TSnapshot> => {
const getState = () => {
const q = system.GetQuery(rb?.id ?? "");
if (q) {
return q.snapshot as StoreSnapshot<TSnapshot>;
return q.snapshot;
}
return EmptySnapshot as StoreSnapshot<TSnapshot>;
return EmptySnapshot;
};
return useSyncExternalStore<StoreSnapshot<TSnapshot>>(
return useSyncExternalStore(
v => subscribe(v),
() => getState(),
);

View File

@ -10,16 +10,23 @@ export function useUserProfile(pubKey?: HexKey): CachedMetadata | undefined {
return useSyncExternalStore<CachedMetadata | undefined>(
h => {
if (pubKey) {
system.ProfileLoader.TrackKeys(pubKey);
const handler = (keys: Array<string>) => {
if (keys.includes(pubKey)) {
h();
}
};
system.profileLoader.cache.on("change", handler);
system.profileLoader.TrackKeys(pubKey);
return () => {
system.profileLoader.cache.off("change", handler);
system.profileLoader.UntrackKeys(pubKey);
};
}
const release = system.ProfileLoader.Cache.hook(h, pubKey);
return () => {
release();
if (pubKey) {
system.ProfileLoader.UntrackKeys(pubKey);
}
// noop
};
},
() => system.ProfileLoader.Cache.getFromCache(pubKey),
() => system.profileLoader.cache.getFromCache(pubKey),
);
}

View File

@ -5,7 +5,7 @@ import { SnortContext } from "./context";
export function useUserSearch() {
const system = useContext(SnortContext);
const cache = system.ProfileLoader.Cache as UserProfileCache;
const cache = system.profileLoader.cache as UserProfileCache;
async function search(input: string): Promise<Array<string>> {
// try exact match first