mirror of
https://github.com/luminous-devs/lume.git
synced 2024-09-18 03:03:31 +00:00
add ndk cache
This commit is contained in:
parent
996ba3f82d
commit
c80d554630
@ -37,6 +37,7 @@
|
||||
"@tauri-apps/plugin-os": "github:tauri-apps/tauri-plugin-os#v2",
|
||||
"@tauri-apps/plugin-process": "github:tauri-apps/tauri-plugin-process#v2",
|
||||
"@tauri-apps/plugin-sql": "github:tauri-apps/tauri-plugin-sql#v2",
|
||||
"@tauri-apps/plugin-store": "github:tauri-apps/tauri-plugin-store#v2",
|
||||
"@tauri-apps/plugin-stronghold": "github:tauri-apps/tauri-plugin-stronghold#v2",
|
||||
"@tauri-apps/plugin-upload": "github:tauri-apps/tauri-plugin-upload#v2",
|
||||
"@tauri-apps/plugin-window": "2.0.0-alpha.0",
|
||||
|
@ -61,6 +61,9 @@ dependencies:
|
||||
'@tauri-apps/plugin-sql':
|
||||
specifier: github:tauri-apps/tauri-plugin-sql#v2
|
||||
version: github.com/tauri-apps/tauri-plugin-sql/0591e9f63e0f86be79c209f4bcb564ccce4bd05c
|
||||
'@tauri-apps/plugin-store':
|
||||
specifier: github:tauri-apps/tauri-plugin-store#v2
|
||||
version: github.com/tauri-apps/tauri-plugin-store/2f5e470ba6d746b54da46fcb3eb10325fa224351
|
||||
'@tauri-apps/plugin-stronghold':
|
||||
specifier: github:tauri-apps/tauri-plugin-stronghold#v2
|
||||
version: github.com/tauri-apps/tauri-plugin-stronghold/a87861766f3520b5fc1ea1f34665d814bbee9b06
|
||||
@ -7755,6 +7758,14 @@ packages:
|
||||
'@tauri-apps/api': 2.0.0-alpha.5
|
||||
dev: false
|
||||
|
||||
github.com/tauri-apps/tauri-plugin-store/2f5e470ba6d746b54da46fcb3eb10325fa224351:
|
||||
resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-store/tar.gz/2f5e470ba6d746b54da46fcb3eb10325fa224351}
|
||||
name: '@tauri-apps/plugin-store'
|
||||
version: 2.0.0-alpha.0
|
||||
dependencies:
|
||||
'@tauri-apps/api': 2.0.0-alpha.5
|
||||
dev: false
|
||||
|
||||
github.com/tauri-apps/tauri-plugin-stronghold/a87861766f3520b5fc1ea1f34665d814bbee9b06:
|
||||
resolution: {tarball: https://codeload.github.com/tauri-apps/tauri-plugin-stronghold/tar.gz/a87861766f3520b5fc1ea1f34665d814bbee9b06}
|
||||
name: '@tauri-apps/plugin-stronghold'
|
||||
|
13
src-tauri/Cargo.lock
generated
13
src-tauri/Cargo.lock
generated
@ -2582,6 +2582,7 @@ dependencies = [
|
||||
"tauri-plugin-process",
|
||||
"tauri-plugin-single-instance",
|
||||
"tauri-plugin-sql",
|
||||
"tauri-plugin-store",
|
||||
"tauri-plugin-stronghold",
|
||||
"tauri-plugin-updater",
|
||||
"tauri-plugin-upload",
|
||||
@ -4981,6 +4982,18 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-store"
|
||||
version = "2.0.0-alpha.0"
|
||||
source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v2#6f01bc11ab5be762d6cbe0ca924f15cdde47ce0d"
|
||||
dependencies = [
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-stronghold"
|
||||
version = "2.0.0-alpha.0"
|
||||
|
@ -31,6 +31,7 @@ tauri-plugin-app = { git = "https://github.com/tauri-apps/plugins-workspace", br
|
||||
tauri-plugin-process = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
|
||||
tauri-plugin-os = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
|
||||
tauri-plugin-window = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
|
||||
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
|
||||
sqlx-cli = { version = "0.7.0", default-features = false, features = [
|
||||
"sqlite",
|
||||
] }
|
||||
|
56
src/libs/ndk/cache.tsx
Normal file
56
src/libs/ndk/cache.tsx
Normal file
@ -0,0 +1,56 @@
|
||||
import { NDKCacheAdapter } from '@nostr-dev-kit/ndk';
|
||||
import { NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk';
|
||||
import { Store } from '@tauri-apps/plugin-store';
|
||||
|
||||
export default class TauriAdapter implements NDKCacheAdapter {
|
||||
public store: Store;
|
||||
readonly locking: boolean;
|
||||
|
||||
constructor() {
|
||||
this.store = new Store('.ndkcache.dat');
|
||||
this.locking = true;
|
||||
}
|
||||
|
||||
public async query(subscription: NDKSubscription): Promise<void> {
|
||||
const { filter } = subscription;
|
||||
|
||||
// if this filter uses both authors and kinds, then we need to query for each combination of author and kind
|
||||
// and then combine the results
|
||||
if (filter.authors && filter.kinds) {
|
||||
const promises = [];
|
||||
|
||||
for (const author of filter.authors) {
|
||||
for (const kind of filter.kinds) {
|
||||
const key = `${author}:${kind}`;
|
||||
promises.push(this.store.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
|
||||
for (const result of results) {
|
||||
if (result) {
|
||||
const event = await this.store.get(result as string);
|
||||
|
||||
if (event) {
|
||||
const ndkEvent = new NDKEvent(subscription.ndk, JSON.parse(event as string));
|
||||
subscription.eventReceived(ndkEvent, undefined, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async setEvent(event: NDKEvent): Promise<void> {
|
||||
const nostrEvent = await event.toNostrEvent();
|
||||
const key = `${nostrEvent.pubkey}:${nostrEvent.kind}`;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
Promise.all([
|
||||
this.store.set(event.id, JSON.stringify(nostrEvent)),
|
||||
this.store.set(key, event.id),
|
||||
this.store.save(),
|
||||
]).then(() => resolve());
|
||||
});
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import { ndkAdapter } from '@nostr-fetch/adapter-ndk';
|
||||
import { NostrFetcher, normalizeRelayUrlSet } from 'nostr-fetch';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import TauriAdapter from '@libs/ndk/cache';
|
||||
import { getSetting } from '@libs/storage';
|
||||
|
||||
const setting = await getSetting('relays');
|
||||
@ -19,7 +20,8 @@ export const NDKInstance = () => {
|
||||
}, []);
|
||||
|
||||
async function loadNdk(explicitRelayUrls: string[]) {
|
||||
const ndkInstance = new NDK({ explicitRelayUrls });
|
||||
const cacheAdapter = new TauriAdapter();
|
||||
const ndkInstance = new NDK({ explicitRelayUrls, cacheAdapter });
|
||||
|
||||
try {
|
||||
await ndkInstance.connect();
|
||||
|
Loading…
Reference in New Issue
Block a user