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] Fix reactivity of dynamic attributes #1268

Merged
merged 1 commit into from
Feb 16, 2021
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
37 changes: 36 additions & 1 deletion packages/@glimmer/integration-tests/test/attributes-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { normalizeProperty } from '@glimmer/runtime';
import { assertElement, hasAttribute, jitSuite, RenderTest, test } from '..';
import { assertElement, hasAttribute, jitSuite, RenderTest, test, tracked } from '..';
import { Namespace, SimpleElement } from '@simple-dom/interface';
import { castToBrowser, expect } from '@glimmer/util';

export class AttributesTests extends RenderTest {
static suiteName = 'Attributes';
Expand Down Expand Up @@ -239,6 +240,40 @@ export class AttributesTests extends RenderTest {
this.assertStableNodes();
}

@test
'handles successive updates to the same value'() {
class Model {
@tracked value = '';
}

let model = new Model();

this.render('<input value={{this.model.value}} />', { model });
this.assert.equal(this.readDOMAttr('value'), '');
this.assertStableRerender();

let inputElement = castToBrowser(
expect(this.element.firstChild, 'expected input to exist'),
'input'
);

inputElement.value = 'bar';
this.assert.equal(this.readDOMAttr('value'), 'bar');

model.value = 'foo';
this.rerender();
this.assert.equal(this.readDOMAttr('value'), 'foo');
this.assertStableNodes();

inputElement.value = 'bar';
this.assert.equal(this.readDOMAttr('value'), 'bar');

model.value = 'foo';
this.rerender();
this.assert.equal(this.readDOMAttr('value'), 'foo');
this.assertStableNodes();
}

@test
'input[checked] prop updates when set to undefined'() {
this.registerHelper('if', (params) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ function setDeferredAttr(
.elements()
.setDynamicAttribute(name, valueForRef(value), trusting, namespace);
if (!isConstRef(value)) {
vm.updateWith(new UpdateDynamicAttributeOpcode(value, attribute));
vm.updateWith(new UpdateDynamicAttributeOpcode(value, attribute, vm.env));
}
}
}
Expand Down
34 changes: 21 additions & 13 deletions packages/@glimmer/runtime/lib/compiled/opcodes/dom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Reference, valueForRef, isConstRef } from '@glimmer/reference';
import { Reference, valueForRef, isConstRef, createComputeRef } from '@glimmer/reference';
import { Revision, Tag, valueForTag, validateTag, consumeTag } from '@glimmer/validator';
import {
check,
Expand All @@ -17,6 +17,7 @@ import {
Owner,
CurriedType,
ModifierDefinitionState,
Environment,
} from '@glimmer/interfaces';
import { $t0 } from '@glimmer/vm';
import { APPEND_OPCODES, UpdatingOpcode } from '../../opcodes';
Expand Down Expand Up @@ -237,27 +238,34 @@ APPEND_OPCODES.add(Op.DynamicAttr, (vm, { op1: _name, op2: _trusting, op3: _name
let attribute = vm.elements().setDynamicAttribute(name, value, trusting, namespace);

if (!isConstRef(reference)) {
vm.updateWith(new UpdateDynamicAttributeOpcode(reference, attribute));
vm.updateWith(new UpdateDynamicAttributeOpcode(reference, attribute, vm.env));
}
});

export class UpdateDynamicAttributeOpcode extends UpdatingOpcode {
public type = 'patch-element';

public lastValue: unknown;
private updateRef: Reference;

constructor(private reference: Reference<unknown>, private attribute: DynamicAttribute) {
constructor(reference: Reference<unknown>, attribute: DynamicAttribute, env: Environment) {
super();
this.lastValue = valueForRef(reference);
}

evaluate(vm: UpdatingVM) {
let { attribute, reference, lastValue } = this;
let currentValue = valueForRef(reference);
let initialized = false;

if (currentValue !== lastValue) {
attribute.update(currentValue, vm.env);
this.lastValue = currentValue;
}
this.updateRef = createComputeRef(() => {
let value = valueForRef(reference);

if (initialized === true) {
attribute.update(value, env);
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I still don't think this guarantees that the DynamicAttribute will actually update things. Some of the subclasses of DynamicAttribute have caches that do not check if the DOM value was changed out from under them.

For example:

update(value: unknown, _env: Environment): void {
let { element } = this.attribute;
if (this.value !== value) {
(element as any)[this.normalizedName] = this.value = value;
if (value === null || value === undefined) {
this.removeAttribute();
}
}
}

The reason the test added above passes is because input.value has a custom DynamicAttribute subclass (that doesn't do the last value caching that DefaultDynamicProperty does):

update(value: unknown) {
let input = castToBrowser(this.attribute.element, ['input', 'textarea']);
let currentValue = input.value;
let normalizedValue = normalizeStringValue(value);
if (currentValue !== normalizedValue) {
input.value = normalizedValue;
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Note: this almost certainly does address the reported issue, it just doesn't 100% fix the full category of issues. I'm fairly sure you could end up with this same issue with other element + attribute combinations that the user could mutate. We have custom overrides for a few common ones (input.value, textarea.value, option.selected), but that list of overrides is not exhaustive (e.g. video element has some local mutable attributes | properties that could have been changed by the user).

} else {
initialized = true;
}
});

valueForRef(this.updateRef);
}

evaluate() {
valueForRef(this.updateRef);
}
}