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

[BUGFIX release] Consumes tags after fully getting values #18668

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions packages/@ember/-internals/metal/lib/property_get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,11 @@ export function get(obj: object, keyName: string): any {
let value: any;

if (isObjectLike) {
let tracking = isTracking();

if (tracking) {
consume(tagForProperty(obj, keyName));
}

if (DEBUG && HAS_NATIVE_PROXY) {
value = getPossibleMandatoryProxyValue(obj, keyName);
} else {
value = obj[keyName];
}

if (tracking) {
// Add the tag of the returned value if it is an array, since arrays
// should always cause updates if they are consumed and then changed
if (Array.isArray(value) || isEmberArray(value)) {
consume(tagForProperty(value, '[]'));
}

// Add the value of the content if the value is a proxy. This is because
// content changes the truthiness/falsiness of the proxy.
if (isProxy(value)) {
consume(tagForProperty(value, 'content'));
}
}
} else {
value = obj[keyName];
}
Expand All @@ -141,18 +121,31 @@ export function get(obj: object, keyName: string): any {
typeof (obj as MaybeHasUnknownProperty).unknownProperty === 'function'
) {
if (DEBUG) {
let ret;

deprecateMutationsInAutotrackingTransaction!(() => {
ret = (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
value = (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
});

return ret;
} else {
return (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
value = (obj as MaybeHasUnknownProperty).unknownProperty!(keyName);
}
}
}

if (isObjectLike && isTracking()) {
consume(tagForProperty(obj, keyName));

// Add the tag of the returned value if it is an array, since arrays
// should always cause updates if they are consumed and then changed
if (Array.isArray(value) || isEmberArray(value)) {
consume(tagForProperty(value, '[]'));
}

// Add the value of the content if the value is a proxy. This is because
// content changes the truthiness/falsiness of the proxy.
if (isProxy(value)) {
consume(tagForProperty(value, 'content'));
}
}

return value;
}

Expand Down
20 changes: 20 additions & 0 deletions packages/@ember/-internals/metal/tests/tracked/validation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,5 +402,25 @@ moduleFor(
});
}, /You attempted to update `foo` on `EmberObject`, but it had already been used previously in the same computation/);
}

['@test get() does not entangle in the autotracking stack until after retrieving the value'](
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this test? How did it fail before? How does it confirm what it says it is confirming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before, get would consume the tag for the property immediately, then actually get the value of the property. If the property signaled a change via notifyPropertyChange in this time, it would throw the backtracking assertion. This is the fundamental problem that led to the bug in Ember Data.

After, we consume all tags for the first time after getting resolved values, whatever they end up being, which means it is safe to signal property changes while calculating the value.

assert
) {
assert.expect(0);

class EmberObject {
get foo() {
notifyPropertyChange(this, 'foo');

return 123;
}
}

let obj = new EmberObject();

track(() => {
get(obj, 'foo');
});
}
}
);