Add store package

This commit is contained in:
Jon Staab
2024-08-01 15:33:37 -07:00
parent cfb0d01316
commit 7bb6d8c54a
10 changed files with 469 additions and 5 deletions
+1 -1
View File
@@ -167,7 +167,7 @@ export const parseAddress = (text: string, context: ParseContext): ParsedAddress
}
export const parseCashu = (text: string, context: ParseContext): ParsedCashu | void => {
const [value] = text.match(/^(cashu)[\d\w=]{50,5000}/i) || []
const [value] = text.match(/^(cashu)[-\d\w=]{50,5000}/i) || []
if (value) {
return {type: ParsedType.Cashu, value, raw: value}
+4
View File
@@ -83,6 +83,10 @@ export const mapVals = <T extends Record<string, any>>(f: (v: any) => any, x: T)
return r as T
}
export const mergeLeft = <T extends Record<string, any>>(a: T, b: T) => ({...b, ...a})
export const mergeRight = <T extends Record<string, any>>(a: T, b: T) => ({...a, ...b})
export const between = (low: number, high: number, n: number) => n > low && n < high
export const randomId = (): string => Math.random().toString().slice(2)
+2
View File
@@ -0,0 +1,2 @@
build
normalize-url
+3
View File
@@ -0,0 +1,3 @@
# @welshman/store [![version](https://badgen.net/npm/v/@welshman/store)](https://npmjs.com/package/@welshman/store)
Utilities for dealing with svelte stores when using welshman.
+36
View File
@@ -0,0 +1,36 @@
{
"name": "@welshman/store",
"version": "0.0.1",
"author": "hodlbod",
"license": "MIT",
"description": "A collection of utilities based on svelte/store for use with welshman",
"publishConfig": {
"access": "public"
},
"type": "module",
"files": [
"build"
],
"types": "./build/src/index.d.ts",
"exports": {
".": {
"types": "./build/src/index.d.ts",
"import": "./build/src/index.mjs",
"require": "./build/src/index.cjs"
}
},
"scripts": {
"pub": "npm run lint && npm run build && npm publish",
"build": "gts clean && tsc-multi",
"lint": "gts lint",
"fix": "gts fix"
},
"devDependencies": {
"gts": "^5.0.1",
"tsc-multi": "^1.1.0",
"typescript": "~5.1.6"
},
"dependencies": {
"svelte": "^4.2.18"
}
}
+199
View File
@@ -0,0 +1,199 @@
import {throttle} from "throttle-debounce"
import {derived} from "svelte/store"
import type {Readable, Writable} from "svelte/store"
import {identity, batch, partition, first} from "@welshman/lib"
import type {Repository} from "@welshman/util"
import {matchFilters, getIdAndAddress, getIdFilters} from "@welshman/util"
import type {Filter, TrustedEvent} from "@welshman/util"
export const getter = <T>(store: Readable<T>) => {
let value: T
store.subscribe((newValue: T) => {
value = newValue
})
return () => value
}
type Stop = () => void
type Sub<T> = (x: T) => void
type Start<T> = (set: Sub<T>) => Stop
export const custom = <T>(start: Start<T>, opts: {throttle?: number} = {}) => {
const subs: Sub<T>[] = []
let value: T
let stop: () => void
return {
subscribe: (sub: Sub<T>) => {
if (opts.throttle) {
sub = throttle(opts.throttle, sub)
}
if (subs.length === 0) {
stop = start((newValue: T) => {
for (const sub of subs) {
sub(newValue)
}
value = newValue
})
}
subs.push(sub)
sub(value)
return () => {
subs.splice(
subs.findIndex(s => s === sub),
1,
)
if (subs.length === 0) {
stop()
}
}
},
}
}
export function withGetter<T>(store: Writable<T>): Writable<T> & {get: () => T}
export function withGetter<T>(store: Readable<T>): Readable<T> & {get: () => T}
export function withGetter<T>(store: Readable<T> | Writable<T>) {
return {...store, get: getter<T>(store)}
}
export const throttled = <T>(delay: number, store: Readable<T>) =>
custom(set => store.subscribe(throttle(delay, set)))
export const createEventStore = (repository: Repository) => {
let subs: Sub<TrustedEvent[]>[] = []
const onUpdate = throttle(300, () => {
const $events = repository.dump()
for (const sub of subs) {
sub($events)
}
})
return {
get: () => repository.dump(),
set: (events: TrustedEvent[]) => repository.load(events),
subscribe: (f: Sub<TrustedEvent[]>) => {
f(repository.dump())
subs.push(f)
if (subs.length === 1) {
repository.on("update", onUpdate)
}
return () => {
subs = subs.filter(x => x !== f)
if (subs.length === 0) {
repository.off("update", onUpdate)
}
}
},
}
}
export const deriveEventsMapped = <T>({
filters,
repository,
eventToItem,
itemToEvent,
includeDeleted = false,
}: {
filters: Filter[]
repository: Repository,
eventToItem: (event: TrustedEvent) => T
itemToEvent: (item: T) => TrustedEvent
includeDeleted?: boolean
}) =>
custom<T[]>(setter => {
let data = repository.query(filters, {includeDeleted}).map(eventToItem).filter(identity)
setter(data)
const onUpdate = batch(300, (updates: {added: TrustedEvent[]; removed: Set<string>}[]) => {
const removed = new Set()
const added = new Map()
// Apply updates in order
for (const update of updates) {
for (const event of update.added.values()) {
added.set(event.id, event)
}
for (const id of update.removed) {
removed.add(id)
added.delete(id)
}
}
let dirty = false
for (const event of added.values()) {
if (matchFilters(filters, event)) {
const item = eventToItem(event)
if (item) {
dirty = true
data.push(item)
}
}
}
if (!includeDeleted && removed.size > 0) {
const [deleted, ok] = partition(
(item: T) => getIdAndAddress(itemToEvent(item)).some(id => removed.has(id)),
data,
)
if (deleted.length > 0) {
dirty = true
data = ok
}
}
if (dirty) {
setter(data)
}
})
repository.on("update", onUpdate)
return () => repository.off("update", onUpdate)
})
export const deriveEvents = (repository: Repository, opts: {filters: Filter[]; includeDeleted?: boolean}) =>
deriveEventsMapped<TrustedEvent>({
...opts,
repository,
eventToItem: identity,
itemToEvent: identity,
})
export const deriveEvent = (repository: Repository, idOrAddress: string) =>
derived(
deriveEvents(repository, {
filters: getIdFilters([idOrAddress]),
includeDeleted: true,
}),
first
)
export const deriveIsDeletedByAddress = (repository: Repository, event: TrustedEvent) =>
custom<boolean>(setter => {
setter(repository.isDeletedByAddress(event))
const onUpdate = batch(300, () => setter(repository.isDeletedByAddress(event)))
repository.on("update", onUpdate)
return () => repository.off("update", onUpdate)
})
+7
View File
@@ -0,0 +1,7 @@
{
"targets": [
{"extname": ".cjs", "module": "commonjs"},
{"extname": ".mjs", "module": "esnext", "moduleResolution": "node"}
],
"projects": ["tsconfig.json"]
}
+11
View File
@@ -0,0 +1,11 @@
{
"extends": "../../node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build",
"esModuleInterop": true,
"skipLibCheck": true,
"lib": ["esnext", "dom", "dom.iterable"]
},
"include": ["**/*.ts"]
}
+5 -3
View File
@@ -20,7 +20,6 @@ export const GROUP_CHAT_THREAD = 11
export const GROUP_CHAT_THREAD_REPLY = 12
export const SEAL = 13
export const DIRECT_MESSAGE = 14
export const READ_RECEIPT = 15
export const GENERIC_REPOST = 16
export const CHANNEL_CREATE = 40
export const CHANNEL_UPDATE = 41
@@ -35,8 +34,8 @@ export const WRAP_NIP04 = 1060
export const FILE_METADATA = 1063
export const LIVE_CHAT_MESSAGE = 1311
export const GIT_PATCH = 1617
export const GIT_ISSUE = 1617
export const GIT_REPLY = 1617
export const GIT_ISSUE = 1621
export const GIT_REPLY = 1622
export const GIT_STATUS_OPEN = 1630
export const GIT_STATUS_COMPLETE = 1631
export const GIT_STATUS_CLOSED = 1632
@@ -103,6 +102,9 @@ export const TOPICS = 10015
export const EMOJIS = 10030
export const INBOX_RELAYS = 10050
export const FILE_SERVERS = 10096
export const SEEN_GENERAL = 10115
export const SEEN_CONTEXT = 10116
export const SEEN_CONVERSATION = 10117
export const LIGHTNING_PUB_RPC = 21000
export const CLIENT_AUTH = 22242
export const WALLET_INFO = 13194