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): re-implement derive to make glitch free (#335 was buggy) #341

Merged
merged 5 commits into from
Jan 25, 2022

Conversation

Noitidart
Copy link
Contributor

@Noitidart Noitidart commented Jan 25, 2022

Related: #296 (comment)

@vercel
Copy link

vercel bot commented Jan 25, 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/5hbgLdq3Gx5kJ1p3t9m4ZhpjuMVt
✅ Preview: https://valtio-git-fork-noitidart-patch-3-pmndrs.vercel.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 25, 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 a553473:

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

const App = () => {
const snap = useSnapshot(derived3)

renderCount++
Copy link
Member

Choose a reason for hiding this comment

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

We should count commits not renders. Let me tweak the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh very interesting I didn't know useEffect was the way to count commits! I learned a lot here thanks!

expect(computeValue).toBeCalledTimes(2)

await findByText('value: v0: 1, v1: 1, v2: 1')
expect(computeValue).toBeCalledTimes(3)
Copy link
Member

Choose a reason for hiding this comment

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

This should be 2, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes! Sorry, I made it 3 because thats the count when it was glitched. Thanks for helping fix!

@dai-shi
Copy link
Member

dai-shi commented Jan 25, 2022

Okay, I kind of new my impl in #335 was truly correct. I had another impl, and based on it, a commit is added. It's complicated, so let me try some refactoring.

Meanwhile, @Noitidart could you try if it solves the issue?

@dai-shi dai-shi changed the title fix(tests): make glitch test count right fix(utils): re-implement derive to make glitch free (#335 was buggy) Jan 25, 2022
@dai-shi
Copy link
Member

dai-shi commented Jan 25, 2022

Let's release this soon, because #342 is a bigger issue. v1.2.10 is full of bugs. 😓

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.

Thanks for your investigation. I hope this is better than #335, but not sure if there are some bugs remaining. Please report issues if anything goes wrong.

@dai-shi dai-shi merged commit 07da988 into pmndrs:main Jan 25, 2022
@Noitidart
Copy link
Contributor Author

Okay, I kind of new my impl in #335 was truly correct. I had another impl, and based on it, a commit is added. It's complicated, so let me try some refactoring.

Meanwhile, @Noitidart could you try if it solves the issue?

Trying now! Sorry for delay I'm on west coast us so just woke up haha.

@Noitidart
Copy link
Contributor Author

With v1.2.9 this used to happen:

​compute1 
compute2 
compute3: 
000 
render value: 
v0: 0, v1: 0, v2: 0 
INCREMENT
compute1 
compute3: 
110 
compute2 
compute3: 
111 
render value: 
v0: 1, v1: 1, v2: 1 

Notice how compute1 then compute3 happens, then again compute2 happens then compute3 happens.

Now in v1.2.11 its perfect:

compute1 
compute2 
compute3: 
000 
render value: 
v0: 0, v1: 0, v2: 0 
INCREMENT
compute1 
compute2 
compute3: 
111 
render value: 
v0: 1, v1: 1, v2: 1 

compute1 then compuet2 then compute3 and all variables are lined up! no extra callback is triggered!

@dai-shi
Copy link
Member

dai-shi commented Jan 25, 2022

Trying now! Sorry for delay I'm on west coast us so just woke up haha.

No worries! I knew the timezone difference. I wanted to replace troublesome v1.2.10 as soon as possible.

@Noitidart
Copy link
Contributor Author

Totally gotcha, thanks for giving me chance to take part of the emergency release. If possible it happens again no matter time if I see it I would definitely get on it, thanks for trying to include us man!

@dai-shi
Copy link
Member

dai-shi commented Jan 25, 2022

By the way, this implementation might still have a limitation. A circular graph may result in an infinite loop. Would you like to test it??
(It may happen with v1.2.9 too.)

@Noitidart
Copy link
Contributor Author

By the way, this implementation might still have a limitation. A circular graph may result in an infinite loop. Would you like to test it?? (It may happen with v1.2.9 too.)

Haha thanks! I don't understand the circular graph situation, if you could help me understand I would learn about this circular graph thing.

@dai-shi
Copy link
Member

dai-shi commented Jan 26, 2022

The circular graph is something like this: A derived from B derived from C derived from A.

@dai-shi
Copy link
Member

dai-shi commented Feb 1, 2022

Found this: https://en.wikipedia.org/wiki/Reactive_programming#Cyclic_dependencies

It's called cyclic dependencies.

@Noitidart
Copy link
Contributor Author

Oh thanks! Sorry I got busy with urgently implementing a Stripe payment system update. I'll have to revisit this please. Thanks very much for trying to get me involved!

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