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

Commit 9f46fb9

Browse files
vicbjbdeboer
authored andcommitted
feat(directive injector): DiCircularDependencyError -> _CircularDependencyError
And make _CircularDependencyError extends CircularDependencyError so that catching CircularDependencyError catches any circular dep error Closes #1399
1 parent 09d3c83 commit 9f46fb9

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

lib/core_dom/directive_injector.dart

+23-17
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ final SOURCE_LIGHT_DOM_KEY = new Key(SourceLightDom);
2626
final TEMPLATE_LOADER_KEY = new Key(TemplateLoader);
2727
final SHADOW_ROOT_KEY = new Key(ShadowRoot);
2828

29-
final int NO_CONSTRUCTION = 0;
29+
const int _NO_CONSTRUCTION = 0;
3030

3131
// Maximum parent directive injectors that would be traversed.
32-
final int MAX_TREE_DEPTH = 1 << 30;
32+
const int _MAX_TREE_DEPTH = 1 << 30;
3333

3434
// Maximum number of concurrent constructions a directive injector would
3535
// support before throwing an error.
36-
final int MAX_CONSTRUCTION_DEPTH = 50;
36+
const int _MAX_CONSTRUCTION_DEPTH = 50;
3737

3838
const int VISIBILITY_LOCAL = -1;
3939
const int VISIBILITY_DIRECT_CHILD = -2;
@@ -114,7 +114,7 @@ class DirectiveInjector implements DirectiveBinder {
114114
, COMPONENT_DIRECTIVE_INJECTOR_KEY
115115
, KEEP_ME_LAST
116116
];
117-
117+
118118
final DirectiveInjector _parent;
119119
final Injector _appInjector;
120120
final Node _node;
@@ -168,7 +168,7 @@ class DirectiveInjector implements DirectiveBinder {
168168
: _parent = parent,
169169
_appInjector = appInjector,
170170
_view = view == null && parent != null ? parent._view : view,
171-
_constructionDepth = NO_CONSTRUCTION;
171+
_constructionDepth = _NO_CONSTRUCTION;
172172

173173
DirectiveInjector._default(this._parent, this._appInjector)
174174
: _node = null,
@@ -177,7 +177,7 @@ class DirectiveInjector implements DirectiveBinder {
177177
scope = null,
178178
_view = null,
179179
_animate = null,
180-
_constructionDepth = NO_CONSTRUCTION;
180+
_constructionDepth = _NO_CONSTRUCTION;
181181

182182
void bind(key, {dynamic toValue: DEFAULT_VALUE,
183183
Function toFactory: DEFAULT_VALUE,
@@ -257,7 +257,7 @@ class DirectiveInjector implements DirectiveBinder {
257257
switch(visType) {
258258
case VISIBILITY_LOCAL: return 0;
259259
case VISIBILITY_DIRECT_CHILD: return 1;
260-
case VISIBILITY_CHILDREN: return MAX_TREE_DEPTH;
260+
case VISIBILITY_CHILDREN: return _MAX_TREE_DEPTH;
261261
default: throw null;
262262
}
263263
}
@@ -322,11 +322,11 @@ class DirectiveInjector implements DirectiveBinder {
322322
}
323323

324324
dynamic _new(Key k, List<Key> paramKeys, Function fn) {
325-
if (_constructionDepth > MAX_CONSTRUCTION_DEPTH) {
326-
_constructionDepth = NO_CONSTRUCTION;
327-
throw new DiCircularDependencyError(k);
325+
if (_constructionDepth > _MAX_CONSTRUCTION_DEPTH) {
326+
_constructionDepth = _NO_CONSTRUCTION;
327+
throw new _CircularDependencyError(k);
328328
}
329-
bool isFirstConstruction = (_constructionDepth++ == NO_CONSTRUCTION);
329+
bool isFirstConstruction = (_constructionDepth++ == _NO_CONSTRUCTION);
330330
var oldTag = _TAG_GET.makeCurrent();
331331
int size = paramKeys.length;
332332
var obj;
@@ -375,7 +375,7 @@ class DirectiveInjector implements DirectiveBinder {
375375
}
376376
}
377377
oldTag.makeCurrent();
378-
if (isFirstConstruction) _constructionDepth = NO_CONSTRUCTION;
378+
if (isFirstConstruction) _constructionDepth = _NO_CONSTRUCTION;
379379
return obj;
380380
}
381381

@@ -433,7 +433,7 @@ class TemplateDirectiveInjector extends DirectiveInjector {
433433
if (_destLightDom != null) _destLightDom.addViewPort(viewPort);
434434
return viewPort;
435435
}
436-
436+
437437
}
438438

439439
class ComponentDirectiveInjector extends DirectiveInjector {
@@ -478,8 +478,8 @@ class ComponentDirectiveInjector extends DirectiveInjector {
478478

479479
// For efficiency we run through the maximum resolving depth and unwind
480480
// instead of setting 'resolving' key per type.
481-
class DiCircularDependencyError extends ResolvingError {
482-
DiCircularDependencyError(Key key) : super(key);
481+
class _CircularDependencyError extends CircularDependencyError {
482+
_CircularDependencyError(key) : super(key);
483483

484484
// strips the cyclical part of the chain.
485485
List<Key> get stripCycle {
@@ -494,6 +494,12 @@ class DiCircularDependencyError extends ResolvingError {
494494
return rkeys;
495495
}
496496

497-
String get resolveChain => stripCycle.join(' -> ');
498-
String toString() => "circular dependency (${resolveChain})";
497+
String get resolveChain {
498+
StringBuffer buffer = new StringBuffer()
499+
..write("(resolving ")
500+
..write(stripCycle.join(' -> '))
501+
..write(")");
502+
return buffer.toString();
503+
}
504+
499505
}

test/core_dom/directive_injector_spec.dart

+12-4
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ void main() {
113113
addDirective(_TypeC0);
114114
addDirective(_TypeC1, Visibility.CHILDREN);
115115
addDirective(_TypeC2, Visibility.CHILDREN);
116-
expect(() => injector.get(_TypeC0)).toThrow(
117-
'circular dependency (_TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
116+
expect(() => injector.get(_TypeC0)).toThrowWith(
117+
where: (e) {
118+
expect(e is CircularDependencyError).toBeTrue();
119+
},
120+
message: 'Cannot resolve a circular dependency! '
121+
'(resolving _TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
118122
});
119123

120124
it('should throw circular dependency error accross injectors', () {
@@ -124,8 +128,12 @@ void main() {
124128
addDirective(_TypeC0, Visibility.LOCAL, childInjector);
125129
addDirective(_TypeC1, Visibility.CHILDREN);
126130
addDirective(_TypeC2, Visibility.CHILDREN);
127-
expect(() => childInjector.get(_TypeC0)).toThrow(
128-
'circular dependency (_TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
131+
expect(() => childInjector.get(_TypeC0)).toThrowWith(
132+
where: (e) {
133+
expect(e is CircularDependencyError).toBeTrue();
134+
},
135+
message: 'Cannot resolve a circular dependency! '
136+
'(resolving _TypeC0 -> _TypeC1 -> _TypeC2 -> _TypeC1)');
129137
});
130138
});
131139

0 commit comments

Comments
 (0)