diff options
author | Eric Bailey <git@esb.lol> | 2023-11-10 08:46:45 -0600 |
---|---|---|
committer | Eric Bailey <git@esb.lol> | 2023-11-10 08:46:45 -0600 |
commit | b0c9cce5c3ea9246fbc2f71ac64c10c5252ec9a4 (patch) | |
tree | 510b14357ceff341a79c8947b9ea9b7e1fe4464d /src/state/session/index.tsx | |
parent | 742f53d1ecfc5060b7e6fc0cea0f8729d2e9b1b5 (diff) | |
download | voidsky-b0c9cce5c3ea9246fbc2f71ac64c10c5252ec9a4.tar.zst |
Follow conventions for query, use isDirty flag in session store to avoid unneccessary writes
Diffstat (limited to 'src/state/session/index.tsx')
-rw-r--r-- | src/state/session/index.tsx | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index a5362ac6b..85ae3b520 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -4,7 +4,7 @@ import {BskyAgent, AtpPersistSessionHandler} from '@atproto/api' import {networkRetry} from '#/lib/async/retry' import {logger} from '#/logger' import * as persisted from '#/state/persisted' -import {PUBLIC_BSKY_AGENT} from '#/data' +import {PUBLIC_BSKY_AGENT} from '#/state/queries' export type SessionAccount = persisted.PersistedAccount @@ -102,6 +102,7 @@ function createPersistSessionHandler( } export function Provider({children}: React.PropsWithChildren<{}>) { + const isDirty = React.useRef(false) const [state, setState] = React.useState<StateContext>({ agent: PUBLIC_BSKY_AGENT, hasSession: false, @@ -113,6 +114,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const upsertAccount = React.useCallback( (account: persisted.PersistedAccount, expired = false) => { + isDirty.current = true setState(s => { return { ...s, @@ -124,7 +126,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) { [setState], ) - // TODO have not connected this yet const createAccount = React.useCallback<ApiContext['createAccount']>( async ({service, email, password, handle, inviteCode}: any) => { logger.debug( @@ -231,6 +232,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const logout = React.useCallback<ApiContext['logout']>(async () => { logger.debug(`session: logout`, {}, logger.DebugContext.session) + isDirty.current = true setState(s => { return { ...s, @@ -301,6 +303,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const removeAccount = React.useCallback<ApiContext['removeAccount']>( account => { + isDirty.current = true setState(s => { return { ...s, @@ -317,6 +320,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ApiContext['updateCurrentAccount'] >( account => { + isDirty.current = true setState(s => { const currentAccount = s.currentAccount @@ -363,6 +367,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) const clearCurrentAccount = React.useCallback(() => { + isDirty.current = true setState(s => ({ ...s, currentAccount: undefined, @@ -370,10 +375,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) { }, [setState]) React.useEffect(() => { - persisted.write('session', { - accounts: state.accounts, - currentAccount: state.currentAccount, - }) + if (isDirty.current) { + persisted.write('session', { + accounts: state.accounts, + currentAccount: state.currentAccount, + }) + isDirty.current = false + } }, [state]) React.useEffect(() => { |