Skip to content

Commit 5e373ff

Browse files
vicbrkirov
authored andcommitted
refactor(DirectiveInjector): better var names and comments
1 parent c18a8f3 commit 5e373ff

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

lib/core_dom/directive_injector.dart

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

6666
class DirectiveInjector implements DirectiveBinder {
6767
static bool _isInit = false;
68+
6869
static initUID() {
6970
if (_isInit) return;
7071
_isInit = true;
@@ -91,6 +92,7 @@ class DirectiveInjector implements DirectiveBinder {
9192
if (_KEYS[i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS} at $i';
9293
}
9394
}
95+
9496
static List<Key> _KEYS =
9597
[ UNDEFINED_ID
9698
, INJECTOR_KEY
@@ -128,6 +130,7 @@ class DirectiveInjector implements DirectiveBinder {
128130
NgElement _ngElement;
129131
ElementProbe _elementProbe;
130132

133+
// Keys, instances, parameter keys and factory functions
131134
Key _key0 = null; dynamic _obj0; List<Key> _pKeys0; Function _factory0;
132135
Key _key1 = null; dynamic _obj1; List<Key> _pKeys1; Function _factory1;
133136
Key _key2 = null; dynamic _obj2; List<Key> _pKeys2; Function _factory2;
@@ -148,11 +151,11 @@ class DirectiveInjector implements DirectiveBinder {
148151
@Deprecated("Deprecated. Use getFromParent instead.")
149152
Object get parent => this._parent;
150153

151-
static _toVisId(Visibility v) => identical(v, Visibility.LOCAL)
154+
static _toVisibilityId(Visibility v) => identical(v, Visibility.LOCAL)
152155
? VISIBILITY_LOCAL
153156
: (identical(v, Visibility.CHILDREN) ? VISIBILITY_CHILDREN : VISIBILITY_DIRECT_CHILD);
154157

155-
static _toVis(int id) {
158+
static Visibility _toVisibility(int id) {
156159
switch (id) {
157160
case VISIBILITY_LOCAL: return Visibility.LOCAL;
158161
case VISIBILITY_DIRECT_CHILD: return Visibility.DIRECT_CHILD;
@@ -185,7 +188,7 @@ class DirectiveInjector implements DirectiveBinder {
185188
toInstanceOf,
186189
inject: const[],
187190
Visibility visibility: Visibility.LOCAL}) {
188-
if (key == null) throw 'Key is required';
191+
assert(key != null);
189192
if (key is! Key) key = new Key(key);
190193
if (inject is! List) inject = [inject];
191194

@@ -197,13 +200,13 @@ class DirectiveInjector implements DirectiveBinder {
197200

198201
void bindByKey(Key key, Function factory, List<Key> parameterKeys, [Visibility visibility]) {
199202
if (visibility == null) visibility = Visibility.CHILDREN;
200-
int visibilityId = _toVisId(visibility);
203+
int visibilityId = _toVisibilityId(visibility);
201204
int keyVisId = key.uid;
202205
if (keyVisId != visibilityId) {
203206
if (keyVisId == null) {
204207
key.uid = visibilityId;
205208
} else {
206-
throw "Can not set $visibility on $key, it alread has ${_toVis(keyVisId)}";
209+
throw "Can not set $visibility on $key, it already has ${_toVisibility(keyVisId)}";
207210
}
208211
}
209212
if (_key0 == null || identical(_key0, key)) { _key0 = key; _pKeys0 = parameterKeys; _factory0 = factory; }
@@ -253,17 +256,17 @@ class DirectiveInjector implements DirectiveBinder {
253256
}
254257
}
255258

256-
num _getDepth(int visType) {
257-
switch(visType) {
259+
num _getDepth(int visibility) {
260+
switch(visibility) {
258261
case VISIBILITY_LOCAL: return 0;
259262
case VISIBILITY_DIRECT_CHILD: return 1;
260263
case VISIBILITY_CHILDREN: return _MAX_TREE_DEPTH;
261264
default: throw null;
262265
}
263266
}
264267

265-
_getDirectiveByKey(Key k, int visType, Injector appInjector) {
266-
num treeDepth = _getDepth(visType);
268+
_getDirectiveByKey(Key k, int visibility, Injector appInjector) {
269+
num treeDepth = _getDepth(visibility);
267270
// ci stands for currentInjector, abbreviated for readability.
268271
var ci = this;
269272
while (ci != null && treeDepth >= 0) {
@@ -379,7 +382,6 @@ class DirectiveInjector implements DirectiveBinder {
379382
return obj;
380383
}
381384

382-
383385
ElementProbe get elementProbe {
384386
if (_elementProbe == null) {
385387
ElementProbe parentProbe = _parent == null ? null : _parent.elementProbe;
@@ -472,7 +474,7 @@ class ComponentDirectiveInjector extends DirectiveInjector {
472474

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

478480

lib/core_dom/shadow_dom_component_factory.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ class _ComponentAssetKey {
194194

195195
_ComponentAssetKey(String tag, String assetUrl)
196196
: _key = "$tag|$assetUrl",
197-
this.tag = tag,
198-
this.assetUrl = assetUrl;
197+
tag = tag,
198+
assetUrl = assetUrl;
199199

200200
@override
201201
String toString() => _key;
@@ -204,9 +204,9 @@ class _ComponentAssetKey {
204204
int get hashCode => _key.hashCode;
205205

206206
bool operator ==(key) =>
207-
key is _ComponentAssetKey
208-
&& tag == key.tag
209-
&& assetUrl == key.assetUrl;
207+
key is _ComponentAssetKey &&
208+
tag == key.tag &&
209+
assetUrl == key.assetUrl;
210210
}
211211

212212
@Injectable()

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
@@ -149,7 +149,7 @@ void main() {
149149
it('should not allow reseting visibility', () {
150150
addDirective(_Type0, Visibility.LOCAL);
151151
expect(() => addDirective(_Type0, Visibility.DIRECT_CHILD)).toThrow(
152-
'Can not set Visibility: DIRECT_CHILD on _Type0, it alread has Visibility: LOCAL');
152+
'Can not set Visibility: DIRECT_CHILD on _Type0, it already has Visibility: LOCAL');
153153
});
154154

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

0 commit comments

Comments
 (0)