Skip to content
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

Scope caused atomWithObservable to be out of sync #36

Open
tien opened this issue Jun 10, 2024 · 2 comments
Open

Scope caused atomWithObservable to be out of sync #36

tien opened this issue Jun 10, 2024 · 2 comments

Comments

@tien
Copy link

tien commented Jun 10, 2024

Example

The 2 counters below while using the same atom are completely different

const counterSubject = interval(1000).pipe(map((i) => `#${i}`));
const counterAtom = atomWithObservable(() => counterSubject);

const Counter = () => {
  const [counter] = useAtom(counterAtom);
  return <div>count: {counter}</div>;
};

export default function App() {
  return (
    <div className="App">
      <h1>Jotai scope bug</h1>
      <Counter />
      <ScopeProvider atoms={[]}>
        <Suspense>
          <Counter />
        </Suspense>
      </ScopeProvider>
    </div>
  );
}

Reproduction

Code sandbox link

@dmaskasky
Copy link
Member

dmaskasky commented Jun 11, 2024

Related: #25

There is currently no way to distinguish between a derived atom that references a scoped atom, and a derived atom that does not. jotai-scope has to copy every derived atom to intercept their getter and setter to address this. As you discovered, this copy can cause problems.

PR #35 adds an api to not copy specified derived atoms. I would merge this but I think there's a better way.

PR #2609 makes a change to the store api that removes the need to copy derived atoms at all. 2609 should merge in a few weeks.

Once 2609 lands I'll rewrite jotai-scope to address the issue you found and #25.

@dmaskasky
Copy link
Member

dmaskasky commented Jun 24, 2024

Update: Integrating with pmndrs/jotai#2609 is a lot harder than I imagined. I need to spend more time to understand how to rewrite jotai-scope following these changes.

Also, it looks like an atomWithObservable refactor is in the works with pmndrs/jotai#2631, but unfortunately it looks as though the issue is still present.

https://codesandbox.io/p/sandbox/atom-with-observable-infinite-mount-bug-fix-forked-253y84?file=%2Fsrc%2FApp.tsx%3A6%2C1-7%2C1

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

No branches or pull requests

2 participants