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

Double state update for Date (Objects) vars in component with binding. #1657

Closed
stalkerg opened this issue Aug 16, 2018 · 7 comments
Closed

Comments

@stalkerg
Copy link
Contributor

First example: https://svelte.technology/repl?version=2.11.0&gist=10425d9f7670c91b6aff756943ffb43c
In console for the first button, you should saw two print and one for the second button.

This happens because we wrong compare Date object after this._state = assign(assign({}, oldState), newState); in _set.
Ofcourse, it will happens for any object and it bad too but for Date at least we can solve this problem just use .getTime() in _differs.

@stalkerg
Copy link
Contributor Author

Will be good if we can introduce functions for compare variables in state like:

	export default {
		data() {
			return {
				bindVar: new Date(),
				bindVar2: " " + Math.random(),
			};
		},
		equalentors: {
                	bindVar: (a, b) => a.getTime() === b.getTime(),
                },
	}

@stalkerg
Copy link
Contributor Author

immutable: true, ha immutable solve my problem

@Conduitry
Copy link
Member

Part of me thinks that letting the object comparison logic be customized is a neat idea - but another part of me thinks that we should just keep it as it is now, and say that if you're at the point where you're caring about that level of optimization, you should probably be using immutable data. Having a special case just for Dates seems weird. This would also be extra code that everyone would be forced to download and run, whether or not it was relevant to them.

Also, Equalentor is definitely the name of my next band.

@mrkishi
Copy link
Member

mrkishi commented Aug 17, 2018

Should the parent even be .set()ing the child component after direct binding changes?

@stalkerg
Copy link
Contributor Author

@Conduitry

you should probably be using immutable data

unfortunately, in JavaScript, we have no any good way to make an equal operator for objects.
In other languages, you can set this outside - in your own class or object. In JS you should do it explicitly anyway.
I want to say in some case we will not have a good solution even with "immutable".

Also, Equalentor is definitely the name of my next band.

I can help you with some instruments. :) PS I just tried to find a good name for this thing and makes the new with the cool scifi sound.

@stalkerg
Copy link
Contributor Author

Should the parent even be .set()ing the child component after direct binding changes?

looks like no... somewhere bug here. At least, what we pushed up we shouldn't set directly again.

@Conduitry
Copy link
Member

We have #2886 for making improvements to the comparators, but we don't want to start worrying about different specific object types. As noted above, a component with immutable data won't have problems with this. Closing.

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

No branches or pull requests

4 participants