-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[Fix Logouts] Persist accounts synchronously #9109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
OK, I think this should be ready for review/testing. I've done some casual testing on the web and it seemed to work. I have not tested on iOS/Android and probably won't be able to (apparently my system is out of date). |
|
||
constructor() { | ||
// Careful: By the time this runs, `persisted` needs to already be filled. | ||
const initialState = getInitialState(persisted.get('session').accounts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens in the case where there is no persisted session or no accounts? Could this result in an error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The persisted
abstraction fills that in with default values (i.e. we get []
here). This part is similar to how it worked before, I just moved it to a class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timing caveat is just that the initial read is async. We delay the component tree from mounting until then but reading in module scope would've read too early.
Dig it, will look in the morning |
I'll try to get these changes merged into my test instance that does all that logging, and then test it there for a week or two |
Oh no I said "instance" :( |
This should address the second problem @mackuba identified in #8695.
Concretely, this one: #8695 (comment).
Note: Although this PR complements #9108, it doesn't depend on it.
The Problem
There may be an async gap between a network callback from a backgrounded tab's agent event (which calls
dispatch
) and the correspondinguseReducer
execution + subsequentuseEffect
that writes thestate
into persisted state.The Fix
There may be some simpler way to write this. But for now, I'd suggest to keep the existing logic (for safety) but drop
useReducer
entirely. Instead, we'll add a custom store class we hook up to React viauseSyncExternalStore
.The store calls our reducer logic but makes sure to persist to local storage immediately. So we don't need to wait for a React render cycle or batching or any effects. It gets written right away.
Note that
dispatch
is referentially stable (thanks to the store being kept in state) so we shouldn't be changing anything about memoization etc. This should be a purely internal change for whatdispatch
does.Test Plan
The usual with accounts — log in, log out, open many tabs, verify they're updating in unison, etc. @estrattonbailey would know what to do.
This does need careful testing. I haven't tested it myself yet.