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

Commit

Permalink
refactor(DirectiveInjector): better var names and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and rkirov committed Oct 2, 2014
1 parent 15ffa9f commit 79b8df8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
26 changes: 14 additions & 12 deletions lib/core_dom/directive_injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ EventHandler eventHandler(DirectiveInjector di) => di._eventHandler;

class DirectiveInjector implements DirectiveBinder {
static bool _isInit = false;

static initUID() {
if (_isInit) return;
_isInit = true;
Expand Down Expand Up @@ -94,6 +95,7 @@ class DirectiveInjector implements DirectiveBinder {
if (_KEYS[i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS} at $i';
}
}

static List<Key> _KEYS =
[ UNDEFINED_ID
, INJECTOR_KEY
Expand Down Expand Up @@ -133,6 +135,7 @@ class DirectiveInjector implements DirectiveBinder {
NgElement _ngElement;
ElementProbe _elementProbe;

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

static _toVisId(Visibility v) => identical(v, Visibility.LOCAL)
static _toVisibilityId(Visibility v) => identical(v, Visibility.LOCAL)
? VISIBILITY_LOCAL
: (identical(v, Visibility.CHILDREN) ? VISIBILITY_CHILDREN : VISIBILITY_DIRECT_CHILD);

static _toVis(int id) {
static Visibility _toVisibility(int id) {
switch (id) {
case VISIBILITY_LOCAL: return Visibility.LOCAL;
case VISIBILITY_DIRECT_CHILD: return Visibility.DIRECT_CHILD;
Expand All @@ -181,7 +184,7 @@ class DirectiveInjector implements DirectiveBinder {
toInstanceOf,
inject: const[],
Visibility visibility: Visibility.LOCAL}) {
if (key == null) throw 'Key is required';
assert(key != null);
if (key is! Key) key = new Key(key);
if (inject is! List) inject = [inject];

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

void bindByKey(Key key, Function factory, List<Key> parameterKeys, [Visibility visibility]) {
if (visibility == null) visibility = Visibility.CHILDREN;
int visibilityId = _toVisId(visibility);
int visibilityId = _toVisibilityId(visibility);
int keyVisId = key.uid;
if (keyVisId != visibilityId) {
if (keyVisId == null) {
key.uid = visibilityId;
} else {
throw "Can not set $visibility on $key, it alread has ${_toVis(keyVisId)}";
throw "Can not set $visibility on $key, it already has ${_toVisibility(keyVisId)}";
}
}
if (_key0 == null || identical(_key0, key)) { _key0 = key; _pKeys0 = parameterKeys; _factory0 = factory; }
Expand Down Expand Up @@ -249,17 +252,17 @@ class DirectiveInjector implements DirectiveBinder {
}
}

num _getDepth(int visType) {
switch(visType) {
num _getDepth(int visibility) {
switch(visibility) {
case VISIBILITY_LOCAL: return 0;
case VISIBILITY_DIRECT_CHILD: return 1;
case VISIBILITY_CHILDREN: return _MAX_TREE_DEPTH;
default: throw 'Invalid visibility "$visType"';
default: throw 'Invalid visibility "$visibility"';
}
}

_getDirectiveByKey(Key k, int visType, Injector appInjector) {
num treeDepth = _getDepth(visType);
_getDirectiveByKey(Key k, int visibility, Injector appInjector) {
num treeDepth = _getDepth(visibility);
// ci stands for currentInjector, abbreviated for readability.
var ci = this;
while (ci != null && treeDepth >= 0) {
Expand Down Expand Up @@ -376,7 +379,6 @@ class DirectiveInjector implements DirectiveBinder {
return obj;
}


ElementProbe get elementProbe {
if (_elementProbe == null) {
ElementProbe parentProbe = _parent == null ? null : _parent.elementProbe;
Expand Down Expand Up @@ -475,7 +477,7 @@ class ComponentDirectiveInjector extends DirectiveInjector {

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


Expand Down
2 changes: 1 addition & 1 deletion lib/core_dom/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ViewPort {
_parentView.addViewPort(this);
}

View insertNew(ViewFactory viewFactory, { View insertAfter, Scope viewScope}) {
View insertNew(ViewFactory viewFactory, {View insertAfter, Scope viewScope}) {
if (viewScope == null) viewScope = scope.createProtoChild();
View view = viewFactory.call(viewScope, directiveInjector);
return insert(view, insertAfter: insertAfter);
Expand Down
13 changes: 5 additions & 8 deletions lib/directive/ng_include.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,28 @@ class NgInclude {
final DirectiveMap directives;

View _view;
Scope _scope;
Scope _childScope;

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

_cleanUp() {
if (_view == null) return;

_view.nodes.forEach((node) => node.remove);
_scope.destroy();
_childScope.destroy();
_childScope = null;
element.innerHtml = '';

_view = null;
_scope = null;
}

_updateContent(ViewFactory viewFactory) {
// create a new scope
_scope = scope.createProtoChild();
_view = viewFactory(_scope, directiveInjector);
_childScope = scope.createProtoChild();
_view = viewFactory(_childScope, directiveInjector);

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


set url(value) {
_cleanUp();
if (value != null && value != '') {
Expand Down
2 changes: 1 addition & 1 deletion test/core_dom/directive_injector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void main() {
it('should not allow reseting visibility', () {
addDirective(_Type0, Visibility.LOCAL);
expect(() => addDirective(_Type0, Visibility.DIRECT_CHILD)).toThrowWith(
message: 'Can not set Visibility: DIRECT_CHILD on _Type0, it alread has Visibility: LOCAL');
message: 'Can not set Visibility: DIRECT_CHILD on _Type0, it already has Visibility: LOCAL');
});

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

0 comments on commit 79b8df8

Please sign in to comment.