Skip to content

Commit

Permalink
safe relay info cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Feb 12, 2025
1 parent 34c936f commit aef779e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@chakra-ui/shared-utils": "^2.0.4",
"@chakra-ui/styled-system": "^2.12.0",
"@chakra-ui/theme-tools": "^2.2.6",
"@codemirror/autocomplete": "^6.18.4",
"@codemirror/autocomplete": "^6.18.5",
"@codemirror/lang-json": "^6.0.1",
"@codemirror/language": "^6.10.8",
"@codemirror/view": "^6.36.2",
Expand Down
102 changes: 51 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/components/navigation/app-favorite-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from "react";
import { IconButton, IconButtonProps } from "@chakra-ui/react";
import { kinds } from "nostr-tools";
import { useEventFactory } from "applesauce-react/hooks";
import { NameValueTag } from "applesauce-core/helpers";
import { removeNameValueTag, addNameValueTag } from "applesauce-factory/operations";

import { App, defaultUserFavoriteApps } from "./apps";
Expand All @@ -26,7 +27,7 @@ export default function AppFavoriteButton({
};

setLoading(true);
const tag = ["app", app.id];
const tag: NameValueTag = ["app", app.id];
const draft = await factory.modifyList(prev, isFavorite ? removeNameValueTag(tag) : addNameValueTag(tag));
await publish(isFavorite ? "Unfavorite app" : "Favorite app", draft);
setLoading(false);
Expand Down
2 changes: 1 addition & 1 deletion src/services/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function filterEvents(events: CategorizedEvent[], pubkey: string, mute?: Mutes):
if (!refs.reply?.e?.id) return false;
if (refs.reply?.e?.author && refs.reply?.e?.author !== pubkey) return false;
const parent = eventStore.getEvent(refs.reply.e.id);
if (!parent || parent.pubkey !== pubkey) return false;
if (parent?.pubkey !== pubkey) return false;
break;
case NotificationType.Mention:
break;
Expand Down
17 changes: 17 additions & 0 deletions src/services/relay-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ db.transaction("relayInfo", "readonly")
log(`Loaded ${loaded} relay info`);
});

async function saveInfo() {
log("Saving relay info");
const cache = Reflect.get(Nip11Registry, "cache") as Map<string, any>;

const tx = db.transaction("relayInfo", "readwrite");
await Promise.all(
Array.from(cache.entries())
.filter(([url, info]) => Object.keys(info).length > 0)
.map(([url, info]) => tx.store.put(info, url)),
);
await tx.done;
}

async function getInfo(relay: string | AbstractRelay, alwaysFetch = false) {
relay = typeof relay === "string" ? relay : relay.url;

Expand All @@ -35,6 +48,10 @@ async function getInfo(relay: string | AbstractRelay, alwaysFetch = false) {
return info;
}

setInterval(() => {
saveInfo();
}, 10_000);

export const relayInfoService = { getInfo };

if (import.meta.env.DEV) {
Expand Down

0 comments on commit aef779e

Please sign in to comment.