Add some session utils
This commit is contained in:
@@ -64,7 +64,7 @@ export type RouterOptions = {
|
|||||||
* Retrieves the user's public key.
|
* Retrieves the user's public key.
|
||||||
* @returns The user's public key as a string, or null if not available.
|
* @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.
|
* Retrieves relays for the specified public key and mode.
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ export type SessionAnyMethod =
|
|||||||
|
|
||||||
export type Session = SessionAnyMethod & Record<string, any>
|
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", {}))
|
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) =>
|
export const updateSession = (pubkey: string, f: (session: Session) => Session) =>
|
||||||
putSession(f(getSession(pubkey)))
|
putSession(f(getSession(pubkey)))
|
||||||
|
|
||||||
export const dropSession = (pubkey: string) =>
|
export const dropSession = (_pubkey: string) => {
|
||||||
sessions.update($sessions => omit([pubkey], $sessions))
|
pubkey.update($pubkey => $pubkey === _pubkey ? undefined : $pubkey)
|
||||||
|
sessions.update($sessions => omit([_pubkey], $sessions))
|
||||||
|
}
|
||||||
|
|
||||||
|
export const clearSessions = () => {
|
||||||
|
pubkey.set(undefined)
|
||||||
|
sessions.set({})
|
||||||
|
}
|
||||||
|
|
||||||
// Session factories
|
// Session factories
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user