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

refactor(vanilla): simplify handling object descriptor #654

Merged
merged 2 commits into from
Feb 7, 2023

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented Feb 5, 2023

Thought this would be more consistent with #653 (comment).

@vercel
Copy link

vercel bot commented Feb 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
valtio ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 6, 2023 at 1:57AM (UTC)

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 5, 2023

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 399f92d:

Sandbox Source
React Configuration
React Typescript Configuration
React Browserify Configuration
React Snowpack Configuration
React Parcel Configuration

@github-actions
Copy link

github-actions bot commented Feb 5, 2023

Size Change: -71 B (0%)

Total Size: 55.3 kB

Filename Size Change
dist/esm/vanilla.js 2.28 kB -8 B (0%)
dist/system/vanilla.development.js 2.42 kB -3 B (0%)
dist/system/vanilla.production.js 1.45 kB -10 B (-1%)
dist/umd/vanilla.development.js 2.58 kB -20 B (-1%)
dist/umd/vanilla.production.js 1.52 kB -11 B (-1%)
dist/vanilla.js 2.47 kB -19 B (-1%)
ℹ️ View Unchanged
Filename Size
dist/esm/index.js 62 B
dist/esm/macro.js 698 B
dist/esm/macro/vite.js 864 B
dist/esm/react.js 710 B
dist/esm/react/utils.js 221 B
dist/esm/utils.js 68 B
dist/esm/vanilla/utils.js 4.21 kB
dist/index.js 232 B
dist/macro.js 937 B
dist/macro/vite.js 1.09 kB
dist/react.js 652 B
dist/react/utils.js 237 B
dist/system/index.development.js 236 B
dist/system/index.production.js 170 B
dist/system/macro.development.js 779 B
dist/system/macro.production.js 556 B
dist/system/macro/vite.development.js 951 B
dist/system/macro/vite.production.js 660 B
dist/system/react.development.js 851 B
dist/system/react.production.js 466 B
dist/system/react/utils.development.js 316 B
dist/system/react/utils.production.js 221 B
dist/system/utils.development.js 241 B
dist/system/utils.production.js 176 B
dist/system/vanilla/utils.development.js 4.43 kB
dist/system/vanilla/utils.production.js 2.84 kB
dist/umd/index.development.js 372 B
dist/umd/index.production.js 322 B
dist/umd/macro.development.js 1.05 kB
dist/umd/macro.production.js 724 B
dist/umd/macro/vite.development.js 1.23 kB
dist/umd/macro/vite.production.js 882 B
dist/umd/react.development.js 797 B
dist/umd/react.production.js 519 B
dist/umd/react/utils.development.js 396 B
dist/umd/react/utils.production.js 297 B
dist/umd/utils.development.js 386 B
dist/umd/utils.production.js 333 B
dist/umd/vanilla/utils.development.js 4.7 kB
dist/umd/vanilla/utils.production.js 2.92 kB
dist/utils.js 236 B
dist/vanilla/utils.js 4.54 kB

compressed-size-action

@dai-shi
Copy link
Member Author

dai-shi commented Feb 5, 2023

@stephenh Would you review this please?

Copy link
Contributor

@stephenh stephenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi looks good to me!

Small disclaimer I am still in the "barely following along" stage of understanding and not "ah I immediately see why this is better" stage. :-)

Object.defineProperty(baseObject, key, desc)
} else {
const hasValue = 'value' in desc
delete desc.value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi just curious, by why delete desc.value and then reset it?

I had noticed when first diving into this that proxyObject[key] = initialObject[key] invoked the handler's set and did the whole "version"/"notifyUpdate"s process for every key on initialObject.

My curiosity was whether, since we've just created the proxy, there should defacto be no subscribers, maybe we could skip all of that?

I might be missing something else important that the handler.set does, not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR, some tests failed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sure, it looks like it sets up child proxies (just glancing at the subscribe.test.tsx failure). That makes sense.

Maybe leave an in-source hint for future maintainers/newbies? LIke

// Unset and reset the value to ensure child proxies are wrapped
const hasValue = 'value' in desc
delete desc.value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@@ -245,9 +245,7 @@ const buildProxyFunction = (
value = getUntracked(value) || value
}
let nextValue = value
if (Object.getOwnPropertyDescriptor(target, prop)?.set) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I had came across this code and couldn't really piece together why it was necessary; i.e. why we wanted to avoid the promise/child proxy logic just b/c this was a setter.

I am a little morbidly curious why it was necessary in the 1st place, but that's not super important, and great to see it go away.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid handling things if setter exists, because we don't know what the setter does. It can be something really unexpected. But, anyway, this would simplify it. Not really sure what happens...

@dai-shi
Copy link
Member Author

dai-shi commented Feb 5, 2023

Small disclaimer I am still in the "barely following along" stage of understanding and not "ah I immediately see why this is better" stage. :-)

Sure, totally fine. (And, there are only few who barely follow along.)

@dai-shi dai-shi merged commit 4f0158c into main Feb 7, 2023
@dai-shi dai-shi deleted the refactor/vanilla/property-descriptor branch February 7, 2023 14:25
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.

2 participants