Add setProfile and sync helper
This commit is contained in:
@@ -53,11 +53,23 @@ const publish = async (content: string) => {
|
||||
|
||||
## Built in commands
|
||||
|
||||
Several thunk factories are provided for more complicated scenarios like updating lists:
|
||||
Several thunk factories are provided for common or more complicated scenarios like updating lists:
|
||||
|
||||
- `follow(pubkey)`
|
||||
- `unfollow(pubkey)`
|
||||
- `mute(pubkey)`
|
||||
- `unmute(pubkey)`
|
||||
- `pin(tag)`
|
||||
- `unpin(tag)`
|
||||
- `removeRelay(url: string, mode: RelayMode)`
|
||||
- `addRelay(url: string, mode: RelayMode)`
|
||||
- `removeInboxRelay(url: string)`
|
||||
- `addInboxRelay(url: string)`
|
||||
- `setProfile(profile: Profile)`
|
||||
- `unfollow(value: string)`
|
||||
- `follow(tag: string[])`
|
||||
- `unmute(value: string)`
|
||||
- `mute(tag: string[])`
|
||||
- `unpin(value: string)`
|
||||
- `pin(tag: string[])`
|
||||
- `sendWrapped({template, pubkeys, ...options}: SendWrappedOptions)`
|
||||
- `manageRelay(url: string, request: ManagementRequest)`
|
||||
- `createRoom(url: string, room: RoomMeta)`
|
||||
- `deleteRoom(url: string, room: RoomMeta)`
|
||||
- `editRoom(url: string, room: RoomMeta)`
|
||||
- `joinRoom(url: string, room: RoomMeta)`
|
||||
- `leaveRoom(url: string, room: RoomMeta)`
|
||||
|
||||
@@ -15,6 +15,9 @@ import {
|
||||
makeRoomEditEvent,
|
||||
makeRoomJoinEvent,
|
||||
makeRoomLeaveEvent,
|
||||
isPublishedProfile,
|
||||
createProfile,
|
||||
editProfile,
|
||||
RelayMode,
|
||||
INBOX_RELAYS,
|
||||
FOLLOWS,
|
||||
@@ -22,7 +25,7 @@ import {
|
||||
MUTES,
|
||||
PINS,
|
||||
} from "@welshman/util"
|
||||
import type {RoomMeta} from "@welshman/util"
|
||||
import type {RoomMeta, Profile} from "@welshman/util"
|
||||
import {Nip59, stamp} from "@welshman/signer"
|
||||
import {Router, addMaximalFallbacks} from "@welshman/router"
|
||||
import {
|
||||
@@ -86,6 +89,16 @@ export const addInboxRelay = async (url: string) => {
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
// NIP 01
|
||||
|
||||
export const setProfile = (profile: Profile) => {
|
||||
const router = Router.get()
|
||||
const relays = router.merge([router.Index(), router.FromUser()]).getUrls()
|
||||
const event = isPublishedProfile(profile) ? editProfile(profile) : createProfile(profile)
|
||||
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
// NIP 02
|
||||
|
||||
export const unfollow = async (value: string) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {writable} from "svelte/store"
|
||||
import {writable, Writable} from "svelte/store"
|
||||
import {getJson, setJson} from "@welshman/lib"
|
||||
|
||||
export interface StorageProvider {
|
||||
@@ -6,32 +6,39 @@ export interface StorageProvider {
|
||||
set: (key: string, value: any) => Promise<void>
|
||||
}
|
||||
|
||||
export const localStorageProvider: StorageProvider = {
|
||||
get: async (key: string) => getJson(key),
|
||||
set: async (key: string, value: any) => setJson(key, value),
|
||||
}
|
||||
|
||||
export interface SyncConfig<T> {
|
||||
key: string
|
||||
store: Writable<T>
|
||||
storage: StorageProvider
|
||||
}
|
||||
|
||||
export const sync = <T>({key, store, storage}: SyncConfig<T>) => {
|
||||
storage.get(key).then((value: T | undefined) => {
|
||||
if (value !== undefined) {
|
||||
store.set(value)
|
||||
}
|
||||
})
|
||||
|
||||
store.subscribe(async (value: T) => {
|
||||
await storage.set(key, value)
|
||||
})
|
||||
}
|
||||
|
||||
export interface SyncedConfig<T> {
|
||||
key: string
|
||||
storage: StorageProvider
|
||||
defaultValue: T
|
||||
}
|
||||
|
||||
export const localStorageProvider: StorageProvider = {
|
||||
get: async (key: string) => getJson(key),
|
||||
set: async (key: string, value: any) => setJson(key, value),
|
||||
}
|
||||
|
||||
export const synced = <T>(config: SyncedConfig<T>) => {
|
||||
const {key, storage, defaultValue} = config
|
||||
export const synced = <T>({key, storage, defaultValue}: SyncedConfig<T>) => {
|
||||
const store = writable<T>(defaultValue)
|
||||
|
||||
// Async initialization
|
||||
storage.get(key).then((value: any) => {
|
||||
if (value !== undefined) {
|
||||
store.set(value)
|
||||
}
|
||||
})
|
||||
|
||||
// Subscribe to changes
|
||||
store.subscribe(async (value: T) => {
|
||||
await storage.set(key, value)
|
||||
})
|
||||
sync({key, store, storage})
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user