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

Commit faaeb32

Browse files
committed
WIP: removed BlockCache
Conflicts: lib/angular.dart lib/block.dart lib/block_list.dart lib/block_type.dart test/compiler_spec.dart
1 parent d1ae74b commit faaeb32

31 files changed

+301
-552
lines changed

lib/block.dart

Lines changed: 126 additions & 248 deletions
Large diffs are not rendered by default.

lib/block_list.dart

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ class BlockListFactory {
44
Scope $rootScope;
55
BlockListFactory(Scope this.$rootScope);
66

7-
BlockList call(List<dom.Node> elements, Map<String, BlockType> blockTypes,
8-
[List<BlockCache> blockCaches]) {
9-
return new BlockList($rootScope, elements, blockTypes, blockCaches);
7+
BlockList call(List<dom.Node> elements, Map<String, BlockType> blockTypes, Injector injector) {
8+
return new BlockList($rootScope, elements, blockTypes, injector);
109
}
1110
}
1211

@@ -19,40 +18,21 @@ class BlockList extends ElementWrapper {
1918
Scope $rootScope;
2019
List<dom.Node> elements;
2120
Map<String, BlockType> blockTypes;
22-
BlockCache blockCache;
23-
Injector customInjector;
21+
Injector injector;
2422

2523
ElementWrapper previous;
2624
ElementWrapper next;
2725

2826
BlockList(Scope this.$rootScope, List<dom.Node> this.elements,
29-
Map<String, BlockType> this.blockTypes, [BlockCache blockCache]) {
30-
this.blockCache = (?blockCache && blockCache != null) ? blockCache : new BlockCache();
31-
32-
// This is a bit of a hack.
33-
// We need to run after the first watch, that means we have to wait for
34-
// watch, and then schedule $evalAsync.
35-
var deregisterWatch = $rootScope.$watch((_) {
36-
// TODO deregisterWatch();
37-
//$rootScope.$evalAsync(() {
38-
// blockCache.flush((block) {
39-
// block.remove();
40-
// });
41-
//});
42-
});
27+
Map<String, BlockType> this.blockTypes, Injector this.injector) {
4328
}
4429

45-
Block newBlock([String type = '']) {
46-
Block block = this.blockCache.get(type);
47-
48-
if (block == null) {
49-
if (!this.blockTypes.containsKey(type)) {
50-
throw new ArgumentError("Unknown block type: '$type'.");
51-
}
52-
53-
block = this.blockTypes[type](null, null, customInjector);
30+
Block newBlock(Scope scope, [String type = '']) {
31+
//TODO(misko): BlockList should not be resposible for BlockTypes. This should be simplified.
32+
if (!this.blockTypes.containsKey(type)) {
33+
throw new ArgumentError("Unknown block type: '$type'.");
5434
}
5535

56-
return block;
36+
return this.blockTypes[type](injector.createChild([new ScopeModule(scope)]));
5737
}
5838
}

lib/block_type.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ class BlockType {
2525
ASSERT(group != null);
2626
}
2727

28-
Block call([List<dom.Node> elements, List<BlockCache> blockCaches, Injector injector]) {
28+
Block call(Injector injector, [List<dom.Node> elements]) {
2929
if (!?elements || elements == null) {
3030
elements = cloneElements(templateElements);
3131
}
32-
if (!?blockCaches || blockCaches == null) {
33-
blockCaches = [];
34-
}
35-
return blockFactory(elements, directivePositions, blockCaches, group, injector: injector);
32+
return blockFactory(elements, directivePositions, group, injector);
3633
}
3734

3835
ClassMirror _getClassMirror(Type type) {

lib/compiler.dart

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Compiler {
1111
}
1212

1313
_compileBlock(NodeCursor domCursor, NodeCursor templateCursor,
14-
List<BlockCache> blockCaches,
1514
List<DirectiveRef> useExistingDirectiveRefs) {
1615
if (domCursor.nodeList().length == 0) return null;
1716

@@ -50,15 +49,10 @@ class Compiler {
5049
}
5150
if (directive.$transclude != null) {
5251
var remainingDirectives = declaredDirectiveRefs.sublist(j + 1);
53-
var transclusion = compileTransclusion(directive.$transclude,
52+
blockTypes = compileTransclusion(directive.$transclude,
5453
domCursor, templateCursor,
5554
directiveRef, remainingDirectives);
5655

57-
if (transclusion['blockCache'] != null) {
58-
blockCaches.add(transclusion['blockCache']);
59-
}
60-
blockTypes = transclusion['blockTypes'];
61-
6256
j = jj; // stop processing further directives since they belong to transclusion;
6357
compileChildren = false;
6458
}
@@ -73,7 +67,7 @@ class Compiler {
7367
templateCursor.descend();
7468

7569
childDirectivePositions = compileChildren
76-
? _compileBlock(domCursor, templateCursor, blockCaches, null)
70+
? _compileBlock(domCursor, templateCursor, null)
7771
: null;
7872

7973
domCursor.ascend();
@@ -106,7 +100,7 @@ class Compiler {
106100
var transcludeCursor = templateCursor.replaceWithAnchor(anchorName);
107101
var groupName = '';
108102
var domCursorIndex = domCursor.index;
109-
var directivePositions = _compileBlock(domCursor, transcludeCursor, [], transcludedDirectiveRefs);
103+
var directivePositions = _compileBlock(domCursor, transcludeCursor, transcludedDirectiveRefs);
110104
if (directivePositions == null) directivePositions = [];
111105

112106
BlockType = $blockTypeFactory(transcludeCursor.elements, directivePositions, groupName);
@@ -127,10 +121,7 @@ class Compiler {
127121
domCursor.replaceWithAnchor(anchorName);
128122
}
129123

130-
return {
131-
"blockTypes": blockTypes,
132-
"blockCache": blocks != null ? new BlockCache(blocks) : null
133-
};
124+
return blockTypes;
134125
}
135126

136127

@@ -172,12 +163,11 @@ class Compiler {
172163
}
173164

174165

175-
call(List<dom.Node> elements, [List<BlockCache> blockCaches]) {
176-
List<dom.Node> domElements = elements;
177-
List<dom.Node> templateElements = cloneElements(domElements);
166+
BlockType call(List<dom.Node> elements) {
167+
List<dom.Node> domElements = elements;
168+
List<dom.Node> templateElements = cloneElements(domElements);
178169
var directivePositions = _compileBlock(
179170
new NodeCursor(domElements), new NodeCursor(templateElements),
180-
?blockCaches && blockCaches != null ? blockCaches : [],
181171
null);
182172

183173
return $blockTypeFactory(templateElements,

lib/directive.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ class DirectiveRegistry {
111111
}
112112
}
113113

114-
class DirectiveValue {
115-
String value;
116-
DirectiveValue() : this.value = "ERROR DEFAULT";
117-
DirectiveValue.fromString(this.value);
118-
}
119-
120114
abstract class DirectiveVisibility {
121115
static const String LOCAL = 'local';
122116
static const String CHILDREN = 'children';

lib/directives/ng_bind.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ part of angular;
22

33
class NgBindAttrDirective {
44

5-
dom.Element element;
6-
DirectiveValue value;
7-
8-
NgBindAttrDirective(dom.Element this.element, DirectiveValue this.value);
9-
10-
attach(Scope scope) {
11-
scope.$watch(value, (value, _, __) { element.text = value; });
5+
NgBindAttrDirective(dom.Element element, NodeAttrs attrs, Scope scope) {
6+
scope.$watch(attrs[this], (value) => element.text = value);
127
}
138

149
}

lib/directives/ng_class.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ part of angular;
22

33

44
class NgClassAttrDirective {
5-
String expression;
6-
dom.Element node;
7-
8-
NgClassAttrDirective(dom.Node this.node, DirectiveValue value) {
9-
expression = value.value;
10-
}
11-
12-
attach(Scope scope) {
13-
scope.$watch(expression, (current, previous, __) {
5+
NgClassAttrDirective(dom.Node node, NodeAttrs attrs, Scope scope) {
6+
scope.$watch(attrs[this], (current, previous, __) {
147
var previousSet;
158
var currentSet;
169

lib/directives/ng_click.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ part of angular;
22

33

44
class NgClickAttrDirective {
5-
String expression;
6-
dom.Node node;
7-
8-
9-
NgClickAttrDirective(dom.Node this.node, DirectiveValue directiveValue) {
10-
expression = directiveValue.value;
11-
}
12-
13-
attach(Scope scope) {
5+
NgClickAttrDirective(dom.Node node, NodeAttrs attrs, Scope scope) {
6+
var expression = attrs[this];
147
node.onClick.listen((event) => scope.$apply(expression));
158
}
16-
}
9+
}

lib/directives/ng_cloak.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ part of angular;
22

33

44
class NgCloakAttrDirective {
5-
dom.Element node;
6-
7-
NgCloakAttrDirective(dom.Node this.node) {}
8-
9-
attach(Scope scope) {
5+
NgCloakAttrDirective(dom.Node node) {
106
node.attributes.remove('ng-cloak');
117
}
128
}

lib/directives/ng_controller.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ class NgControllerAttrDirective {
99
Injector injector;
1010
BlockList blockList;
1111

12-
NgControllerAttrDirective(DirectiveValue value, Injector this.injector, BlockList this.blockList) {
13-
var match = CTRL_REGEXP.firstMatch(value.value);
12+
NgControllerAttrDirective(NodeAttrs attrs, Injector this.injector, BlockList this.blockList, Scope scope) {
13+
var match = CTRL_REGEXP.firstMatch(attrs[this]);
1414

1515
ctrlSymbol = new Symbol(match.group(1) + 'Controller');
1616
alias = match.group(3);
17-
}
1817

19-
attach(Scope scope) {
20-
var childScope = scope.$new();
21-
var module = new Module();
22-
module.value(Scope, childScope);
18+
scope.$evalAsync(() {
19+
var childScope = scope.$new();
2320

24-
// attach the child scope
25-
blockList.newBlock()..attach(childScope)..insertAfter(blockList);
21+
// attach the child scope
22+
blockList.newBlock(childScope).insertAfter(blockList);
2623

27-
// instantiate the controller
28-
var controller = injector.createChild([module], [ctrlSymbol]).getBySymbol(ctrlSymbol);
24+
// instantiate the controller
25+
var controller = injector.
26+
createChild([new ScopeModule(childScope)], [ctrlSymbol]).
27+
getBySymbol(ctrlSymbol);
2928

30-
// publish the controller into the scope
31-
if (alias != null) {
32-
childScope[alias] = controller;
33-
}
29+
// publish the controller into the scope
30+
if (alias != null) {
31+
childScope[alias] = controller;
32+
}
33+
});
3434
}
3535
}

lib/directives/ng_hide.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@ part of angular;
44
class NgHideAttrDirective {
55
static String NG_HIDE_CLASS = 'ng-hide';
66

7-
String expression;
8-
dom.Element node;
9-
10-
NgHideAttrDirective(dom.Node this.node, DirectiveValue value) {
11-
expression = value.value;
12-
}
13-
14-
attach(Scope scope) {
15-
scope.$watch(expression, (value, _, __) {
7+
NgHideAttrDirective(dom.Node node, NodeAttrs attrs, Scope scope) {
8+
scope.$watch(attrs[this], (value) {
169
if (value != null && toBool(value)) {
1710
node.classes.add(NG_HIDE_CLASS);
1811
} else {

lib/directives/ng_if.dart

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@ part of angular;
44
class NgIfAttrDirective {
55
static String $transclude = 'element';
66

7-
String expression;
8-
dom.Element node;
9-
Injector injector;
10-
BlockList blockList;
11-
12-
NgIfAttrDirective(DirectiveValue value, Injector this.injector, BlockList this.blockList, dom.Node this.node) {
13-
expression = value.value;
14-
}
15-
16-
attach(Scope scope) {
17-
// TODO(vojta): detach the scope when block is removed
7+
NgIfAttrDirective(NodeAttrs attrs, Injector injector, BlockList blockList, dom.Node node, Scope scope) {
188
var childScope = scope.$new();
19-
var block = blockList.newBlock();
9+
var block = blockList.newBlock(childScope);
2010
var isInserted = false;
2111

22-
block.attach(childScope);
23-
24-
scope.$watch(expression, (value, _, __) {
12+
scope.$watch(attrs[this], (value) {
2513
// TODO(vojta): ignore changes like null -> false
2614
if (value != null && toBool(value)) {
2715
if (!isInserted) {

lib/directives/ng_mustache.dart

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,24 @@ class NgTextMustacheDirective {
66
dom.Node element;
77
ParsedFn interpolateFn;
88

9-
NgTextMustacheDirective(dom.Node this.element, DirectiveValue value, Interpolate interpolate) {
10-
interpolateFn = interpolate(value.value);
9+
NgTextMustacheDirective(dom.Node this.element, NodeAttrs attrs, Interpolate interpolate, Scope scope) {
10+
interpolateFn = interpolate(attrs[this]);
1111
element.text = '';
12-
}
13-
14-
attach(Scope scope) {
1512
scope.$watch(interpolateFn, (text) => element.text = text);
1613
}
14+
1715
}
1816

1917
class NgAttrMustacheDirective {
2018
static String $selector = r'[*=/{{.*}}/]';
2119
static RegExp ATTR_NAME_VALUE_REGEXP = new RegExp(r'^([^=]+)=(.*)$');
2220

23-
ParsedFn interpolateFn;
24-
dom.Element element;
25-
Function attrSetter;
26-
27-
NgAttrMustacheDirective(dom.Element this.element, DirectiveValue value, Interpolate interpolate) {
28-
var match = ATTR_NAME_VALUE_REGEXP.firstMatch(value.value);
21+
NgAttrMustacheDirective(dom.Element element, NodeAttrs attrs, Interpolate interpolate, Scope scope) {
22+
var match = ATTR_NAME_VALUE_REGEXP.firstMatch(attrs[this]);
2923
var attrName = match[1];
30-
interpolateFn = interpolate(match[2]);
31-
attrSetter = (text) => element.attributes[attrName] = text;
24+
ParsedFn interpolateFn = interpolate(match[2]);
25+
Function attrSetter = (text) => element.attributes[attrName] = text;
3226
attrSetter('');
33-
}
34-
35-
attach(Scope scope) {
3627
scope.$watch(interpolateFn, attrSetter);
3728
}
3829
}

lib/directives/ng_repeat.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class NgRepeatAttrDirective {
3232
NgRepeatAttrDirective(BlockListFactory blockListFactory,
3333
BlockList this.blockList,
3434
dom.Node node,
35-
DirectiveValue value) {
36-
expression = value.value;
35+
NodeAttrs attrs,
36+
Scope scope) {
37+
expression = attrs[this];
3738
Match match = SYNTAX.firstMatch(expression);
3839
if (match == null) {
3940
throw "[NgErr7] ngRepeat error! Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '$expression'.";
@@ -47,9 +48,7 @@ class NgRepeatAttrDirective {
4748
valueIdentifier = match.group(3);
4849
if (valueIdentifier == null) valueIdentifier = match.group(1);
4950
keyIdentifier = match.group(2);
50-
}
5151

52-
attach(Scope scope) {
5352
scope.$watchCollection(listExpr, (collection) {
5453
var previousNode = blockList.elements[0], // current position of the node
5554
nextNode,
@@ -129,14 +128,13 @@ class NgRepeatAttrDirective {
129128

130129
if (row.startNode == null) {
131130
newRows[row.id] = row;
132-
var block = blockList.newBlock();
131+
var block = blockList.newBlock(childScope);
133132
row.block = block;
134133
row.scope = childScope;
135134
row.elements = block.elements;
136135
row.startNode = row.elements[0];
137136
row.endNode = row.elements[row.elements.length - 1];
138137
block.insertAfter(cursor);
139-
block.attach(childScope);
140138
}
141139
cursor = row.block;
142140
}

0 commit comments

Comments
 (0)