Skip to content

Commit

Permalink
Clean up isDirty handling
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Nov 10, 2023
1 parent 4990212 commit d0d9316
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/state/session/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,25 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
currentAccount: undefined, // assume logged out to start
})

const setStateAndPersist = React.useCallback(
(fn: (prev: StateContext) => StateContext) => {
isDirty.current = true
setState(fn)
},
[setState],
)

const upsertAccount = React.useCallback(
(account: persisted.PersistedAccount, expired = false) => {
isDirty.current = true
setState(s => {
setStateAndPersist(s => {
return {
...s,
currentAccount: expired ? undefined : account,
accounts: [account, ...s.accounts.filter(a => a.did !== account.did)],
}
})
},
[setState],
[setStateAndPersist],
)

const createAccount = React.useCallback<ApiContext['createAccount']>(
Expand Down Expand Up @@ -232,8 +239,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 => {
setStateAndPersist(s => {
return {
...s,
agent: PUBLIC_BSKY_AGENT,
Expand All @@ -245,7 +251,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
})),
}
})
}, [setState])
}, [setStateAndPersist])

const initSession = React.useCallback<ApiContext['initSession']>(
async account => {
Expand Down Expand Up @@ -303,8 +309,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {

const removeAccount = React.useCallback<ApiContext['removeAccount']>(
account => {
isDirty.current = true
setState(s => {
setStateAndPersist(s => {
return {
...s,
accounts: s.accounts.filter(
Expand All @@ -313,15 +318,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}
})
},
[setState],
[setStateAndPersist],
)

const updateCurrentAccount = React.useCallback<
ApiContext['updateCurrentAccount']
>(
account => {
isDirty.current = true
setState(s => {
setStateAndPersist(s => {
const currentAccount = s.currentAccount

// ignore, should never happen
Expand All @@ -347,7 +351,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}
})
},
[setState],
[setStateAndPersist],
)

const selectAccount = React.useCallback<ApiContext['selectAccount']>(
Expand All @@ -367,12 +371,11 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
)

const clearCurrentAccount = React.useCallback(() => {
isDirty.current = true
setState(s => ({
setStateAndPersist(s => ({
...s,
currentAccount: undefined,
}))
}, [setState])
}, [setStateAndPersist])

React.useEffect(() => {
if (isDirty.current) {
Expand Down

0 comments on commit d0d9316

Please sign in to comment.