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

@ -44,6 +44,6 @@ export class FollowListCache extends RefreshFeedCache<TaggedNostrEvent> {
override async preload() {
await super.preload();
this.snapshot().forEach(e => socialGraphInstance.handleEvent(e));
this.cache.forEach(e => socialGraphInstance.handleEvent(e));
}
}

View File

@ -43,7 +43,10 @@ export class FollowsFeedCache extends RefreshFeedCache<TaggedNostrEvent> {
const filtered = evs.filter(a => this.#kinds.includes(a.kind));
if (filtered.length > 0) {
await this.bulkSet(filtered);
this.notifyChange(filtered.map(a => this.key(a)));
this.emit(
"change",
filtered.map(a => this.key(a)),
);
}
}
@ -64,7 +67,7 @@ export class FollowsFeedCache extends RefreshFeedCache<TaggedNostrEvent> {
const oldest = await this.table?.orderBy("created_at").first();
this.#oldest = oldest?.created_at;
this.notifyChange(latest?.map(a => this.key(a)) ?? []);
this.emit("change", latest?.map(a => this.key(a)) ?? []);
debug(this.name)(`Loaded %d/%d in %d ms`, latest?.length ?? 0, keys.length, (unixNowMs() - start).toLocaleString());
}
@ -96,7 +99,7 @@ export class FollowsFeedCache extends RefreshFeedCache<TaggedNostrEvent> {
this.onTable.add(k);
});
this.notifyChange(latest?.map(a => this.key(a)) ?? []);
this.emit("change", latest?.map(a => this.key(a)) ?? []);
}
}

View File

@ -33,7 +33,10 @@ export class NotificationsCache extends RefreshFeedCache<NostrEventForSession> {
forSession: pubKey,
})),
);
this.notifyChange(filtered.map(v => this.key(v)));
this.emit(
"change",
filtered.map(v => this.key(v)),
);
}
}

View File

@ -24,7 +24,6 @@ export abstract class RefreshFeedCache<T> extends FeedCache<TWithCreated<T>> {
override async preload(): Promise<void> {
await super.preload();
// load all dms to memory
await this.buffer([...this.onTable]);
}
}

View File

