-
-
Notifications
You must be signed in to change notification settings - Fork 617
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: freeze atom util #185
Conversation
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 6804270:
|
I think there might be expectation that If this is intentional I think it's better to not return anything from |
It’s intentional and I can accept your suggestion. |
I'd suggest to encourage functional programming patterns, and make |
Do you mean like this? export function freezeAtom<T extends Atom<unknown>>(anAtom: T) {
const origRead = anAtom.read
return {
...anAtom,
read: (get: Getter) => {
const value = origRead(get)
deepFreeze(value)
return value
}
}
} I'm fine with this too. Just thought originally that people expect freeze to have side effects, analogous to Another option is we don't export freezeAtom, just atomFroznInDev. How does that sound? cc @likern |
It will require deep-cloning, otherwise original object will be affected. I think performance penalty will be significant. If we will deep-clone (correct, if I am wrong) no need to freeze object. I think to make it pure side-effect is better. Providing |
Here's a clarification just in case. Let's not confuse atom configs and atom values. Now, Object.freeze and |
Yes, I understand that. But when we will use original atom there is no such expectation, especially when I would expect this code should work const frozenAtom = freezeAtom(nonFrozenAtom)
...
const value = useAtom(nonFrozenAtom)
value.property = 'changed' but because of |
OK, was just in case.
The code snippet I wrote in #185 (comment) makes it possible, no? Hm, that code wasn't very nice. It's not updating Let me update this PR and see how it goes. |
Updated the description. |
Maybe a dumb suggestion but what if we had: import { atom } from 'jotai';
const myAtom = atom(42);
myAtom.freeze(); There is absolutely no doubt that this method has the side effect of changing |
@mdingena Thanks for ideation and suggestion. I agree that's a cleaner api. I enjoy these conversations.
However, I'd prefer this fp way.
This requires adding a property and it may increase a production bundle a bit, which I prefer to avoid. We had some discussions in #167, but currently what I think is:
|
Should we move forward with this, or still gather more ideas? |
I think it's good to go as utils without affecting core. |
ref: #167
Usage 1
Usage 2