Add some session utils

This commit is contained in:
Jon Staab
2025-04-11 11:08:25 -07:00
parent bdef37d404
commit 8b67be3bbd
2 changed files with 11 additions and 4 deletions
+1 -1
View File
@@ -64,7 +64,7 @@ export type RouterOptions = {
* Retrieves the user's public key.
* @returns The user's public key as a string, or null if not available.
*/
getUserPubkey?: () => string | null
getUserPubkey?: () => string | undefined
/**
* Retrieves relays for the specified public key and mode.
+10 -3
View File
@@ -52,7 +52,7 @@ export type SessionAnyMethod =
export type Session = SessionAnyMethod & Record<string, any>
export const pubkey = withGetter(synced<string | null>("pubkey", null))
export const pubkey = withGetter(synced<string | undefined>("pubkey", undefined))
export const sessions = withGetter(synced<Record<string, Session>>("sessions", {}))
@@ -76,8 +76,15 @@ export const putSession = (session: Session) => {
export const updateSession = (pubkey: string, f: (session: Session) => Session) =>
putSession(f(getSession(pubkey)))
export const dropSession = (pubkey: string) =>
sessions.update($sessions => omit([pubkey], $sessions))
export const dropSession = (_pubkey: string) => {
pubkey.update($pubkey => $pubkey === _pubkey ? undefined : $pubkey)
sessions.update($sessions => omit([_pubkey], $sessions))
}
export const clearSessions = () => {
pubkey.set(undefined)
sessions.set({})
}
// Session factories