Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit b7d0eab

Browse files
vicbmhevery
authored andcommitted
style: code cleanup
1 parent dc75b01 commit b7d0eab

File tree

9 files changed

+120
-117
lines changed

9 files changed

+120
-117
lines changed

lib/core/filter.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class NgFilter {
4040
@NgInjectableService()
4141
class FilterMap extends AnnotationMap<NgFilter> {
4242
Injector _injector;
43-
FilterMap(Injector injector, MetadataExtractor extractMetadata) :
44-
this._injector = injector,
45-
super(injector, extractMetadata);
43+
FilterMap(Injector injector, MetadataExtractor extractMetadata)
44+
: this._injector = injector,
45+
super(injector, extractMetadata);
4646

4747
call(String name) {
4848
var filter = new NgFilter(name: name);

lib/core/parser/dynamic_parser.dart

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,40 @@ class DynamicParserBackend extends ParserBackend {
7878
return new Filter(expression, name, arguments, allArguments);
7979
}
8080

81-
Expression newChain(expressions)
82-
=> new Chain(expressions);
83-
Expression newAssign(target, value)
84-
=> new Assign(target, value);
81+
Expression newChain(expressions) => new Chain(expressions);
82+
Expression newAssign(target, value) => new Assign(target, value);
8583
Expression newConditional(condition, yes, no)
8684
=> new Conditional(condition, yes, no);
8785

88-
Expression newAccessKeyed(object, key)
89-
=> new AccessKeyed(object, key);
86+
Expression newAccessKeyed(object, key) => new AccessKeyed(object, key);
9087
Expression newCallFunction(function, arguments)
9188
=> new CallFunction(function, arguments);
9289

93-
Expression newPrefixNot(expression)
94-
=> new PrefixNot(expression);
90+
Expression newPrefixNot(expression) => new PrefixNot(expression);
9591

9692
Expression newBinary(operation, left, right)
9793
=> new Binary(operation, left, right);
9894

99-
Expression newLiteralPrimitive(value)
100-
=> new LiteralPrimitive(value);
101-
Expression newLiteralArray(elements)
102-
=> new LiteralArray(elements);
103-
Expression newLiteralObject(keys, values)
104-
=> new LiteralObject(keys, values);
105-
Expression newLiteralString(value)
106-
=> new LiteralString(value);
95+
Expression newLiteralPrimitive(value) => new LiteralPrimitive(value);
96+
Expression newLiteralArray(elements) => new LiteralArray(elements);
97+
Expression newLiteralObject(keys, values) => new LiteralObject(keys, values);
98+
Expression newLiteralString(value) => new LiteralString(value);
10799

108100

109101
Expression newAccessScope(name) {
110102
Getter getter = _closures.lookupGetter(name);
111103
Setter setter = _closures.lookupSetter(name);
112-
if (getter != null && setter != null) {
113-
return new AccessScopeFast(name, getter, setter);
114-
} else {
115-
return new AccessScope(name);
116-
}
104+
return (getter != null && setter != null)
105+
? new AccessScopeFast(name, getter, setter)
106+
: new AccessScope(name);
117107
}
118108

119109
Expression newAccessMember(object, name) {
120110
Getter getter = _closures.lookupGetter(name);
121111
Setter setter = _closures.lookupSetter(name);
122-
if (getter != null && setter != null) {
123-
return new AccessMemberFast(object, name, getter, setter);
124-
} else {
125-
return new AccessMember(object, name);
126-
}
112+
return (getter != null && setter != null)
113+
? new AccessMemberFast(object, name, getter, setter)
114+
: new AccessMember(object, name);
127115
}
128116

129117
Expression newCallScope(name, arguments) {
@@ -137,7 +125,7 @@ class DynamicParserBackend extends ParserBackend {
137125
Expression newCallMember(object, name, arguments) {
138126
Function constructor = _computeCallConstructor(
139127
_callMemberConstructors, name, arguments);
140-
return (constructor != null)
128+
return constructor != null
141129
? constructor(object, name, arguments, _closures)
142130
: new CallMember(object, name, arguments);
143131
}

lib/core/parser/static_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class StaticParserFunctions {
1515
class StaticParser implements Parser<Expression> {
1616
final StaticParserFunctions _functions;
1717
final DynamicParser _fallbackParser;
18-
final Map<String, Expression> _cache = {};
18+
final _cache = <String, Expression>{};
1919
StaticParser(this._functions, this._fallbackParser);
2020

2121
Expression call(String input) {

lib/core/parser/syntax.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ abstract class Expression {
3939
bool get isAssignable => false;
4040
bool get isChain => false;
4141

42-
eval(scope, [FilterMap filters = defaultFilterMap])
43-
=> throw new EvalError("Cannot evaluate $this");
44-
assign(scope, value)
45-
=> throw new EvalError("Cannot assign to $this");
46-
bind(context, [LocalsWrapper wrapper])
47-
=> new BoundExpression(this, context, wrapper);
42+
eval(scope, [FilterMap filters = defaultFilterMap]) =>
43+
throw new EvalError("Cannot evaluate $this");
44+
assign(scope, value) =>
45+
throw new EvalError("Cannot assign to $this");
46+
bind(context, [LocalsWrapper wrapper]) =>
47+
new BoundExpression(this, context, wrapper);
4848

4949
accept(Visitor visitor);
5050
String toString() => Unparser.unparse(this);

lib/core_dom/directive.dart

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,62 +15,55 @@ class NodeAttrs {
1515
final dom.Element element;
1616

1717
Map<String, List<AttributeChanged>> _observers;
18-
19-
Map<String, Mustache> _mustacheObservers = {};
20-
Set<String> _mustacheComputedAttrs = new Set<String>();
18+
final _mustacheAttrs = <String, _MustacheAttr>{};
2119

2220
NodeAttrs(this.element);
2321

24-
operator [](String attributeName) => element.attributes[attributeName];
22+
operator [](String attrName) => element.attributes[attrName];
2523

26-
void operator []=(String attributeName, String value) {
27-
if (_mustacheObservers.containsKey(attributeName)) {
28-
_mustacheComputedAttrs.add(attributeName);
24+
void operator []=(String attrName, String value) {
25+
if (_mustacheAttrs.containsKey(attrName)) {
26+
_mustacheAttrs[attrName].isComputed = true;
2927
}
3028
if (value == null) {
31-
element.attributes.remove(attributeName);
29+
element.attributes.remove(attrName);
3230
} else {
33-
element.attributes[attributeName] = value;
31+
element.attributes[attrName] = value;
3432
}
35-
if (_observers != null && _observers.containsKey(attributeName)) {
36-
_observers[attributeName].forEach((fn) => fn(value));
33+
if (_observers != null && _observers.containsKey(attrName)) {
34+
_observers[attrName].forEach((notifyFn) => notifyFn(value));
3735
}
3836
}
3937

4038
/**
41-
* Observe changes to the attribute by invoking the [AttributeChanged]
42-
* function. On registration the [AttributeChanged] function gets invoked
43-
* synchronise with the current value.
39+
* Observe changes to the attribute by invoking the [notifyFn] function. On
40+
* registration the [notifyFn] function gets invoked synchronize with the
41+
* current value.
4442
*/
45-
observe(String attributeName, AttributeChanged notifyFn) {
43+
observe(String attrName, AttributeChanged notifyFn) {
4644
if (_observers == null) _observers = <String, List<AttributeChanged>>{};
47-
_observers.putIfAbsent(attributeName, () => <AttributeChanged>[])
45+
_observers.putIfAbsent(attrName, () => <AttributeChanged>[])
4846
.add(notifyFn);
4947

50-
bool hasMustache = _mustacheObservers.containsKey(attributeName);
51-
bool hasMustacheAndIsComputed = _mustacheComputedAttrs.contains(attributeName);
52-
53-
if (!hasMustache || hasMustacheAndIsComputed) {
54-
notifyFn(this[attributeName]);
55-
}
56-
57-
if (_mustacheObservers.containsKey(attributeName)) {
58-
_mustacheObservers[attributeName](true);
48+
if (_mustacheAttrs.containsKey(attrName)) {
49+
if (_mustacheAttrs[attrName].isComputed) notifyFn(this[attrName]);
50+
_mustacheAttrs[attrName].notifyFn(true);
51+
} else {
52+
notifyFn(this[attrName]);
5953
}
6054
}
6155

6256
void forEach(void f(String k, String v)) {
6357
element.attributes.forEach(f);
6458
}
6559

66-
bool containsKey(String attributeName) =>
67-
element.attributes.containsKey(attributeName);
60+
bool containsKey(String attrName) => element.attributes.containsKey(attrName);
6861

6962
Iterable<String> get keys => element.attributes.keys;
7063

71-
void listenObserverChanges(String attributeName, Mustache fn) {
72-
_mustacheObservers[attributeName] = fn;
73-
fn(false);
64+
void listenObserverChanges(String attrName, Mustache notifyFn) {
65+
_mustacheAttrs[attrName] = new _MustacheAttr(notifyFn);
66+
notifyFn(false);
7467
}
7568
}
7669

@@ -84,3 +77,12 @@ class TemplateLoader {
8477

8578
TemplateLoader(this.template);
8679
}
80+
81+
class _MustacheAttr {
82+
// Listener trigger when the attribute becomes observed
83+
final Mustache notifyFn;
84+
// Whether the value has first been computed
85+
bool isComputed = false;
86+
87+
_MustacheAttr(this.notifyFn);
88+
}

perf/parser_perf.dart

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import '../gen/generated_getter_setter.dart' as generated_getter_setter;
1313

1414
main() {
1515
var module = new Module()
16-
..type(Parser, implementedBy: DynamicParser)
17-
..type(ParserBackend, implementedBy: DynamicParserBackend)
18-
..type(SubstringFilter)
19-
..type(IncrementFilter)
20-
..install(new NgFilterModule());
16+
..type(Parser, implementedBy: DynamicParser)
17+
..type(ParserBackend, implementedBy: DynamicParserBackend)
18+
..type(SubstringFilter)
19+
..type(IncrementFilter)
20+
..install(new NgFilterModule());
2121
var injector = new DynamicInjector(
2222
modules: [module],
2323
allowImplicitInjection:true);
@@ -27,16 +27,17 @@ main() {
2727

2828
var generatedParser = new DynamicInjector(
2929
modules: [new Module()
30-
..type(Parser, implementedBy: StaticParser)
31-
..type(ParserBackend, implementedBy: DynamicParserBackend)
32-
..value(StaticParserFunctions, generated_functions.functions())],
30+
..type(Parser, implementedBy: StaticParser)
31+
..type(ParserBackend, implementedBy: DynamicParserBackend)
32+
..value(StaticParserFunctions, generated_functions.functions())],
3333
allowImplicitInjection:true).get(Parser);
3434

3535
var hybridParser = new DynamicInjector(
3636
modules: [new Module()
37-
..type(Parser, implementedBy: DynamicParser)
38-
..type(ParserBackend, implementedBy: DynamicParserBackend)
39-
..type(ClosureMap, implementedBy: generated_getter_setter.StaticClosureMap)],
37+
..type(Parser, implementedBy: DynamicParser)
38+
..type(ParserBackend, implementedBy: DynamicParserBackend)
39+
..type(ClosureMap,
40+
implementedBy: generated_getter_setter.StaticClosureMap)],
4041
allowImplicitInjection:true).get(Parser);
4142

4243
scope['a'] = new ATest();
@@ -53,15 +54,15 @@ main() {
5354
var rTime = measure(() => reflectionExpr.eval(scope));
5455
var hTime = measure(() => hybridExpr.eval(scope));
5556
var iTime = measure(() => idealFn(scope));
56-
print('$expr => g: ${nf.format(gTime)} ops/sec ' +
57-
'r: ${nf.format(rTime)} ops/sec ' +
58-
'h: ${nf.format(hTime)} ops/sec ' +
59-
'i: ${nf.format(iTime)} ops/sec = ' +
60-
'i/g: ${nf.format(iTime / gTime)} x ' +
61-
'i/r: ${nf.format(iTime / rTime)} x ' +
62-
'i/h: ${nf.format(iTime / hTime)} x ' +
63-
'g/h: ${nf.format(gTime / hTime)} x ' +
64-
'h/r: ${nf.format(hTime / rTime)} x ' +
57+
print('$expr => g: ${nf.format(gTime)} ops/sec '
58+
'r: ${nf.format(rTime)} ops/sec '
59+
'h: ${nf.format(hTime)} ops/sec '
60+
'i: ${nf.format(iTime)} ops/sec = '
61+
'i/g: ${nf.format(iTime / gTime)} x '
62+
'i/r: ${nf.format(iTime / rTime)} x '
63+
'i/h: ${nf.format(iTime / hTime)} x '
64+
'g/h: ${nf.format(gTime / hTime)} x '
65+
'h/r: ${nf.format(hTime / rTime)} x '
6566
'g/r: ${nf.format(gTime / rTime)} x');
6667
}
6768

test/core/parser/parser_spec.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ main() {
6161
filters = injectedFilters;
6262
});
6363

64-
eval(String text, [FilterMap f])
65-
=> parser(text).eval(context, f == null ? filters : f);
64+
eval(String text, [FilterMap f]) =>
65+
parser(text).eval(context, f == null ? filters : f);
6666
expectEval(String expr) => expect(() => eval(expr));
6767

6868
beforeEach((){ context = {}; });

test/core/scope_spec.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,26 +1149,26 @@ void main() {
11491149

11501150

11511151
it(r'should return a function that allows listeners to be unregistered', inject(
1152-
(RootScope rootScope) {
1153-
var listener = jasmine.createSpy('watch listener');
1154-
var watch;
1155-
1156-
watch = rootScope.watch('foo', listener);
1157-
rootScope.digest(); //init
1158-
expect(listener).toHaveBeenCalled();
1159-
expect(watch).toBeDefined();
1160-
1161-
listener.reset();
1162-
rootScope.context['foo'] = 'bar';
1163-
rootScope.digest(); //triger
1164-
expect(listener).toHaveBeenCalledOnce();
1165-
1166-
listener.reset();
1167-
rootScope.context['foo'] = 'baz';
1168-
watch.remove();
1169-
rootScope.digest(); //trigger
1170-
expect(listener).not.toHaveBeenCalled();
1171-
}));
1152+
(RootScope rootScope) {
1153+
var listener = jasmine.createSpy('watch listener');
1154+
var watch;
1155+
1156+
watch = rootScope.watch('foo', listener);
1157+
rootScope.digest(); //init
1158+
expect(listener).toHaveBeenCalled();
1159+
expect(watch).toBeDefined();
1160+
1161+
listener.reset();
1162+
rootScope.context['foo'] = 'bar';
1163+
rootScope.digest(); //triger
1164+
expect(listener).toHaveBeenCalledOnce();
1165+
1166+
listener.reset();
1167+
rootScope.context['foo'] = 'baz';
1168+
watch.remove();
1169+
rootScope.digest(); //trigger
1170+
expect(listener).not.toHaveBeenCalled();
1171+
}));
11721172

11731173

11741174
it(r'should not infinitely digest when current value is NaN', (RootScope rootScope) {

0 commit comments

Comments
 (0)