diff --git a/src/app/shared/Feed.svelte b/src/app/shared/Feed.svelte index 5fb44717..6721ba40 100644 --- a/src/app/shared/Feed.svelte +++ b/src/app/shared/Feed.svelte @@ -2,9 +2,8 @@ import {onMount} from "svelte" import {writable} from "@welshman/lib" import type {Filter} from "@welshman/util" - import {createScroller} from "src/util/misc" + import {createScroller, synced} from "src/util/misc" import {fly, fade} from "src/util/transition" - import {synced} from "src/partials/state" import Anchor from "src/partials/Anchor.svelte" import Spinner from "src/partials/Spinner.svelte" import FlexColumn from "src/partials/FlexColumn.svelte" diff --git a/src/engine/state.ts b/src/engine/state.ts index b5489262..9f0fd38a 100644 --- a/src/engine/state.ts +++ b/src/engine/state.ts @@ -3,7 +3,6 @@ import {nip19} from "nostr-tools" import {throttle} from "throttle-debounce" import { Fetch, - Storage as LocalStorage, createMapOf, defer, displayList, @@ -95,7 +94,7 @@ import { subscribe as baseSubscribe, } from "@welshman/net" import type {Publish, PublishRequest, SubscribeRequest} from "@welshman/net" -import {fuzzy, createBatcher, pushToKey, tryJson, fromCsv} from "src/util/misc" +import {fuzzy, synced, createBatcher, pushToKey, tryJson, fromCsv} from "src/util/misc" import {parseContent} from "src/util/notes" import { appDataKeys, @@ -165,8 +164,9 @@ export const env = new Writable({ SEARCH_RELAYS: fromCsv(import.meta.env.VITE_SEARCH_RELAYS).map(normalizeRelayUrl) as string[], }) -export const pubkey = new Writable(null) -export const sessions = new Writable>({}) +export const pubkey = synced("pubkey", null) +export const sessions = synced>("sessions", {}) + export const relays = new CollectionStore("url") export const groups = new CollectionStore("address") export const groupAdminKeys = new CollectionStore("pubkey") @@ -2045,30 +2045,6 @@ class IndexedDB { } } -type LocalStorageAdapterOpts = { - load: (x: any) => any - dump: (x: any) => any -} - -class LocalStorageAdapter { - constructor( - readonly key: string, - readonly store: IWritable, - readonly opts?: LocalStorageAdapterOpts, - ) {} - - initialize(storage: Storage) { - const {key, store, opts} = this - const {load, dump} = opts || {load: identity, dump: identity} - - if (key in localStorage) { - store.set(load(LocalStorage.getJson(key))) - } - - store.subscribe(throttle(300, $value => LocalStorage.setJson(key, dump($value)))) - } -} - class IndexedDBAdapter { constructor( readonly key: string, @@ -2139,7 +2115,7 @@ class Storage { constructor( readonly version, - readonly adapters: (LocalStorageAdapter | IndexedDBAdapter)[], + readonly adapters: IndexedDBAdapter[], ) { this.initialize() } @@ -2200,12 +2176,6 @@ const sortByPubkeyWhitelist = (fallback: (x: any) => number) => (rows: Record $s.method !== "bunker"), - dump: identity, -} - const scoreEvent = e => { if (getSession(e.pubkey)) return -Infinity if (giftWrapKinds.includes(e.kind)) return -Infinity @@ -2215,8 +2185,6 @@ const scoreEvent = e => { } export const storage = new Storage(12, [ - new LocalStorageAdapter("pubkey", pubkey), - new LocalStorageAdapter("sessions", sessions, sessionsAdapter), new IndexedDBAdapter("seen3", "id", seen, 10000, sortBy(prop("created_at"))), new IndexedDBAdapter( "publishes", diff --git a/src/partials/state.ts b/src/partials/state.ts index 9d126f8f..3c36edc4 100644 --- a/src/partials/state.ts +++ b/src/partials/state.ts @@ -1,16 +1,7 @@ -import {debounce} from "throttle-debounce" import {fromPairs} from "ramda" -import {Storage} from "hurdak" import {writable} from "@welshman/lib" import {parseHex} from "src/util/html" - -export const synced = (key: string, defaultValue: any) => { - const store = writable(Storage.getJson(key) || defaultValue) - - store.subscribe(debounce(1000, $value => Storage.setJson(key, $value))) - - return store -} +import {synced} from "src/util/misc" // Settings diff --git a/src/util/misc.ts b/src/util/misc.ts index 435db549..bd6bedd9 100644 --- a/src/util/misc.ts +++ b/src/util/misc.ts @@ -1,6 +1,7 @@ -import {now, stripProtocol} from "@welshman/lib" +import {throttle} from "throttle-debounce" +import {now, stripProtocol, writable} from "@welshman/lib" import {pluck, fromPairs, last, identity, sum, is, equals} from "ramda" -import {ensurePlural, defer, isPojo, first, seconds, tryFunc, sleep, round} from "hurdak" +import {Storage, ensurePlural, defer, isPojo, first, seconds, tryFunc, sleep, round} from "hurdak" import Fuse from "fuse.js" import logger from "src/util/logger" @@ -362,5 +363,16 @@ export class SearchHelper { export const fromCsv = s => (s || "").split(",").filter(identity) -export const toSpliced = (xs: T[], start: number, deleteCount: number = 0, ...items: T[]) => - [...xs.slice(0, start), ...items, ...xs.slice(start + deleteCount)] +export const toSpliced = (xs: T[], start: number, deleteCount: number = 0, ...items: T[]) => [ + ...xs.slice(0, start), + ...items, + ...xs.slice(start + deleteCount), +] + +export const synced = (key: string, defaultValue: T, delay = 300) => { + const store = writable(Storage.getJson(key) || defaultValue) + + store.subscribe(throttle(delay, ($value: T) => Storage.setJson(key, $value))) + + return store +}