Skip to content

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented Sep 29, 2025

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 corresponding useReducer execution + subsequent useEffect that writes the state 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 via useSyncExternalStore.

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 what dispatch 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.

@gaearon gaearon marked this pull request as draft September 29, 2025 23:40
@gaearon gaearon marked this pull request as ready for review September 29, 2025 23:56
@gaearon
Copy link
Contributor Author

gaearon commented Sep 29, 2025

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)

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@estrattonbailey estrattonbailey self-requested a review September 30, 2025 02:02
@estrattonbailey
Copy link
Member

Dig it, will look in the morning

@mackuba
Copy link

mackuba commented Sep 30, 2025

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

@mackuba
Copy link

mackuba commented Sep 30, 2025

Oh no I said "instance" :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants