feat: use localhost relay over worker-relay
This commit is contained in:
@ -1,14 +1,13 @@
|
||||
import { CachedTable, CacheEvents } from "@snort/shared";
|
||||
import { NostrEvent } from "@snort/system";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
import { CacheRelay, NostrEvent } from "@snort/system";
|
||||
import { EventEmitter } from "eventemitter3";
|
||||
|
||||
export class EventCacheWorker extends EventEmitter<CacheEvents> implements CachedTable<NostrEvent> {
|
||||
#relay: WorkerRelayInterface;
|
||||
#relay: CacheRelay;
|
||||
#keys = new Set<string>();
|
||||
#cache = new Map<string, NostrEvent>();
|
||||
|
||||
constructor(relay: WorkerRelayInterface) {
|
||||
constructor(relay: CacheRelay) {
|
||||
super();
|
||||
this.#relay = relay;
|
||||
}
|
||||
|
@ -1,16 +1,15 @@
|
||||
import { CachedTable, CacheEvents, removeUndefined, unixNowMs, unwrap } from "@snort/shared";
|
||||
import { CachedMetadata, mapEventToProfile, NostrEvent } from "@snort/system";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
import { CachedMetadata, CacheRelay, mapEventToProfile, NostrEvent } from "@snort/system";
|
||||
import debug from "debug";
|
||||
import { EventEmitter } from "eventemitter3";
|
||||
|
||||
export class ProfileCacheRelayWorker extends EventEmitter<CacheEvents> implements CachedTable<CachedMetadata> {
|
||||
#relay: WorkerRelayInterface;
|
||||
#relay: CacheRelay;
|
||||
#keys = new Set<string>();
|
||||
#cache = new Map<string, CachedMetadata>();
|
||||
#log = debug("ProfileCacheRelayWorker");
|
||||
|
||||
constructor(relay: WorkerRelayInterface) {
|
||||
constructor(relay: CacheRelay) {
|
||||
super();
|
||||
this.#relay = relay;
|
||||
}
|
||||
|
@ -1,16 +1,15 @@
|
||||
import { CachedTable, CacheEvents, removeUndefined, unixNowMs, unwrap } from "@snort/shared";
|
||||
import { EventKind, NostrEvent, UsersFollows } from "@snort/system";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
import { CacheRelay, EventKind, NostrEvent, UsersFollows } from "@snort/system";
|
||||
import debug from "debug";
|
||||
import { EventEmitter } from "eventemitter3";
|
||||
|
||||
export class UserFollowsWorker extends EventEmitter<CacheEvents> implements CachedTable<UsersFollows> {
|
||||
#relay: WorkerRelayInterface;
|
||||
#relay: CacheRelay;
|
||||
#keys = new Set<string>();
|
||||
#cache = new Map<string, UsersFollows>();
|
||||
#log = debug("UserFollowsWorker");
|
||||
|
||||
constructor(relay: WorkerRelayInterface) {
|
||||
constructor(relay: CacheRelay) {
|
||||
super();
|
||||
this.#relay = relay;
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { RelayMetricCache, UserRelaysCache } from "@snort/system";
|
||||
import { CacheRelay, Connection, ConnectionCacheRelay, RelayMetricCache, UserRelaysCache } from "@snort/system";
|
||||
import { SnortSystemDb } from "@snort/system-web";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
import WorkerVite from "@snort/worker-relay/src/worker?worker";
|
||||
@ -8,13 +8,41 @@ import { GiftWrapCache } from "./GiftWrapCache";
|
||||
import { ProfileCacheRelayWorker } from "./ProfileWorkerCache";
|
||||
import { UserFollowsWorker } from "./UserFollowsWorker";
|
||||
|
||||
export const Relay = new WorkerRelayInterface(
|
||||
const cacheRelay = localStorage.getItem("cache-relay");
|
||||
|
||||
const workerRelay = new WorkerRelayInterface(
|
||||
import.meta.env.DEV ? new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url) : new WorkerVite(),
|
||||
);
|
||||
export async function initRelayWorker() {
|
||||
|
||||
export const Relay: CacheRelay = cacheRelay
|
||||
? new ConnectionCacheRelay(new Connection(cacheRelay, { read: true, write: true }))
|
||||
: workerRelay;
|
||||
|
||||
async function tryUseCacheRelay(url: string) {
|
||||
try {
|
||||
await Relay.debug("");
|
||||
await Relay.init({
|
||||
const conn = new Connection(url, { read: true, write: true });
|
||||
await conn.connect(true);
|
||||
localStorage.setItem("cache-relay", url);
|
||||
return conn;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initRelayWorker() {
|
||||
if (!cacheRelay) {
|
||||
let conn = await tryUseCacheRelay("ws://localhost:4869");
|
||||
if (!conn) {
|
||||
conn = await tryUseCacheRelay("ws://umbrel:4848");
|
||||
}
|
||||
if (conn) return;
|
||||
} else if (Relay instanceof ConnectionCacheRelay) {
|
||||
await Relay.connection.connect();
|
||||
}
|
||||
|
||||
try {
|
||||
await workerRelay.debug("");
|
||||
await workerRelay.init({
|
||||
databasePath: "relay.db",
|
||||
insertBatchSize: 100,
|
||||
});
|
||||
|
@ -6,6 +6,7 @@ import { useNavigate } from "react-router-dom";
|
||||
import { GiftsCache, Relay, RelayMetrics } from "@/Cache";
|
||||
import AsyncButton from "@/Components/Button/AsyncButton";
|
||||
import useLogin from "@/Hooks/useLogin";
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
|
||||
export function CacheSettings() {
|
||||
return (
|
||||
@ -56,9 +57,11 @@ function RelayCacheStats() {
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
Relay.summary().then(setCounts);
|
||||
if (login.publicKey) {
|
||||
Relay.count(["REQ", "my", { authors: [login.publicKey] }]).then(setMyEvents);
|
||||
if (Relay instanceof WorkerRelayInterface) {
|
||||
Relay.summary().then(setCounts);
|
||||
if (login.publicKey) {
|
||||
Relay.count(["REQ", "my", { authors: [login.publicKey] }]).then(setMyEvents);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
@ -30,7 +30,6 @@ System.on("auth", async (c, r, cb) => {
|
||||
});
|
||||
|
||||
System.on("event", (_, ev) => {
|
||||
Relay.event(ev);
|
||||
EventsCache.discover(ev);
|
||||
UserCache.discover(ev);
|
||||
addEventToFuzzySearch(ev);
|
||||
|
Reference in New Issue
Block a user