diff --git a/package-lock.json b/package-lock.json index 8e52588..23e7c1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,10 @@ "": { "workspaces": [ "packages/*" - ] + ], + "dependencies": { + "@types/throttle-debounce": "^5.0.2" + } }, "node_modules/@ampproject/remapping": { "version": "2.3.0", @@ -402,6 +405,8 @@ }, "node_modules/@types/throttle-debounce": { "version": "5.0.2", + "resolved": "https://registry.npmjs.org/@types/throttle-debounce/-/throttle-debounce-5.0.2.tgz", + "integrity": "sha512-pDzSNulqooSKvSNcksnV72nk8p7gRqN8As71Sp28nov1IgmPKWbOEIwAWvBME5pPTtaXJAvG3O4oc76HlQ4kqQ==", "license": "MIT" }, "node_modules/@typescript-eslint/eslint-plugin": { @@ -3640,6 +3645,7 @@ "version": "0.0.32", "license": "MIT", "dependencies": { + "@types/throttle-debounce": "^5.0.2", "@welshman/dvm": "~0.0.10", "@welshman/feeds": "~0.0.26", "@welshman/lib": "~0.0.28", @@ -3710,9 +3716,7 @@ "dependencies": { "@scure/base": "^1.1.6", "@types/events": "^3.0.3", - "@types/throttle-debounce": "^5.0.2", - "events": "^3.3.0", - "throttle-debounce": "^5.0.0" + "events": "^3.3.0" }, "devDependencies": { "gts": "^5.0.1", diff --git a/package.json b/package.json index 01ab2dd..437a32d 100644 --- a/package.json +++ b/package.json @@ -6,5 +6,8 @@ "repository": { "type": "git", "url": "https://github.com/coracle-social/skiff.git" + }, + "dependencies": { + "@types/throttle-debounce": "^5.0.2" } } diff --git a/packages/app/package.json b/packages/app/package.json index 6392ee6..608533a 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -31,9 +31,10 @@ "typescript": "~5.1.6" }, "dependencies": { - "@welshman/lib": "~0.0.28", - "@welshman/feeds": "~0.0.26", + "@types/throttle-debounce": "^5.0.2", "@welshman/dvm": "~0.0.10", + "@welshman/feeds": "~0.0.26", + "@welshman/lib": "~0.0.28", "@welshman/net": "~0.0.40", "@welshman/signer": "~0.0.16", "@welshman/store": "~0.0.12", diff --git a/packages/app/src/core.ts b/packages/app/src/core.ts index bac6ce7..a3eca00 100644 --- a/packages/app/src/core.ts +++ b/packages/app/src/core.ts @@ -1,4 +1,4 @@ -import {throttle} from 'throttle-debounce' +import {throttle} from '@welshman/lib' import {Repository, Relay} from "@welshman/util" import type {TrustedEvent} from "@welshman/util" import {Tracker} from "@welshman/net" diff --git a/packages/app/src/plaintext.ts b/packages/app/src/plaintext.ts index 9c05cd9..4239b6a 100644 --- a/packages/app/src/plaintext.ts +++ b/packages/app/src/plaintext.ts @@ -14,22 +14,26 @@ export const setPlaintext = (e: TrustedEvent, content: string) => export const ensurePlaintext = async (e: TrustedEvent) => { if (e.content && !getPlaintext(e)) { - const $signer = getSigner(getSession(e.pubkey)) + const $session = getSession(e.pubkey) - if ($signer) { - let result + if (!$session) return - try { - result = await decrypt($signer, e.pubkey, e.content) - } catch (e: any) { - if (!String(e).match(/invalid base64/)) { - throw e - } + const $signer = getSigner($session) + + if (!$signer) return + + let result + + try { + result = await decrypt($signer, e.pubkey, e.content) + } catch (e: any) { + if (!String(e).match(/invalid base64/)) { + throw e } + } - if (result) { - setPlaintext(e, result) - } + if (result) { + setPlaintext(e, result) } } diff --git a/packages/app/src/storage.ts b/packages/app/src/storage.ts index 1da85cd..bdb2dc8 100644 --- a/packages/app/src/storage.ts +++ b/packages/app/src/storage.ts @@ -1,9 +1,8 @@ import {openDB, deleteDB} from "idb" import type {IDBPDatabase} from "idb" -import {throttle} from "throttle-debounce" import {writable} from "svelte/store" import type {Unsubscriber, Writable} from "svelte/store" -import {indexBy, fromPairs} from "@welshman/lib" +import {indexBy, throttle, fromPairs} from "@welshman/lib" import type {TrustedEvent, Repository} from "@welshman/util" import type {Tracker} from "@welshman/net" import {withGetter, adapter, throttled, custom} from "@welshman/store" diff --git a/packages/app/src/topics.ts b/packages/app/src/topics.ts index 31ae73a..3cf5120 100644 --- a/packages/app/src/topics.ts +++ b/packages/app/src/topics.ts @@ -1,5 +1,4 @@ -import {throttle} from 'throttle-debounce' -import {inc} from '@welshman/lib' +import {inc, throttle} from '@welshman/lib' import {custom} from '@welshman/store' import {repository} from './core' diff --git a/packages/app/src/wot.ts b/packages/app/src/wot.ts index 39cb069..56cc1a8 100644 --- a/packages/app/src/wot.ts +++ b/packages/app/src/wot.ts @@ -1,6 +1,5 @@ -import {throttle} from 'throttle-debounce' import {derived, writable} from 'svelte/store' -import {max, addToMapKey, inc, dec} from '@welshman/lib' +import {max, throttle, addToMapKey, inc, dec} from '@welshman/lib' import {getListTags, getPubkeyTagValues} from '@welshman/util' import {throttled, withGetter} from '@welshman/store' import {pubkey} from './session' diff --git a/packages/lib/package.json b/packages/lib/package.json index 6d93724..a1ab616 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -32,9 +32,7 @@ }, "dependencies": { "@types/events": "^3.0.3", - "@types/throttle-debounce": "^5.0.2", "@scure/base": "^1.1.6", - "events": "^3.3.0", - "throttle-debounce": "^5.0.0" + "events": "^3.3.0" } } diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 10d8f0d..26f634e 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -1,4 +1,3 @@ -import {throttle} from 'throttle-debounce' import {bech32, utf8} from "@scure/base" // Dealing with nil @@ -298,7 +297,7 @@ export const choice = (xs: T[]): T => xs[Math.floor(xs.length * Math.random() export const shuffle = (xs: Iterable): T[] => Array.from(xs).sort(() => Math.random() > 0.5 ? 1 : -1) -export const sample = (n: number, xs: T[]) => shuffle(xs).slice(0, n) +export const samplo = (n: number, xs: T[]) => shuffle(xs).slice(0, n) export const isIterable = (x: any) => Symbol.iterator in Object(x) @@ -459,6 +458,36 @@ export const memoize = (f: (...args: any[]) => T) => { } } +export const throttle = any>(ms: number, f: F) => { + if (ms === 0) { + return f + } + + let args: Parameters + let paused = false + let trailing = false + + const unpause = () => { + if (trailing) { + f(...args) + trailing = false + } + + paused = false + } + + return (...thisArgs: Parameters) => { + if (!paused) { + f(...thisArgs) + paused = true + args = thisArgs + setTimeout(unpause, ms) + } else { + trailing = true + } + } +} + export const throttleWithValue = (ms: number, f: () => T) => { let value: T diff --git a/packages/signer/src/signers/nip46.ts b/packages/signer/src/signers/nip46.ts index 0065edd..d2fe4fe 100644 --- a/packages/signer/src/signers/nip46.ts +++ b/packages/signer/src/signers/nip46.ts @@ -1,5 +1,4 @@ -import {throttle} from 'throttle-debounce' -import {Emitter, makePromise, defer, sleep, tryCatch, randomId, equals} from "@welshman/lib" +import {Emitter, throttle, makePromise, defer, sleep, tryCatch, randomId, equals} from "@welshman/lib" import {createEvent, normalizeRelayUrl, TrustedEvent, StampedEvent, NOSTR_CONNECT} from "@welshman/util" import {subscribe, publish, Subscription, SubscriptionEvent} from "@welshman/net" import {ISigner, decrypt, hash, own} from '../util' diff --git a/packages/store/src/index.ts b/packages/store/src/index.ts index 6ac6449..85c0317 100644 --- a/packages/store/src/index.ts +++ b/packages/store/src/index.ts @@ -1,7 +1,6 @@ -import {throttle} from "throttle-debounce" import {derived, writable} from "svelte/store" import type {Readable, Writable, Subscriber, Unsubscriber} from "svelte/store" -import {identity, ensurePlural, getJson, setJson, batch, partition, first} from "@welshman/lib" +import {identity, throttle, ensurePlural, getJson, setJson, batch, partition, first} from "@welshman/lib" import type {Maybe} from "@welshman/lib" import type {Repository} from "@welshman/util" import {matchFilters, getIdAndAddress, getIdFilters} from "@welshman/util"