Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

fix(compiler): OneWayOneTime bindings now wait for the model to stablize #1013

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,18 @@ class ElementBinder {

Expression attrExprFn = _parser(nodeAttrs[attrName]);
var watch;
var lastOneTimeValue;
watch = scope.watch(nodeAttrs[attrName], (value, _) {
if (dstPathFn.assign(controller, value) != null) {
watch.remove();
if ((lastOneTimeValue = dstPathFn.assign(controller, value)) != null && watch != null) {
var watchToRemove = watch;
watch = null;
scope.rootScope.domWrite(() {
Copy link
Contributor

Choose a reason for hiding this comment

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

domWrite sounds wrong here

if (lastOneTimeValue != null) {
watchToRemove.remove();
} else { // It was set to non-null, but stablized to null, wait.
watch = watchToRemove;
}
});
}
}, formatters: formatters);
break;
Expand Down
72 changes: 71 additions & 1 deletion test/core_dom/compiler_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ void main() {
..bind(SometimesComponent)
..bind(ExprAttrComponent)
..bind(LogElementComponent)
..bind(SayHelloFormatter);
..bind(SayHelloFormatter)
..bind(OneTimeDecorator);
});

it('should select on element', async(() {
Expand Down Expand Up @@ -690,6 +691,64 @@ void main() {
}
}));
});

describe('bindings', () {
it('should set a one-time binding with the correct value', (Logger logger) {
_.compile(r'<div one-time="v"></div>');

_.rootScope.context['v'] = 1;

var context = _.rootScope.context;
_.rootScope.watch('3+4', (v, _) => context['v'] = v);

// In the 1st digest iteration:
// v will be set to 7
// OneTimeDecorator.value will be set to 1
// In the 2nd digest iteration:
// OneTimeDecorator.value will be set to 7
_.rootScope.apply();

expect(logger).toEqual([1, 7]);
});

it('should keep one-time binding until it is set to non-null', (Logger logger) {
_.compile(r'<div one-time="v"></div>');
_.rootScope.context['v'] = null;
_.rootScope.apply();
expect(logger).toEqual([null]);

_.rootScope.context['v'] = 7;
_.rootScope.apply();
expect(logger).toEqual([null, 7]);

// Check that the binding is removed.
_.rootScope.context['v'] = 8;
_.rootScope.apply();
expect(logger).toEqual([null, 7]);
});

it('should remove the one-time binding only if it stablizied to null', (Logger logger) {
_.compile(r'<div one-time="v"></div>');

_.rootScope.context['v'] = 1;

var context = _.rootScope.context;
_.rootScope.watch('3+4', (v, _) => context['v'] = null);

_.rootScope.apply();
expect(logger).toEqual([1, null]);

// Even though there was a null in the unstable model, we shouldn't remove the binding
context['v'] = 8;
_.rootScope.apply();
expect(logger).toEqual([1, null, 8]);

// Check that the binding is removed.
_.rootScope.context['v'] = 9;
_.rootScope.apply();
expect(logger).toEqual([1, null, 8]);
});
});
});


Expand Down Expand Up @@ -1130,3 +1189,14 @@ class LogElementComponent{
logger(shadowRoot);
}
}

@Decorator(
selector: '[one-time]',
map: const {
'one-time': '=>!value'
})
class OneTimeDecorator {
Logger log;
OneTimeDecorator(this.log);
set value(v) => log(v);
}