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

feat(vanilla): Support class fields (defineProperty) #760

Merged
merged 4 commits into from
Jul 20, 2023

Conversation

pastelmind
Copy link
Contributor

Related Issues or Discussions

Fixes #759 (the feature request at the end)

Summary

Adds support for ES2022 class fields created with Object.defineProperty() semantics. When a configurable, enumerable, and writable (CEW) own property (usually a class field) is created on a proxy object that does not have the property, or has a CEW own property of the same name, treat it like a [[set]] trap.

Check List

  • yarn run prettier for formatting code and docs

Support ES2022 class fields created with Object.defineProperty()
semantics. When a class field is created on a proxy object that does not
have the field yet, or has a data field of the same name, treat it
like a [[set]] trap.
@vercel
Copy link

vercel bot commented Jul 12, 2023

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

Name Status Preview Comments Updated (UTC)
valtio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 20, 2023 2:44am

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 12, 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 577e63e:

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

@dai-shi
Copy link
Member

dai-shi commented Jul 13, 2023

https://github.com/pmndrs/valtio/actions/runs/5537152975/jobs/10105667717?pr=760

Size Change: +1.33 kB (+2%)

Total Size: 58.6 kB

Filename Size Change
dist/esm/vanilla.js 2.59 kB +170 B (+7%) 🔍
dist/system/vanilla.development.js 2.74 kB +171 B (+7%) 🔍
dist/system/vanilla.production.js 1.61 kB +104 B (+7%) 🔍
dist/umd/vanilla.development.js 3.08 kB +343 B (+13%) ⚠️
dist/umd/vanilla.production.js 1.83 kB +201 B (+12%) ⚠️
dist/vanilla.js 2.95 kB +342 B (+13%) ⚠️
ℹ️ 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 732 B
dist/esm/react/utils.js 225 B
dist/esm/utils.js 68 B
dist/esm/vanilla/utils.js 4.28 kB
dist/index.js 243 B
dist/macro.js 918 B
dist/macro/vite.js 1.08 kB
dist/react.js 668 B
dist/react/utils.js 244 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 871 B
dist/system/react.production.js 471 B
dist/system/react/utils.development.js 321 B
dist/system/react/utils.production.js 223 B
dist/system/utils.development.js 241 B
dist/system/utils.production.js 176 B
dist/system/vanilla/utils.development.js 4.5 kB
dist/system/vanilla/utils.production.js 2.9 kB
dist/umd/index.development.js 382 B
dist/umd/index.production.js 330 B
dist/umd/macro.development.js 1.04 kB
dist/umd/macro.production.js 721 B
dist/umd/macro/vite.development.js 1.23 kB
dist/umd/macro/vite.production.js 879 B
dist/umd/react.development.js 814 B
dist/umd/react.production.js 526 B
dist/umd/react/utils.development.js 400 B
dist/umd/react/utils.production.js 299 B
dist/umd/utils.development.js 398 B
dist/umd/utils.production.js 344 B
dist/umd/vanilla/utils.development.js 5.04 kB
dist/umd/vanilla/utils.production.js 3.13 kB
dist/utils.js 247 B
dist/vanilla/utils.js 4.87 kB

@dai-shi
Copy link
Member

dai-shi commented Jul 13, 2023

I think it's good. Thanks for the suggestion.

I would like to refactor to make "treat it like a [[set]] trap" clear.
Let me add some commits.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

Not sure if there are pitfalls, but this should be harmless for normal (plain objects) usage, except for the bundle size increase.

@dai-shi dai-shi merged commit c0bda5a into pmndrs:main Jul 20, 2023
29 checks passed
@pastelmind pastelmind deleted the support-define-property branch July 20, 2023 10:39
@pastelmind
Copy link
Contributor Author

While investigating #764, I learned that the [[Set]] internal method invokes [[DefineOwnProperty]] when the property does not exist or is a data property. This means that each property assignment triggers both the set and defineProperty traps of the proxy--potentially calling notifyUpdate() twice (unless the property is a setter).

Perhaps I was too hasty in reusing the set trap logic for the defineProperty trap? It would be semantically correct to create a separate Op for defineProperty.

@dai-shi
Copy link
Member

dai-shi commented Jul 23, 2023

potentially calling notifyUpdate() twice

would it be possible to reproduce it in tests?

pastelmind added a commit to pastelmind/valtio that referenced this pull request Jul 24, 2023
)"

This reverts commit c0bda5a.

Due to what I presume is a bug in the Hermes engine, React Native
projects cannot use Valtio 1.11.0. Let's revert pmndrs#760 to support existing
React Native users.
dai-shi pushed a commit that referenced this pull request Jul 27, 2023
…766)

This reverts commit c0bda5a.

Due to what I presume is a bug in the Hermes engine, React Native
projects cannot use Valtio 1.11.0. Let's revert #760 to support existing
React Native users.
pastelmind pushed a commit to pastelmind/valtio that referenced this pull request Jul 28, 2023
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