-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsession-router.ts
More file actions
38 lines (30 loc) · 995 Bytes
/
session-router.ts
File metadata and controls
38 lines (30 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { chromeLocalStore } from 'chrome/helpers/chrome-local-store'
import { ResultAsync, ok } from 'neverthrow'
export type SessionRouter = ReturnType<typeof SessionRouter>
export type SessionId = string
export type WalletPublicKey = string
export const SessionRouter = () => {
const store = new Map<SessionId, WalletPublicKey>()
const getWalletPublicKey = (sessionId?: SessionId) =>
sessionId ? store.get(sessionId) : undefined
const refreshStore = (data: Record<SessionId, WalletPublicKey>) => {
store.clear()
Object.entries(data).forEach(([sessionId, walletPublicKey]) => {
store.set(sessionId, walletPublicKey)
})
}
return {
refreshStore,
getWalletPublicKey,
store,
}
}
export const sessionRouter = SessionRouter()
export const getSessionRouterData = (): ResultAsync<
Record<string, string>,
never
> =>
chromeLocalStore
.getItem('sessionRouter')
.map(({ sessionRouter }) => sessionRouter || {})
.orElse(() => ok({}))