-
Notifications
You must be signed in to change notification settings - Fork 6
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: base addition for context handling #8
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 baf1af6:
|
some ideas for the complete version. // invalid
const state = proxy({
count: 0,
inc() {
++this.count
},
})
// valid
const state = proxy({
count: 0,
inc() {
++state.count
},
})
// valid
const state = proxy({
count: 0,
inc: function () {
++state.count
},
})
// valid
const state = proxy({
count: 0,
inc: () => {
++state.count
},
})
// invalid
const state = proxy({
count: 0,
inc: function () {
++this.count
},
})
// invalid (technically possible though)
const state = proxy({
count: 0,
inc: () => {
++this.count
},
})
// valid
const state = proxy({
arr: [],
inc: () => {
state.arr = [1, 2, 3].map(function(x) { return x / this }, 10)
},
})
// invalid
const state = proxy({
count: [],
inc() {
(() => { ++this.count })()
},
}) Basically, if |
On second thought, we don't need to take care of those edge cases. We can just warn |
don't plan to either, but these are just for the tests so that we can be assured that beginners are warned that something is going to be wrong aka, more for our confirmation than for the users Also, |
If possible, we want to detect this, but maybe it's hard and not possible across modules? // invalid
const initialObj = {
count: 0,
inc() {
++this.count
},
}
const state = proxy(initialObj) |
I think it's good. To add a note, we can make sure that the rule is optional. |
Hmm, I'll have to add in a few more handlers to see how I can handle that, it should be possible but then I guess the eslint error would show on the
They can turn it off from the rules , and yes i'll add "using Though, I think people turning the rule off will probably know what it's going to do so makes sense to keep it in the recommended rules |
handles if the this context is used inside another call context instead of proxy extra: also adds comments and `nearestCalleeName` in utils
@dai-shi I have a version that detects this // invalid
const initialObj = {
count: 0,
inc() {
++this.count
},
}
const state = proxy(initialObj) though it's the hackiest solution I've ever written, so I'm testing it with other common setups so it doesn't collide with them. |
@dai-shi want me to set it as ready for review or is there another case we need to check, let me know |
If you don't have any concerns, it means ready! |
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.
LGTM. Nice work.
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
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.
That's a clean & valid code! Thank you for your efforts.
pmndrs/valtio#204 (comment)
Note: Needs additional test cases and checks , this is just a base commit for the feature, not the completed version.