From 4e506bc7dbeb4468bc952a3903db7a075e069168 Mon Sep 17 00:00:00 2001 From: scobru Date: Fri, 11 Oct 2024 00:04:35 +0200 Subject: [PATCH] hooks --- packages/svelte/gun.config.json | 3 - packages/svelte/src/lib/hooks/useGun.ts | 94 ----------------- packages/svelte/src/lib/hooks/useRelay.ts | 119 ---------------------- packages/svelte/src/lib/stores.ts | 21 +--- 4 files changed, 2 insertions(+), 235 deletions(-) delete mode 100644 packages/svelte/gun.config.json delete mode 100644 packages/svelte/src/lib/hooks/useGun.ts delete mode 100644 packages/svelte/src/lib/hooks/useRelay.ts diff --git a/packages/svelte/gun.config.json b/packages/svelte/gun.config.json deleted file mode 100644 index 34e4c7e..0000000 --- a/packages/svelte/gun.config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "peers": ["https://peer.wallie.io/gun", "https://gun-relay.scobrudot.dev/gun"] -} diff --git a/packages/svelte/src/lib/hooks/useGun.ts b/packages/svelte/src/lib/hooks/useGun.ts deleted file mode 100644 index da5e010..0000000 --- a/packages/svelte/src/lib/hooks/useGun.ts +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Gun DB initialization and basic methods - * @module Gun - * @group Database - */ - -import Gun from "gun/gun"; -import "gun/lib/then"; -import "gun/lib/radix"; -import "gun/lib/radisk"; -import "gun/lib/store"; -import "gun/lib/rindexed"; -import "gun/lib/webrtc"; - -import { relay } from "./useRelay"; - - -// https://github.com/amark/gun/wiki/volunteer.dht -// https://github.com/draeder/gun-relays - -/** @type {import('gun').IGunInstance} The main Gun instance for database operations */ -let gun; - -/** - * Instantiate a Gun instance for DB manipulations - * @param {import('gun').GunOptions} [options={ localStorage: false }] - Gun options - * @returns {import('gun').IGunInstance} - * @example - * import { useGun } from '@gun-vue/composables' - * const gun = useGun() - */ -export function useGun(options = { localStorage: false }) { - if (!gun) { - const opts = { peers: [relay.peer] }; - if (typeof options === "object") { - Object.assign(opts, options); - } - console.log(opts.peers); - gun = Gun(opts); - } - return gun; -} - -/** - * @param {...string} args - * @returns {import('gun').IGunInstance} - */ -export function useGunPath(...args) { - const gun = useGun(); - let g; - for (let arg of args) { - g = gun.get(arg); - } - return g || gun; -} - -/** - * @param {...string} args - */ -export function useGunMap(...args) { - // Implementation not provided in the original code -} - -/** - * get a secondary Gun instance for certificate management - * @param {object} [options={ localStorage: false }] - * @returns {import('gun').IGunInstance} - */ -export function useGunSecondary(options = { localStorage: false }) { - const gun2 = Gun({ peers: [relay.peer], ...options }); - return gun2; -} - -/** - * SEA library - * @constant SEA - */ -export { default as SEA } from "gun/sea.js"; - -/** - * Get a soul for any given node - * A wrapper for `Gun.node.soul` - * @function soul - */ -export const soul = Gun?.node?.soul; - -/** - * Generate a random UUID - * A wrapper for `Gun.text.random` - * @function genUUID - * @param {number} [num] - * @returns {string} - */ -export const genUUID = Gun?.text?.random; \ No newline at end of file diff --git a/packages/svelte/src/lib/hooks/useRelay.ts b/packages/svelte/src/lib/hooks/useRelay.ts deleted file mode 100644 index 44f15c3..0000000 --- a/packages/svelte/src/lib/hooks/useRelay.ts +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Relay connection management - * @module Relay - * @group Database - */ - -import { useGun } from "./useGun"; -import { derived, writable, get } from "svelte/store"; -import ms from "ms"; - -import config from "../../../gun.config.json"; -import { browser } from "$app/environment"; - -const defaultPeer = config.relay; - -/** - * Peer server status reactive object - * @typedef {Object} Relay - * @property {string} peer - * @property {string} hostname - * @property {string} status - * @property {number} pulse - * @property {number} lag - * @property {number} started - * @property {number} diff - * @property {string} age - * @property {boolean} blink - */ - -function createRelay() { - if (!browser) return - - const { subscribe, set, update } = writable({ - list: [], - peer: localStorage.getItem("peer") || defaultPeer, - hostname: "", - status: "offline", - started: 0, - pulse: 0, - lag: 0, - diff: 0, - age: "", - delay: 0, - blink: false - }); - - return { - subscribe, - set, - update, - setPeer: (url) => { - update(r => { - r.peer = url; - localStorage.setItem("peer", url); - return r; - }); - setTimeout(() => { - window.location.reload(); - }, 700); - }, - resetPeer: () => { - update(r => { - r.peer = defaultPeer; - localStorage.setItem("peer", defaultPeer); - return r; - }); - setTimeout(() => { - window.location.reload(); - }, 700); - } - }; -} - -export const relay = createRelay(); - -// Derived stores -export const hostname = derived(relay, $relay => new URL($relay.peer)?.hostname || ""); -export const diff = derived(relay, $relay => $relay.pulse - $relay.started); -export const age = derived(diff, $diff => ms($diff)); -export const delay = derived(relay, $relay => Date.now() - $relay.pulse); - -// Watch for pulse changes -let prevPulse = 0; -relay.subscribe($relay => { - if (prevPulse !== 0 && $relay.pulse !== prevPulse) { - relay.update(r => { - r.blink = !r.blink; - r.lag = $relay.pulse - prevPulse - 500; - return r; - }); - } - prevPulse = $relay.pulse; -}); - -/** - * Peer server status monitor - * @returns {{relay: Writable, setPeer: (url: string) => void, resetPeer: () => void}} - */ -export function useRelay() { - const gun = useGun(); - const $relay = get(relay); - if ($relay.pulse === 0 && get(hostname)) { - gun - .get(get(hostname)) - .map() - .on((d, k) => { - try { - relay.update(r => { - r[k] = d; - return r; - }); - } catch (e) { - console.log(e); - } - }); - } - - return { relay, setPeer: relay.setPeer, resetPeer: relay.resetPeer }; -} \ No newline at end of file diff --git a/packages/svelte/src/lib/stores.ts b/packages/svelte/src/lib/stores.ts index 570a975..77738e2 100644 --- a/packages/svelte/src/lib/stores.ts +++ b/packages/svelte/src/lib/stores.ts @@ -1,27 +1,10 @@ import type { IGunInstance } from "gun"; import { writable, type Writable } from "svelte/store"; -import { browser } from '$app/environment'; -import Gun from 'gun'; -import "gun-eth"; -import { useGun } from "./hooks/useGun"; +import { useGun } from "./gun"; export const currentUser = writable(null); export const gun = writable(null) as unknown as Writable>; -let gunInstance = useGun(); +gun.set(useGun()); -gun.set(gunInstance); -Gun.on('opt', function (ctx) { - if (ctx.once) { - return - } - ctx.on('out', function (msg) { - var to = this.to - // Adds headers for put - msg.headers = { - token: 'test' - } - to.next(msg) // pass to next middleware - }) -})