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

Commit ad27b2d

Browse files
vicbrkirov
authored andcommitted
refactor(DirectiveInjector): better var names and comments
1 parent e2350ca commit ad27b2d

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

lib/core_dom/directive_injector.dart

+13-11
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ EventHandler eventHandler(DirectiveInjector di) => di._eventHandler;
6767

6868
class DirectiveInjector implements DirectiveBinder {
6969
static bool _isInit = false;
70+
7071
static initUID() {
7172
if (_isInit) return;
7273
_isInit = true;
@@ -94,6 +95,7 @@ class DirectiveInjector implements DirectiveBinder {
9495
if (_KEYS[i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS} at $i';
9596
}
9697
}
98+
9799
static List<Key> _KEYS =
98100
[ UNDEFINED_ID
99101
, INJECTOR_KEY
@@ -133,6 +135,7 @@ class DirectiveInjector implements DirectiveBinder {
133135
NgElement _ngElement;
134136
ElementProbe _elementProbe;
135137

138+
// Keys, instances, parameter keys and factory functions
136139
Key _key0 = null; dynamic _obj0; List<Key> _pKeys0; Function _factory0;
137140
Key _key1 = null; dynamic _obj1; List<Key> _pKeys1; Function _factory1;
138141
Key _key2 = null; dynamic _obj2; List<Key> _pKeys2; Function _factory2;
@@ -153,11 +156,11 @@ class DirectiveInjector implements DirectiveBinder {
153156
@Deprecated("Deprecated. Use getFromParent instead.")
154157
Object get parent => this._parent;
155158

156-
static _toVisId(Visibility v) => identical(v, Visibility.LOCAL)
159+
static _toVisibilityId(Visibility v) => identical(v, Visibility.LOCAL)
157160
? VISIBILITY_LOCAL
158161
: (identical(v, Visibility.CHILDREN) ? VISIBILITY_CHILDREN : VISIBILITY_DIRECT_CHILD);
159162

160-
static _toVis(int id) {
163+
static Visibility _toVisibility(int id) {
161164
switch (id) {
162165
case VISIBILITY_LOCAL: return Visibility.LOCAL;
163166
case VISIBILITY_DIRECT_CHILD: return Visibility.DIRECT_CHILD;
@@ -181,7 +184,7 @@ class DirectiveInjector implements DirectiveBinder {
181184
toInstanceOf,
182185
inject: const[],
183186
Visibility visibility: Visibility.LOCAL}) {
184-
if (key == null) throw 'Key is required';
187+
assert(key != null);
185188
if (key is! Key) key = new Key(key);
186189
if (inject is! List) inject = [inject];
187190

@@ -193,13 +196,13 @@ class DirectiveInjector implements DirectiveBinder {
193196

194197
void bindByKey(Key key, Function factory, List<Key> parameterKeys, [Visibility visibility]) {
195198
if (visibility == null) visibility = Visibility.CHILDREN;
196-
int visibilityId = _toVisId(visibility);
199+
int visibilityId = _toVisibilityId(visibility);
197200
int keyVisId = key.uid;
198201
if (keyVisId != visibilityId) {
199202
if (keyVisId == null) {
200203
key.uid = visibilityId;
201204
} else {
202-
throw "Can not set $visibility on $key, it alread has ${_toVis(keyVisId)}";
205+
throw "Can not set $visibility on $key, it already has ${_toVisibility(keyVisId)}";
203206
}
204207
}
205208
if (_key0 == null || identical(_key0, key)) { _key0 = key; _pKeys0 = parameterKeys; _factory0 = factory; }
@@ -249,17 +252,17 @@ class DirectiveInjector implements DirectiveBinder {
249252
}
250253
}
251254

252-
num _getDepth(int visType) {
253-
switch(visType) {
255+
num _getDepth(int visibility) {
256+
switch(visibility) {
254257
case VISIBILITY_LOCAL: return 0;
255258
case VISIBILITY_DIRECT_CHILD: return 1;
256259
case VISIBILITY_CHILDREN: return _MAX_TREE_DEPTH;
257260
default: throw null;
258261
}
259262
}
260263

261-
_getDirectiveByKey(Key k, int visType, Injector appInjector) {
262-
num treeDepth = _getDepth(visType);
264+
_getDirectiveByKey(Key k, int visibility, Injector appInjector) {
265+
num treeDepth = _getDepth(visibility);
263266
// ci stands for currentInjector, abbreviated for readability.
264267
var ci = this;
265268
while (ci != null && treeDepth >= 0) {
@@ -376,7 +379,6 @@ class DirectiveInjector implements DirectiveBinder {
376379
return obj;
377380
}
378381

379-
380382
ElementProbe get elementProbe {
381383
if (_elementProbe == null) {
382384
ElementProbe parentProbe = _parent == null ? null : _parent.elementProbe;
@@ -475,7 +477,7 @@ class ComponentDirectiveInjector extends DirectiveInjector {
475477

476478
// Add 1 to visibility to allow to skip over current component injector.
477479
// For example, a local directive is visible from its component injector children.
478-
num _getDepth(int visType) => super._getDepth(visType) + 1;
480+
num _getDepth(int visibility) => super._getDepth(visibility) + 1;
479481
}
480482

481483

lib/core_dom/view.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ViewPort {
5454
_parentView.addViewPort(this);
5555
}
5656

57-
View insertNew(ViewFactory viewFactory, { View insertAfter, Scope viewScope}) {
57+
View insertNew(ViewFactory viewFactory, {View insertAfter, Scope viewScope}) {
5858
if (viewScope == null) viewScope = scope.createChild(new PrototypeMap(scope.context));
5959
View view = viewFactory.call(viewScope, directiveInjector);
6060
return insert(view, insertAfter: insertAfter);

lib/directive/ng_include.dart

+5-8
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,28 @@ class NgInclude {
2727
final DirectiveMap directives;
2828

2929
View _view;
30-
Scope _scope;
30+
Scope _childScope;
3131

3232
NgInclude(this.element, this.scope, this.viewCache,
3333
this.directiveInjector, this.directives);
3434

3535
_cleanUp() {
3636
if (_view == null) return;
37-
3837
_view.nodes.forEach((node) => node.remove);
39-
_scope.destroy();
38+
_childScope.destroy();
39+
_childScope = null;
4040
element.innerHtml = '';
41-
4241
_view = null;
43-
_scope = null;
4442
}
4543

4644
_updateContent(ViewFactory viewFactory) {
4745
// create a new scope
48-
_scope = scope.createChild(new PrototypeMap(scope.context));
49-
_view = viewFactory(_scope, directiveInjector);
46+
_childScope = scope.createChild(new PrototypeMap(scope.context));
47+
_view = viewFactory(_childScope, directiveInjector);
5048

5149
_view.nodes.forEach((node) => element.append(node));
5250
}
5351

54-
5552
set url(value) {
5653
_cleanUp();
5754
if (value != null && value != '') {

test/core_dom/directive_injector_spec.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void main() {
179179
it('should not allow reseting visibility', () {
180180
addDirective(_Type0, Visibility.LOCAL);
181181
expect(() => addDirective(_Type0, Visibility.DIRECT_CHILD)).toThrow(
182-
'Can not set Visibility: DIRECT_CHILD on _Type0, it alread has Visibility: LOCAL');
182+
'Can not set Visibility: DIRECT_CHILD on _Type0, it already has Visibility: LOCAL');
183183
});
184184

185185
it('should allow child injector to see types declared at parent injector', () {

0 commit comments

Comments
 (0)