upgrade nostr-idb

This commit is contained in:
hzrd149 2024-01-18 21:30:55 +00:00
parent 99378b6941
commit 089105b39a
7 changed files with 25 additions and 16 deletions

View File

@ -55,7 +55,7 @@
"match-sorter": "^6.3.1",
"nanoid": "^5.0.4",
"ngeohash": "^0.6.3",
"nostr-idb": "^1.1.1",
"nostr-idb": "^2.0.0",
"nostr-tools": "^2.1.3",
"react": "^18.2.0",
"react-chartjs-2": "^5.2.0",

View File

@ -20,8 +20,8 @@ import {
stringifyFilter,
} from "../helpers/nostr/filter";
import { localCacheRelay } from "../services/local-cache-relay";
import { SimpleSubscription } from "nostr-idb";
import { relayRequest } from "../helpers/relay";
import { Subscription } from "nostr-idb";
const BLOCK_SIZE = 100;
@ -116,7 +116,7 @@ export default class TimelineLoader {
name: string;
private log: Debugger;
private subscription: NostrMultiSubscription;
private cacheSubscription?: SimpleSubscription;
private cacheSubscription?: Subscription;
private blockLoaders = new Map<string, RelayBlockLoader>();

View File

@ -1,5 +1,2 @@
import debug from "debug";
export const logger = debug("noStrudel");
debug.enable("noStrudel:*");

View File

@ -1,4 +1,4 @@
import { SimpleRelay, SimpleSubscription, SimpleSubscriptionOptions } from "nostr-idb";
import { SimpleRelay, SubscriptionOptions } from "nostr-idb";
import { Filter } from "nostr-tools";
import { RelayConfig } from "../classes/relay";
@ -106,10 +106,10 @@ export function splitQueryByPubkeys(query: NostrQuery, relayPubkeyMap: Record<st
return filtersByRelay;
}
export function relayRequest(relay: SimpleRelay, filters: Filter[], opts: SimpleSubscriptionOptions = {}) {
export function relayRequest(relay: SimpleRelay, filters: Filter[], opts: SubscriptionOptions = {}) {
return new Promise<NostrEvent[]>((res) => {
const events: NostrEvent[] = [];
const sub: SimpleSubscription = relay.subscribe(filters, {
const sub = relay.subscribe(filters, {
...opts,
onevent: (e) => events.push(e),
oneose: () => {

View File

@ -1,5 +1,5 @@
import { openDB, deleteDB, IDBPDatabase, IDBPTransaction } from "idb";
import { clearDB } from "nostr-idb";
import { clearDB, deleteDB as nostrIDBDelete } from "nostr-idb";
import { SchemaV1, SchemaV2, SchemaV3, SchemaV4, SchemaV5, SchemaV6, SchemaV7, SchemaV8 } from "./schema";
import { logger } from "../../helpers/debug";
@ -203,7 +203,7 @@ export async function deleteDatabase() {
db.close();
log("Deleting");
await deleteDB(dbName);
await deleteDB("events");
await nostrIDBDelete();
window.location.reload();
}

View File

@ -1,4 +1,4 @@
import { CacheRelay, openDB } from "nostr-idb";
import { CacheRelay, openDB, pruneLastUsed } from "nostr-idb";
import { Relay } from "nostr-tools";
import { logger } from "../helpers/debug";
import _throttle from "lodash.throttle";
@ -35,9 +35,16 @@ function createRelay() {
export const localCacheRelay = createRelay();
function pruneLocalDatabase() {
if (localCacheRelay instanceof CacheRelay) {
pruneLastUsed(localCacheRelay.db, 20_000);
}
}
// connect without waiting
localCacheRelay.connect().then(() => {
log("Connected");
pruneLocalDatabase();
});
// keep the relay connection alive
@ -45,6 +52,10 @@ setInterval(() => {
if (!localCacheRelay.connected) localCacheRelay.connect().then(() => log("Reconnected"));
}, 1000 * 5);
setInterval(() => {
pruneLocalDatabase();
}, 1000 * 60);
if (import.meta.env.DEV) {
//@ts-ignore
window.localCacheRelay = localCacheRelay;

View File

@ -5261,11 +5261,12 @@ normalize-package-data@^2.5.0:
semver "2 || 3 || 4 || 5"
validate-npm-package-license "^3.0.1"
nostr-idb@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/nostr-idb/-/nostr-idb-1.1.1.tgz#4ce758965d2fddba62ae17765be2c83aef037c0f"
integrity sha512-t1jDehxfwjygOpLBoXHRoX4O5C4O/8gHP5YfUr68O6+3Ho1bCLDPbWe5L5Vo4qQEsYWOh97bgvkDrq6BJjhvHw==
nostr-idb@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/nostr-idb/-/nostr-idb-2.0.0.tgz#0fdce309613ffb19d12de64d3ffb050e0c7df208"
integrity sha512-qLflqSVaK02ClRXMUdNMOd6DRJZALgoHx+Y8Y+TOnnBc4utq8akPrsdjkCit2+N02PklcJh2kCIZSP9fl7cHJw==
dependencies:
debug "^4.3.4"
idb "^8.0.0"
nostr-tools "^2.1.3"