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

fix(utils): make derive glitch free #335

Merged
merged 6 commits into from
Jan 24, 2022
Merged

Conversation

dai-shi
Copy link
Member

@dai-shi dai-shi commented Jan 24, 2022

@mweststrate hinted that a solution to make glitch free is simply to use a counter. https://twitter.com/mweststrate/status/1484928361084379136

So I tried. closes #296

I had to make the subscription sync and then async in derive. There might be some edge cases this won't work well, like mixing sync and async, but the basic patterns should be covered. Let's see how it goes with releasing this.

@vercel
Copy link

vercel bot commented Jan 24, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pmndrs/valtio/o8ctRVtBQgUTvNvw8gi8cSGB8xC8
✅ Preview: https://valtio-git-fix-utilsglitch-free-derive-pmndrs.vercel.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 24, 2022

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 e3a37a0:

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

@github-actions
Copy link

github-actions bot commented Jan 24, 2022

Size Change: +716 B (+2%)

Total Size: 33.4 kB

Filename Size Change
dist/esm/utils.js 3.58 kB +165 B (+5%) 🔍
dist/system/utils.js 3.88 kB +174 B (+5%) 🔍
dist/umd/utils.js 4.45 kB +194 B (+5%) 🔍
dist/utils.js 4.25 kB +183 B (+4%)
ℹ️ View Unchanged
Filename Size
dist/esm/index.js 1.36 kB
dist/esm/macro.js 613 B
dist/esm/vanilla.js 1.84 kB
dist/index.js 1.45 kB
dist/macro.js 916 B
dist/system/index.js 1.59 kB
dist/system/macro.js 733 B
dist/system/vanilla.js 2.02 kB
dist/umd/index.js 1.59 kB
dist/umd/macro.js 1.07 kB
dist/umd/vanilla.js 2.11 kB
dist/vanilla.js 1.96 kB

compressed-size-action

@dai-shi dai-shi changed the title fix: make derive glitch free fix(utils): make derive glitch free Jan 24, 2022
@@ -211,7 +211,7 @@ it('nested emulation with derive', async () => {
{
doubled: (get) => computeDouble(get(state.math).count),
},
{ proxy: state.math }
{ proxy: state.math, sync: true }
Copy link
Member Author

Choose a reason for hiding this comment

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

This is technically a behavioral change, but hope it's not too critical.

Copy link
Contributor

Choose a reason for hiding this comment

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

So if it's async it can't be glitch free?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, but I didn't confirm it in the nested scenario. Without sync in this case, it simply double call the subscriber. It's a side effect of supporting glitch free.

@dai-shi dai-shi merged commit 105d57c into main Jan 24, 2022
@dai-shi dai-shi deleted the fix/utils/glitch-free-derive branch January 24, 2022 11:41
dai-shi added a commit that referenced this pull request Jan 25, 2022
…341)

* fix(tests): make glitch test count right

* tweak a test

* fix: derive with shared subscriptions

* refactor

* revise comments

Co-authored-by: daishi <daishi@axlight.com>
@Noitidart
Copy link
Contributor

Just tested it, wow it's so awesome! Even when false its glitch free! I upgraded the package to 1.2.11 here and tried - https://codesandbox.io/s/dervie-glitch-0ji5r

@dai-shi
Copy link
Member Author

dai-shi commented Jan 25, 2022

Thanks for confirming. That's great.
The reason I did #335 in the first place (and I didn't notice the bug because the tests are bad. appreciate for your fix!) is, as you might see, #341 implementation is very complicated. I liked the original very simple derive impl (even glitched), but let's try to improve the new impl.

@Noitidart
Copy link
Contributor

Thank you brother!

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.

This library doesn't seem to be glitch free?
2 participants