commit e057c75ee3af52d1bebb9d833667562b43027742
parent 0bc7ce94c36d6726fb5b75f534f921af62e4aec6
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Thu, 10 Apr 2025 23:47:50 +0000
apps-lib: add nostr poll relays retry handler, add/edit nostr stores
Diffstat:
3 files changed, 61 insertions(+), 31 deletions(-)
diff --git a/apps-lib/src/lib/index.ts b/apps-lib/src/lib/index.ts
@@ -107,6 +107,7 @@ export * from "./util/casl.js"
export * from "./util/component.js"
export * from "./util/idb.js"
export * from "./util/lib.js"
+export * from "./util/nostr/lib.js"
export * from "./util/service/nostr-sync.js"
export * from "./util/view.js"
export { default as Home } from "./view/home.svelte"
diff --git a/apps-lib/src/lib/store/nostr.ts b/apps-lib/src/lib/store/nostr.ts
@@ -1,37 +1,16 @@
-import { get_store } from "$root";
import { writable } from "svelte/store";
-export const nostr_ndk_configured = writable<boolean>(false);
-export const nostr_sync_prevent = writable<boolean>(false);
+export const nostr_ndk_configured = writable(false);
+
+export const nostr_sync_prevent = writable(false);
export const nostr_sync_attempts = writable(0);
export const nostr_sync_attempts_max = writable(8);
-export const nostr_sync_failure = writable(true);
-/*
-export const nostr_relays_poll_documents = writable<boolean>(false);
-export const nostr_relays_poll_documents_count = writable<number>(0);
-export const nostr_relays_connected = writable<string[]>([]);
-*/
+export const nostr_sync_stop = writable(true);
-export const nostr_sync_retry_handler = async (callback: () => Promise<any>) => {
- let current_count = 0;
- const attempts_max = get_store(nostr_sync_attempts_max);
+export const nostr_poll_relays_prevent = writable(false);
+export const nostr_poll_relays_attempts = writable(0);
+export const nostr_poll_relays_attempts_max = writable(0);
+export const nostr_poll_relays_stop = writable(true);
- nostr_sync_attempts.set(0);
- nostr_sync_failure.set(false);
-
- const exe = async () => {
- if (get_store(nostr_sync_failure)) return;
- try {
- await callback();
- } catch (e) {
- current_count += 1;
- nostr_sync_attempts.set(current_count);
- if (current_count < attempts_max) {
- await new Promise((res) => setTimeout(res, 500));
- await exe();
- } else nostr_sync_failure.set(true);
- }
- };
+export const nostr_relays_connected = writable<string[]>([]);
- await exe();
-};
-\ No newline at end of file
diff --git a/apps-lib/src/lib/util/nostr/lib.ts b/apps-lib/src/lib/util/nostr/lib.ts
@@ -0,0 +1,51 @@
+import { get_store, nostr_poll_relays_attempts, nostr_poll_relays_attempts_max, nostr_poll_relays_stop, nostr_sync_attempts, nostr_sync_attempts_max, nostr_sync_stop } from "$root";
+
+export const nostr_sync_retry_handler = async (callback: () => Promise<any>) => {
+ let current_count = 0;
+ const attempts_max = get_store(nostr_sync_attempts_max);
+
+ nostr_sync_attempts.set(0);
+ nostr_sync_stop.set(false);
+
+ const exe = async () => {
+ if (get_store(nostr_sync_stop)) return;
+ console.log(`nostr_sync_retry_handler ${current_count}`)
+ try {
+ await callback();
+ } catch (e) {
+ current_count += 1;
+ nostr_sync_attempts.set(current_count);
+ if (current_count < attempts_max) {
+ await new Promise((res) => setTimeout(res, 500));
+ await exe();
+ } else nostr_sync_stop.set(true);
+ }
+ };
+
+ await exe();
+};
+
+export const nostr_poll_relays_retry_handler = async (callback: () => Promise<any>) => {
+ let current_count = 0;
+ const attempts_max = get_store(nostr_poll_relays_attempts_max);
+
+ nostr_poll_relays_attempts.set(0);
+ nostr_poll_relays_stop.set(false);
+
+ const exe = async () => {
+ if (get_store(nostr_poll_relays_stop)) return;
+ console.log(`nostr_poll_relays_retry_handler ${current_count}`)
+ try {
+ await callback();
+ } catch (e) {
+ current_count += 1;
+ nostr_poll_relays_attempts.set(current_count);
+ if (current_count < attempts_max) {
+ await new Promise((res) => setTimeout(res, 2000));
+ await exe();
+ } else nostr_poll_relays_stop.set(true);
+ }
+ };
+
+ await exe();
+};