Add some session utils
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user