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

Evaluated computed property dependencies first. Fixes #5143 #5145

Closed
wants to merge 5 commits into from

Conversation

kevinpschaaf
Copy link
Member

Updates the implementation of runComputedEffect to recursively walk the graph of dependencies that are themselves computed and re-compute them if needed, which ensures computed properties are not computed out of order.

The initial walk is still kicked off by the normal runEffects loop over changed properties, which ensures that no work related to computed properties occurs unless at least one dependency to a computed property has changed. An alternate implementation of running every computed effect each flush was considered, but it seems worth keeping the "pay-for-play" model intact. Thus, the effect de-duping mechanism still exists, which prevents a property later in the batch of changes from causing any computed effect that has already run during this flush from running again, and also prevents iloops for circular dependencies.

When walking the graph of computed dependencies to a computed property, we track when a computed effect is being run transitively (vs. directly due to a dependency change in changedProps) by passing a sentinel TRANSITIVE_DEPENDENCY property name to runComputedEffect's property argument (which normally is the input dependency which triggered the change). This tracking is useful because transitively run computed effects need to dirty-check their dependencies, while effects that were directly run by runEffects can skip the expensive argument dirty check to occur since they are already guaranteed to have at least one argument dirty.

Reference Issue

Fixes #5143

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

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

Only some small nits regarding the implementation. Other than that implementations looks fine.

* @return {boolean} true when the property is dirty
* @private
*/
function propertyIsDirty(inst, trigger, changedProps) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put these functions before runComputedEffect. That makes reading these functions a bit easier, since runComputedEffect calls into these functions and I was confused which ones they were.

let deps = info.computedDependencies;
if (!deps) {
deps = info.computedDependencies = [];
info.args.forEach(arg => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be a info.args.reduce instead

}
}
}

const TRANSITIVE_DEPENDENCY = '~transitive~dependency~';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use a Symbol here to make sure we are always unique? (however unlikely someone names a property exactly this)

@@ -82,20 +84,23 @@
*
* @param {Object} model Prototype or instance
* @param {string} type Property effect type
* @param {boolean=} cloneArrays Clone any arrays mapped in the map
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of adding a boolean parameter, I would rather see an Object{cloneArrays:boolean} per #4864

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@stale
Copy link

stale bot commented Mar 13, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 13, 2020
@stale
Copy link

stale bot commented Mar 30, 2022

This issue has been automatically closed after being marked stale. If you're still facing this problem with the above solution, please comment and we'll reopen!

@stale stale bot closed this Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature request] Run computed properties in dependency order
3 participants