@ -285,15 +285,20 @@ class IrisAccount extends Component<Props> {
componentDidMount() {
const session = LoginStore.snapshot();
const myPub = session.publicKey;
System.ProfileLoader.Cache.hook(() => {
const profile = System.ProfileLoader.Cache.getFromCache(myPub);
const irisToActive = profile && profile.nip05 && profile.nip05.endsWith("@iris.to");
this.setState({ profile, irisToActive });
if (profile && !irisToActive) {
this.checkExistingAccount(myPub);
}
}, myPub);
this.checkExistingAccount(myPub);
if (myPub) {
System.profileLoader.cache.on("change", keys => {
if (keys.includes(myPub)) {
const profile = System.profileLoader.cache.getFromCache(myPub);
const irisToActive = profile && profile.nip05 && profile.nip05.endsWith("@iris.to");
this.setState({ profile, irisToActive });
if (profile && !irisToActive) {
this.checkExistingAccount(myPub);
}
}
});
this.checkExistingAccount(myPub);
}
}
async checkExistingAccount(pub: any) {

View File

@ -18,7 +18,7 @@ export function ProfileLink({
children?: ReactNode;
} & Omit<LinkProps, "to">) {
const system = useContext(SnortContext);
const relays = system.RelayCache.getFromCache(pubkey)
const relays = system.relayCache.getFromCache(pubkey)
?.relays?.filter(a => a.settings.write)
?.map(a => a.url);

View File

@ -1,4 +1,4 @@
import { EventKind, NoteCollection, RequestBuilder } from "@snort/system";
import { EventKind, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -15,5 +15,5 @@ export function useArticles() {
return rb;
}, [follows.timestamp]);
return useRequestBuilder(NoteCollection, sub);
return useRequestBuilder(sub);
}

View File

@ -1,4 +1,4 @@
import { EventKind, HexKey, NoteCollection, ReplaceableNoteStore, RequestBuilder } from "@snort/system";
import { EventKind, HexKey, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -17,12 +17,12 @@ export default function useProfileBadges(pubkey?: HexKey) {
return b;
}, [pubkey]);
const profileBadges = useRequestBuilder(ReplaceableNoteStore, sub);
const profileBadges = useRequestBuilder(sub);
const profile = useMemo(() => {
if (profileBadges.data) {
return chunks(
profileBadges.data.tags.filter(t => t[0] === "a" || t[0] === "e"),
profileBadges.data[0].tags.filter(t => t[0] === "a" || t[0] === "e"),
2,
).reduce((acc, [a, e]) => {
return {
@ -57,7 +57,7 @@ export default function useProfileBadges(pubkey?: HexKey) {
return b;
}, [profile, ds]);
const awards = useRequestBuilder(NoteCollection, awardsSub);
const awards = useRequestBuilder(awardsSub);
const result = useMemo(() => {
if (awards.data) {

View File

@ -1,4 +1,4 @@
import { EventKind, HexKey, NoteCollection, RequestBuilder, socialGraphInstance } from "@snort/system";
import { EventKind, HexKey, RequestBuilder, socialGraphInstance } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -10,7 +10,7 @@ export default function useFollowersFeed(pubkey?: HexKey) {
return b;
}, [pubkey]);
const followersFeed = useRequestBuilder(NoteCollection, sub);
const followersFeed = useRequestBuilder(sub);
const followers = useMemo(() => {
const contactLists = followersFeed.data?.filter(

View File

@ -1,4 +1,4 @@
import { EventKind, HexKey, NoteCollection, RequestBuilder, TaggedNostrEvent } from "@snort/system";
import { EventKind, HexKey, RequestBuilder, TaggedNostrEvent } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -15,7 +15,7 @@ export default function useFollowsFeed(pubkey?: HexKey) {
return b;
}, [isMe, pubkey]);
const contactFeed = useRequestBuilder(NoteCollection, sub);
const contactFeed = useRequestBuilder(sub);
return useMemo(() => {
if (isMe) {
return follows.item;

View File

@ -1,5 +1,5 @@
import { unixNow } from "@snort/shared";
import { EventKind, NoteCollection, RequestBuilder } from "@snort/system";
import { EventKind, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -18,7 +18,7 @@ export default function useHashtagsFeed() {
}, [hashtags]);
return {
data: useRequestBuilder(NoteCollection, sub),
data: useRequestBuilder(sub),
hashtags,
};
}

View File

@ -1,9 +1,9 @@
import { EventKind, NostrLink, NoteCollection, parseRelayTags, RequestBuilder, TaggedNostrEvent } from "@snort/system";
import { EventKind, NostrLink, parseRelayTags, RequestBuilder, TaggedNostrEvent } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { usePrevious } from "@uidotdev/usehooks";
import { useEffect, useMemo } from "react";
import { FollowLists, FollowsFeed, GiftsCache, Notifications, UserRelays } from "@/Cache";
import { FollowLists, FollowsFeed, GiftsCache, Notifications } from "@/Cache";
import { Nip4Chats, Nip28Chats } from "@/chat";
import { Nip28ChatSystem } from "@/chat/nip28";
import useEventPublisher from "@/Hooks/useEventPublisher";
@ -96,7 +96,7 @@ export default function useLoginFeed() {
return b;
}, [login]);
const loginFeed = useRequestBuilder(NoteCollection, subLogin);
const loginFeed = useRequestBuilder(subLogin);
// update relays and follow lists
useEffect(() => {
@ -219,7 +219,6 @@ export default function useLoginFeed() {
}, [loginFeed]);
useEffect(() => {
UserRelays.buffer(follows.item).catch(console.error);
system.ProfileLoader.TrackKeys(follows.item); // always track follows profiles
system.profileLoader.TrackKeys(follows.item); // always track follows profiles
}, [follows.item]);
}

View File

@ -1,4 +1,4 @@
import { EventKind, HexKey, parseRelayTags, ReplaceableNoteStore, RequestBuilder } from "@snort/system";
import { EventKind, HexKey, parseRelayTags, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -10,6 +10,6 @@ export default function useRelaysFeed(pubkey?: HexKey) {
return b;
}, [pubkey]);
const relays = useRequestBuilder(ReplaceableNoteStore, sub);
return parseRelayTags(relays.data?.tags.filter(a => a[0] === "r") ?? []);
const relays = useRequestBuilder(sub);
return parseRelayTags(relays.data?.[0].tags.filter(a => a[0] === "r") ?? []);
}

View File

@ -1,5 +1,5 @@
import { unixNow } from "@snort/shared";
import { EventKind, NoteCollection, RequestBuilder } from "@snort/system";
import { EventKind, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -18,7 +18,7 @@ export function useStatusFeed(id?: string, leaveOpen = false) {
return rb;
}, [id]);
const status = useRequestBuilder(NoteCollection, sub);
const status = useRequestBuilder(sub);
const statusFiltered = status.data?.filter(a => {
const exp = Number(findTag(a, "expiration"));

View File

@ -1,4 +1,4 @@
import { EventExt, EventKind, NostrLink, NoteCollection, RequestBuilder } from "@snort/system";
import { EventExt, EventKind, NostrLink, RequestBuilder } from "@snort/system";
import { useReactions, useRequestBuilder } from "@snort/system-react";
import { useEffect, useMemo, useState } from "react";
@ -30,7 +30,7 @@ export default function useThreadFeed(link: NostrLink) {
return sub;
}, [allEvents.length]);
const store = useRequestBuilder(NoteCollection, sub);
const store = useRequestBuilder(sub);
useEffect(() => {
if (store.data) {

View File

@ -1,5 +1,5 @@
import { unixNow } from "@snort/shared";
import { EventKind, NoteCollection, RequestBuilder } from "@snort/system";
import { EventKind, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useCallback, useMemo } from "react";
@ -116,7 +116,7 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
return rb?.builder ?? null;
}, [until, since, options.method, pref, createBuilder]);
const main = useRequestBuilder(NoteCollection, sub);
const main = useRequestBuilder(sub);
const subRealtime = useMemo(() => {
const rb = createBuilder();
@ -130,7 +130,7 @@ export default function useTimelineFeed(subject: TimelineSubject, options: Timel
return rb?.builder ?? null;
}, [pref.autoShowLatest, createBuilder]);
const latest = useRequestBuilder(NoteCollection, subRealtime);
const latest = useRequestBuilder(subRealtime);
return {
main: main.data,

View File

@ -1,4 +1,4 @@
import { EventKind, NostrLink, NoteCollection, parseZap, RequestBuilder } from "@snort/system";
import { EventKind, NostrLink, parseZap, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
@ -10,7 +10,7 @@ export default function useZapsFeed(link?: NostrLink) {
return b;
}, [link]);
const zapsFeed = useRequestBuilder(NoteCollection, sub);
const zapsFeed = useRequestBuilder(sub);
const zaps = useMemo(() => {
if (zapsFeed.data) {

View File

@ -146,7 +146,7 @@ const NetworkGraph = () => {
const node = {
id: UID,
address: pubkey,
profile: system.ProfileLoader.Cache.getFromCache(pubkey),
profile: system.profileLoader.cache.getFromCache(pubkey),
distance,
inboundCount,
outboundCount,

View File

@ -23,7 +23,7 @@ export function FollowsRelayHealth({
const uniqueFollows = dedupe(follows.item);
const hasRelays = useMemo(() => {
return uniqueFollows.filter(a => (system.RelayCache.getFromCache(a)?.relays.length ?? 0) > 0);
return uniqueFollows.filter(a => (system.relayCache.getFromCache(a)?.relays.length ?? 0) > 0);
}, [uniqueFollows]);
const missingRelays = useMemo(() => {
@ -31,7 +31,7 @@ export function FollowsRelayHealth({
}, [hasRelays]);
const topWriteRelays = useMemo(() => {
return pickTopRelays(system.RelayCache, uniqueFollows, 1e31, "write");
return pickTopRelays(system.relayCache, uniqueFollows, 1e31, "write");
}, [uniqueFollows]);
return (

View File

@ -12,7 +12,7 @@ export class Nip24ChatSystem extends ExternalStore<Array<Chat>> implements ChatS
constructor(cache: GiftWrapCache) {
super();
this.#cache = cache;
this.#cache.hook(() => this.notifyChange(), "*");
this.#cache.on("change", () => this.notifyChange());
}
subscription() {

View File

@ -19,7 +19,7 @@ import { LoginSession } from "@/Utils/Login";
export class Nip28ChatSystem extends ExternalStore<Array<Chat>> implements ChatSystem {
#cache: FeedCache<NostrEvent>;
#log = debug("NIP-04");
#log = debug("NIP-28");
readonly ChannelKinds = [
EventKind.PublicChatChannel,
EventKind.PublicChatMessage,

View File

@ -30,21 +30,20 @@ System.on("event", (_, ev) => {
socialGraphInstance.handleEvent(ev);
});
System.profileCache.on("change", keys => {
const changed = removeUndefined(keys.map(a => System.profileCache.getFromCache(a)));
changed.forEach(addCachedMetadataToFuzzySearch);
});
/**
* Add profile loader fn
*/
if (CONFIG.httpCache) {
System.ProfileLoader.loaderFn = async (keys: Array<string>) => {
System.profileLoader.loaderFn = async (keys: Array<string>) => {
return removeUndefined(await Promise.all(keys.map(a => fetchProfile(a))));
};
}
setTimeout(() => {
System.UserProfileCache.snapshot().forEach(a => {
addCachedMetadataToFuzzySearch(a);
});
}, 2000);
export async function fetchProfile(key: string) {
try {
throwIfOffline();