-
-
Notifications
You must be signed in to change notification settings - Fork 642
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
feat(vanilla): Add a hack for jotai-scope #2353
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit b938def:
|
First of all, thanks for working on this. Here's some notes.
|
As I state in jotaijs/jotai-scope#20, the shallow equality comparison is insufficient. Yesterday I thought about hours and figured out the key is to call atom.write EXACTLY once, which means this modification is unavoidable.
I am not so sure, at least part of the change should be reverted. Let me illustrate the key difference: |
#2356 is something I would like to go with. |
And, it seems intuitive, no? const copyAtom = (a) => ({ ...a });
const countAtom = atom(0);
const copiedAtom = copyAtom(countAtom);
set(copiedAtom, 1);
get(countAtom) // ---> should be `0` |
Nope, the difference is here Lines 100 to 102 in b5c3808
and Lines 103 to 115 in b5c3808
Originally I want to make sure the However, I think it can be solved by adding more conditions in jotai-scope. |
The difference is not in store access, but in the parameter passed to setter and getter. Very hard to explain this, maybe after I write a documentation to explain how the scope works now. |
anAtomCopy = {...anAtom};
store.set(anAtomCopy, 0);
// =>
anAtomCopy.write(getter, setter, 0);
// =>
anAtomCopy.write(getter, (a, 0) => set(a, 0), 0);
// with `this`, a here is `anAtomCopy`
// without `this`, a here is `anAtom` |
I still want to keep #2186 as it feels right and gives more capability. So, I wish you can find a solution on the jotai-scope end. |
by the way, you can override |
@@ -524,7 +532,7 @@ export const createStore = () => { | |||
...args: As | |||
) => { | |||
let r: R | undefined | |||
if ((a as AnyWritableAtom) === atom) { | |||
if (getScopedOriginal(a) === getScopedOriginal(atom)) { |
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 same thing can be done with #2356, no?
anAtom.is = function(target) {
return getScopedOriginal(this) === getScopedOriginal(target)
}
Reverts #2186
Add a property
scopedOriginal
and add a intercepted function to ensure copy of scoped atoms can directly set itself without calling its write function again. Now all the write function in jotai-scope get executed exactly once.Related to jotaijs/jotai-scope#20