Remove withGetter

This commit is contained in:
Jon Staab
2025-11-03 14:52:12 -08:00
parent 6923c2a8b7
commit b469addd29
2 changed files with 51 additions and 65 deletions
+4 -3
View File
@@ -103,6 +103,7 @@ import {
DEFAULT_BLOSSOM_SERVERS, DEFAULT_BLOSSOM_SERVERS,
userSpaceUrls, userSpaceUrls,
userSettingsValues, userSettingsValues,
getSetting,
userInboxRelays, userInboxRelays,
userGroupSelections, userGroupSelections,
} from "@app/core/state" } from "@app/core/state"
@@ -549,7 +550,7 @@ export const createDmAlert = async () => {
// Settings // Settings
export const makeSettings = async (params: Partial<SettingsValues>) => { export const makeSettings = async (params: Partial<SettingsValues>) => {
const json = JSON.stringify({...userSettingsValues.get(), ...params}) const json = JSON.stringify({...get(userSettingsValues), ...params})
const content = await signer.get().nip44.encrypt(pubkey.get()!, json) const content = await signer.get().nip44.encrypt(pubkey.get()!, json)
const tags = [["d", SETTINGS]] const tags = [["d", SETTINGS]]
@@ -560,10 +561,10 @@ export const publishSettings = async (params: Partial<SettingsValues>) =>
publishThunk({event: await makeSettings(params), relays: Router.get().FromUser().getUrls()}) publishThunk({event: await makeSettings(params), relays: Router.get().FromUser().getUrls()})
export const addTrustedRelay = async (url: string) => export const addTrustedRelay = async (url: string) =>
publishSettings({trusted_relays: append(url, userSettingsValues.get().trusted_relays)}) publishSettings({trusted_relays: append(url, getSetting<string[]>("trusted_relays"))})
export const removeTrustedRelay = async (url: string) => export const removeTrustedRelay = async (url: string) =>
publishSettings({trusted_relays: remove(url, userSettingsValues.get().trusted_relays)}) publishSettings({trusted_relays: remove(url, getSetting<string[]>("trusted_relays"))})
// Join request // Join request
+47 -62
View File
@@ -36,14 +36,7 @@ import {
SocketEvent, SocketEvent,
netContext, netContext,
} from "@welshman/net" } from "@welshman/net"
import { import {collection, custom, throttled, deriveEvents, deriveEventsMapped} from "@welshman/store"
collection,
custom,
throttled,
deriveEvents,
deriveEventsMapped,
withGetter,
} from "@welshman/store"
import {isKindFeed, findFeed} from "@welshman/feeds" import {isKindFeed, findFeed} from "@welshman/feeds"
import { import {
ALERT_ANDROID, ALERT_ANDROID,
@@ -241,31 +234,29 @@ export const deriveEvent = (idOrAddress: string, hints: string[] = []) => {
) )
} }
export const getUrlsForEvent = withGetter( export const getUrlsForEvent = derived([trackerStore, thunks], ([$tracker, $thunks]) => {
derived([trackerStore, thunks], ([$tracker, $thunks]) => { const getThunksByEventId = memoize(() => {
const getThunksByEventId = memoize(() => { const thunksByEventId = new Map<string, Thunk[]>()
const thunksByEventId = new Map<string, Thunk[]>()
for (const thunk of $thunks) { for (const thunk of $thunks) {
pushToMapKey(thunksByEventId, thunk.event.id, thunk) pushToMapKey(thunksByEventId, thunk.event.id, thunk)
}
return thunksByEventId
})
return (id: string) => {
const urls = Array.from($tracker.getRelays(id))
for (const thunk of getThunksByEventId().get(id) || []) {
for (const url of thunk.options.relays) {
urls.push(url)
}
}
return uniq(urls)
} }
}),
) return thunksByEventId
})
return (id: string) => {
const urls = Array.from($tracker.getRelays(id))
for (const thunk of getThunksByEventId().get(id) || []) {
for (const url of thunk.options.relays) {
urls.push(url)
}
}
return uniq(urls)
}
})
export const getEventsForUrl = (url: string, filters: Filter[]) => { export const getEventsForUrl = (url: string, filters: Filter[]) => {
const ids = uniq([ const ids = uniq([
@@ -383,15 +374,13 @@ export const userSettings = makeUserData({
export const loadUserSettings = makeUserLoader(loadSettings) export const loadUserSettings = makeUserLoader(loadSettings)
export const userSettingsValues = withGetter( export const userSettingsValues = derived(userSettings, $s => $s?.values || defaultSettings)
derived(userSettings, $s => $s?.values || defaultSettings),
)
export const getSetting = <T>(key: keyof Settings["values"]) => userSettingsValues.get()[key] as T export const getSetting = <T>(key: keyof Settings["values"]) => get(userSettingsValues)[key] as T
// Relays sending events with empty signatures that the user has to choose to trust // Relays sending events with empty signatures that the user has to choose to trust
export const relaysPendingTrust = withGetter(writable<string[]>([])) export const relaysPendingTrust = writable<string[]>([])
// Relays that mostly send restricted responses to requests and events // Relays that mostly send restricted responses to requests and events
@@ -416,21 +405,19 @@ export type Alert = {
tags: string[][] tags: string[][]
} }
export const alerts = withGetter( export const alerts = deriveEventsMapped<Alert>(repository, {
deriveEventsMapped<Alert>(repository, { filters: [{kinds: [ALERT_EMAIL, ALERT_WEB, ALERT_IOS, ALERT_ANDROID]}],
filters: [{kinds: [ALERT_EMAIL, ALERT_WEB, ALERT_IOS, ALERT_ANDROID]}], itemToEvent: item => item.event,
itemToEvent: item => item.event, eventToItem: async event => {
eventToItem: async event => { const $signer = signer.get()
const $signer = signer.get()
if ($signer) { if ($signer) {
const tags = parseJson(await decrypt($signer, NOTIFIER_PUBKEY, event.content)) const tags = parseJson(await decrypt($signer, NOTIFIER_PUBKEY, event.content))
return {event, tags} return {event, tags}
} }
}, },
}), })
)
export const getAlertFeed = (alert: Alert) => export const getAlertFeed = (alert: Alert) =>
tryCatch(() => JSON.parse(getTagValue("feed", alert.tags)!)) tryCatch(() => JSON.parse(getTagValue("feed", alert.tags)!))
@@ -450,21 +437,19 @@ export type AlertStatus = {
tags: string[][] tags: string[][]
} }
export const alertStatuses = withGetter( export const alertStatuses = deriveEventsMapped<AlertStatus>(repository, {
deriveEventsMapped<AlertStatus>(repository, { filters: [{kinds: [ALERT_STATUS]}],
filters: [{kinds: [ALERT_STATUS]}], itemToEvent: item => item.event,
itemToEvent: item => item.event, eventToItem: async event => {
eventToItem: async event => { const $signer = signer.get()
const $signer = signer.get()
if ($signer) { if ($signer) {
const tags = parseJson(await decrypt($signer, NOTIFIER_PUBKEY, event.content)) const tags = parseJson(await decrypt($signer, NOTIFIER_PUBKEY, event.content))
return {event, tags} return {event, tags}
} }
}, },
}), })
)
export const deriveAlertStatus = (address: string) => export const deriveAlertStatus = (address: string) =>
derived(alertStatuses, statuses => statuses.find(s => getTagValue("d", s.event.tags) === address)) derived(alertStatuses, statuses => statuses.find(s => getTagValue("d", s.event.tags) === address))