Skip to content

Commit 73b7076

Browse files
committed
refactor(DirectiveInjector)
1 parent f8bbd35 commit 73b7076

6 files changed

+81
-82
lines changed

lib/core/annotation_src.dart

+21-30
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class DirectiveBinder {
1111
Visibility visibility: Visibility.LOCAL});
1212
}
1313

14-
typedef void DirectiveBinderFn(DirectiveBinder module);
14+
typedef void DirectiveBinderFn(DirectiveBinder binder);
1515

1616
RegExp _ATTR_NAME = new RegExp(r'\[([^\]]+)\]$');
1717

@@ -36,18 +36,18 @@ class Visibility {
3636
abstract class Directive {
3737

3838
/// The directive can only be injected to other directives on the same element.
39-
@deprecated // ('Use Visibility.LOCAL instead')
39+
@Deprecated('Use Visibility.LOCAL instead')
4040
static const Visibility LOCAL_VISIBILITY = Visibility.LOCAL;
4141

4242
/// The directive can be injected to other directives on the same or child elements.
43-
@deprecated// ('Use Visibility.CHILDREN instead')
43+
@Deprecated('Use Visibility.CHILDREN instead')
4444
static const Visibility CHILDREN_VISIBILITY = Visibility.CHILDREN;
4545

4646
/**
4747
* The directive on this element can only be injected to other directives
4848
* declared on elements which are direct children of the current element.
4949
*/
50-
@deprecated// ('Use Visibility.DIRECT_CHILD instead')
50+
@Deprecated('Use Visibility.DIRECT_CHILD instead')
5151
static const Visibility DIRECT_CHILDREN_VISIBILITY = Visibility.DIRECT_CHILD;
5252

5353
/**
@@ -91,26 +91,22 @@ abstract class Directive {
9191
static const String IGNORE_CHILDREN = 'ignore';
9292

9393
/**
94-
* A directive/component controller class can be injected into other
95-
* directives/components. This attribute controls whether the
96-
* controller is available to others.
94+
* A directive/component controller class can be injected into other directives/components. This
95+
* attribute controls whether the controller is available to others.
9796
*
98-
* * `local` [Directive.LOCAL_VISIBILITY] - the controller can be injected
99-
* into other directives / components on the same DOM element.
100-
* * `children` [Directive.CHILDREN_VISIBILITY] - the controller can be
101-
* injected into other directives / components on the same or child DOM
102-
* elements.
103-
* * `direct_children` [Directive.DIRECT_CHILDREN_VISIBILITY] - the
104-
* controller can be injected into other directives / components on the
105-
* direct children of the current DOM element.
97+
* * [Visibility.LOCAL] - the controller can be injected into other directives / components on the
98+
* same DOM element.
99+
* * [Visibility.CHILDREN] - the controller can be injected into other directives / components on
100+
* the same or child DOM elements.
101+
* * [Visibility.DIRECT_CHILD] - the controller can be injected into other directives / components
102+
* on the direct children of the current DOM element.
106103
*/
107104
final Visibility visibility;
108105

109106
/**
110-
* A directive/component class can publish types by using a factory
111-
* function to generate a module. The module is then installed into
112-
* the injector at that element. Any types declared in the module then
113-
* become available for injection.
107+
* A directive/component class can publish types by using a factory function to generate a module.
108+
* The module is then installed into the injector at that element. Any types declared in the
109+
* module then become available for injection.
114110
*
115111
* Example:
116112
*
@@ -122,11 +118,10 @@ abstract class Directive {
122118
* binder.bind(SomeTypeA, visibility: Directive.LOCAL_VISIBILITY);
123119
* }
124120
*
125-
* When specifying types, factories or values in the module, notice that
126-
* `Visibility` maps to:
127-
* * [Directive.LOCAL_VISIBILITY]
128-
* * [Directive.CHILDREN_VISIBILITY]
129-
* * [Directive.DIRECT_CHILDREN_VISIBILITY]
121+
* `visibility` is one of:
122+
* * [Visibility.LOCAL]
123+
* * [Visibility.CHILDREN] (default)
124+
* * [Visibility.DIRECT_CHILD]
130125
*/
131126
final DirectiveBinderFn module;
132127

@@ -264,10 +259,8 @@ class Component extends Directive {
264259
/**
265260
* Set the shadow root applyAuthorStyles property. See shadow-DOM
266261
* documentation for further details.
267-
*
268-
* This feature will be removed in Chrome 35.
269262
*/
270-
@deprecated
263+
@Deprecated('in Chrome 35')
271264
bool get applyAuthorStyles {
272265
if (!_applyAuthorStylesDeprecationWarningPrinted && _applyAuthorStyles == true) {
273266
print("WARNING applyAuthorStyles is deprecated in component $selector");
@@ -280,10 +273,8 @@ class Component extends Directive {
280273
/**
281274
* Set the shadow root resetStyleInheritance property. See shadow-DOM
282275
* documentation for further details.
283-
*
284-
* This feature will be removed in Chrome 35.
285276
*/
286-
@deprecated
277+
@Deprecated('in Chrome 35')
287278
bool get resetStyleInheritance {
288279
if (!_resetStyleInheritanceDeprecationWarningPrinted && _resetStyleInheritance == true) {
289280
print("WARNING resetStyleInheritance is deprecated in component $selector");

lib/core_dom/directive_injector.dart

+44-32
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final CONTENT_PORT_KEY = new Key(ContentPort);
2424
final TEMPLATE_LOADER_KEY = new Key(TemplateLoader);
2525
final SHADOW_ROOT_KEY = new Key(ShadowRoot);
2626

27+
// Visibility < 0 for directives
2728
const int VISIBILITY_LOCAL = -1;
2829
const int VISIBILITY_DIRECT_CHILD = -2;
2930
const int VISIBILITY_CHILDREN = -3;
@@ -32,6 +33,7 @@ const int VISIBILITY_COMPONENT_LOCAL = VISIBILITY_LOCAL + VISIBI
3233
const int VISIBILITY_COMPONENT_DIRECT_CHILD = VISIBILITY_DIRECT_CHILD + VISIBILITY_COMPONENT_OFFSET;
3334
const int VISIBILITY_COMPONENT_CHILDREN = VISIBILITY_CHILDREN + VISIBILITY_COMPONENT_OFFSET;
3435

36+
// Visibility >= 0 for predefined types
3537
const int UNDEFINED_ID = 0;
3638
const int INJECTOR_KEY_ID = 1;
3739
const int DIRECTIVE_INJECTOR_KEY_ID = 2;
@@ -54,6 +56,7 @@ const int KEEP_ME_LAST = 18;
5456

5557
class DirectiveInjector implements DirectiveBinder {
5658
static bool _isInit = false;
59+
5760
static initUID() {
5861
if (_isInit) return;
5962
_isInit = true;
@@ -74,10 +77,12 @@ class DirectiveInjector implements DirectiveBinder {
7477
CONTENT_PORT_KEY.uid = CONTENT_PORT_KEY_ID;
7578
EVENT_HANDLER_KEY.uid = EVENT_HANDLER_KEY_ID;
7679
ANIMATE_KEY.uid = ANIMATE_KEY_ID;
80+
// todo(vicb) wrap in an assert (or assign uids in the for loop) ?
7781
for(var i = 1; i < KEEP_ME_LAST; i++) {
7882
if (_KEYS[i].uid != i) throw 'MISSORDERED KEYS ARRAY: ${_KEYS} at $i';
7983
}
8084
}
85+
8186
static List<Key> _KEYS =
8287
[ UNDEFINED_ID
8388
, INJECTOR_KEY
@@ -111,6 +116,7 @@ class DirectiveInjector implements DirectiveBinder {
111116
NgElement _ngElement;
112117
ElementProbe _elementProbe;
113118

119+
// Keys, instances, parameter keys and factory functions
114120
Key _key0 = null; dynamic _obj0; List<Key> _pKeys0; Function _factory0;
115121
Key _key1 = null; dynamic _obj1; List<Key> _pKeys1; Function _factory1;
116122
Key _key2 = null; dynamic _obj2; List<Key> _pKeys2; Function _factory2;
@@ -122,11 +128,11 @@ class DirectiveInjector implements DirectiveBinder {
122128
Key _key8 = null; dynamic _obj8; List<Key> _pKeys8; Function _factory8;
123129
Key _key9 = null; dynamic _obj9; List<Key> _pKeys9; Function _factory9;
124130

125-
static _toVisId(Visibility v) => identical(v, Visibility.LOCAL)
131+
static int _toVisibilityId(Visibility v) => identical(v, Visibility.LOCAL)
126132
? VISIBILITY_LOCAL
127133
: (identical(v, Visibility.CHILDREN) ? VISIBILITY_CHILDREN : VISIBILITY_DIRECT_CHILD);
128134

129-
static _toVis(int id) {
135+
static Visibility _toVisibility(int id) {
130136
switch (id) {
131137
case VISIBILITY_LOCAL: return Visibility.LOCAL;
132138
case VISIBILITY_DIRECT_CHILD: return Visibility.DIRECT_CHILD;
@@ -158,7 +164,7 @@ class DirectiveInjector implements DirectiveBinder {
158164
toInstanceOf,
159165
inject: const[],
160166
Visibility visibility: Visibility.LOCAL}) {
161-
if (key == null) throw 'Key is required';
167+
assert(key != null);
162168
if (key is! Key) key = new Key(key);
163169
if (inject is! List) inject = [inject];
164170

@@ -170,13 +176,13 @@ class DirectiveInjector implements DirectiveBinder {
170176

171177
void bindByKey(Key key, Function factory, List<Key> parameterKeys, [Visibility visibility]) {
172178
if (visibility == null) visibility = Visibility.CHILDREN;
173-
int visibilityId = _toVisId(visibility);
179+
int visibilityId = _toVisibilityId(visibility);
174180
int keyVisId = key.uid;
175181
if (keyVisId != visibilityId) {
176182
if (keyVisId == null) {
177183
key.uid = visibilityId;
178184
} else {
179-
throw "Can not set $visibility on $key, it alread has ${_toVis(keyVisId)}";
185+
throw "Can not set $visibility on $key, it already has ${_toVisibility(keyVisId)}";
180186
}
181187
}
182188
if (_key0 == null || identical(_key0, key)) { _key0 = key; _pKeys0 = parameterKeys; _factory0 = factory; }
@@ -213,7 +219,7 @@ class DirectiveInjector implements DirectiveBinder {
213219
return isDirective ? _getDirectiveByKey(key, uid, appInjector) : _getById(uid);
214220
}
215221

216-
Object _getDirectiveByKey(Key k, int visType, Injector i) {
222+
Object _getDirectiveByKey(Key k, int visibility, Injector i) {
217223
do {
218224
if (_key0 == null) break; if (identical(_key0, k)) return _obj0 == null ? _obj0 = _new(_pKeys0, _factory0) : _obj0;
219225
if (_key1 == null) break; if (identical(_key1, k)) return _obj1 == null ? _obj1 = _new(_pKeys1, _factory1) : _obj1;
@@ -226,7 +232,7 @@ class DirectiveInjector implements DirectiveBinder {
226232
if (_key8 == null) break; if (identical(_key8, k)) return _obj8 == null ? _obj8 = _new(_pKeys8, _factory8) : _obj8;
227233
if (_key9 == null) break; if (identical(_key9, k)) return _obj9 == null ? _obj9 = _new(_pKeys9, _factory9) : _obj9;
228234
} while (false);
229-
switch (visType) {
235+
switch (visibility) {
230236
case VISIBILITY_LOCAL: return appInjector.getByKey(k);
231237
case VISIBILITY_DIRECT_CHILD: return parent._getDirectiveByKey(k, VISIBILITY_LOCAL, i);
232238
case VISIBILITY_CHILDREN: return parent._getDirectiveByKey(k, VISIBILITY_CHILDREN, i);
@@ -321,7 +327,6 @@ class DirectiveInjector implements DirectiveBinder {
321327
return obj;
322328
}
323329

324-
325330
ElementProbe get elementProbe {
326331
if (_elementProbe == null) {
327332
ElementProbe parentProbe = parent is DirectiveInjector ? parent.elementProbe : null;
@@ -344,15 +349,15 @@ class TemplateDirectiveInjector extends DirectiveInjector {
344349
BoundViewFactory _boundViewFactory;
345350

346351
TemplateDirectiveInjector(DirectiveInjector parent, Injector appInjector,
347-
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
348-
Scope scope, Animate animate, this._viewFactory)
352+
Node node, NodeAttrs nodeAttrs, EventHandler eventHandler,
353+
Scope scope, Animate animate, this._viewFactory)
349354
: super(parent, appInjector, node, nodeAttrs, eventHandler, scope, animate);
350355

351356

352357
Object _getById(int keyId) {
353358
switch(keyId) {
354359
case VIEW_FACTORY_KEY_ID: return _viewFactory;
355-
case VIEW_PORT_KEY_ID: return ((_viewPort) == null) ?
360+
case VIEW_PORT_KEY_ID: return (_viewPort == null) ?
356361
_viewPort = new ViewPort(this, scope, _node, _animate) : _viewPort;
357362
case BOUND_VIEW_FACTORY_KEY_ID: return (_boundViewFactory == null) ?
358363
_boundViewFactory = _viewFactory.bind(this.parent) : _boundViewFactory;
@@ -363,56 +368,59 @@ class TemplateDirectiveInjector extends DirectiveInjector {
363368
}
364369

365370
abstract class ComponentDirectiveInjector extends DirectiveInjector {
366-
367371
final TemplateLoader _templateLoader;
368372
final ShadowRoot _shadowRoot;
373+
final Key _typeKey;
369374

370375
ComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
371-
EventHandler eventHandler, Scope scope,
372-
this._templateLoader, this._shadowRoot)
373-
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, scope,
376+
EventHandler eventHandler, this._templateLoader, this._shadowRoot,
377+
this._typeKey)
378+
: super(parent, appInjector, parent._node, parent._nodeAttrs, eventHandler, null,
374379
parent._animate);
375380

376381
Object _getById(int keyId) {
377382
switch(keyId) {
378383
case TEMPLATE_LOADER_KEY_ID: return _templateLoader;
379384
case SHADOW_ROOT_KEY_ID: return _shadowRoot;
385+
case SCOPE_KEY_ID:
386+
if (scope == null) {
387+
Scope parentScope = parent is DirectiveInjector ?
388+
parent.scope :
389+
parent.getByKey(SCOPE_KEY);
390+
scope = parentScope.createChild(getByKey(_typeKey));
391+
}
392+
return scope;
380393
default: return super._getById(keyId);
381394
}
382395
}
383396

384-
_getDirectiveByKey(Key k, int visType, Injector i) =>
385-
super._getDirectiveByKey(k, visType + VISIBILITY_COMPONENT_OFFSET, i);
397+
Object _getDirectiveByKey(Key k, int visibility, Injector i) =>
398+
super._getDirectiveByKey(k, visibility + VISIBILITY_COMPONENT_OFFSET, i);
386399
}
387400

388401
class ShadowlessComponentDirectiveInjector extends ComponentDirectiveInjector {
389402
final ContentPort _contentPort;
390403

391404
ShadowlessComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
392-
EventHandler eventHandler, Scope scope,
393-
templateLoader, shadowRoot, this._contentPort)
394-
: super(parent, appInjector, eventHandler, scope, templateLoader, shadowRoot);
405+
EventHandler eventHandler, templateLoader, shadowRoot,
406+
this._contentPort, Key typeKey)
407+
: super(parent, appInjector, eventHandler, templateLoader, shadowRoot, typeKey);
395408

396-
Object _getById(int keyId) {
397-
switch(keyId) {
398-
case CONTENT_PORT_KEY_ID: return _contentPort;
399-
default: return super._getById(keyId);
400-
}
401-
}
409+
Object _getById(int keyId) => keyId == CONTENT_PORT_KEY_ID ? _contentPort : super._getById(keyId);
402410
}
403411

404412
class ShadowDomComponentDirectiveInjector extends ComponentDirectiveInjector {
405413
ShadowDomComponentDirectiveInjector(DirectiveInjector parent, Injector appInjector,
406-
Scope scope, templateLoader, shadowRoot)
414+
templateLoader, shadowRoot, Key typeKey)
407415
: super(parent, appInjector, new ShadowRootEventHandler(shadowRoot,
408416
parent.getByKey(EXPANDO_KEY),
409417
parent.getByKey(EXCEPTION_HANDLER_KEY)),
410-
scope, templateLoader, shadowRoot);
418+
templateLoader, shadowRoot, typeKey);
411419

412420
ElementProbe get elementProbe {
413421
if (_elementProbe == null) {
414422
ElementProbe parentProbe =
415-
parent is DirectiveInjector ? parent.elementProbe : parent.getByKey(ELEMENT_PROBE_KEY);
423+
parent is DirectiveInjector ? parent.elementProbe : parent.getByKey(ELEMENT_PROBE_KEY);
416424
_elementProbe = new ElementProbe(parentProbe, _shadowRoot, this, scope);
417425
}
418426
return _elementProbe;
@@ -422,12 +430,16 @@ class ShadowDomComponentDirectiveInjector extends ComponentDirectiveInjector {
422430
@Injectable()
423431
class DefaultDirectiveInjector extends DirectiveInjector {
424432
DefaultDirectiveInjector(Injector appInjector): super._default(null, appInjector);
433+
425434
DefaultDirectiveInjector.newAppInjector(DirectiveInjector parent, Injector appInjector)
426-
: super._default(parent, appInjector);
435+
: super._default(parent, appInjector);
436+
427437

428438
Object getByKey(Key key) => appInjector.getByKey(key);
429-
_getDirectiveByKey(Key key, int visType, Injector i) =>
430-
parent == null ? i.getByKey(key) : parent._getDirectiveByKey(key, visType, i);
439+
440+
_getDirectiveByKey(Key key, int visibility, Injector i) =>
441+
parent == null ? i.getByKey(key) : parent._getDirectiveByKey(key, visibility, i);
442+
431443
_getById(int keyId) {
432444
switch (keyId) {
433445
case CONTENT_PORT_KEY_ID: return null;

lib/core_dom/shadow_dom_component_factory.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ class _ComponentAssetKey {
190190

191191
_ComponentAssetKey(String tag, String assetUrl)
192192
: _key = "$tag|$assetUrl",
193-
this.tag = tag,
194-
this.assetUrl = assetUrl;
193+
tag = tag,
194+
assetUrl = assetUrl;
195195

196196
@override
197197
String toString() => _key;
@@ -200,9 +200,9 @@ class _ComponentAssetKey {
200200
int get hashCode => _key.hashCode;
201201

202202
bool operator ==(key) =>
203-
key is _ComponentAssetKey
204-
&& tag == key.tag
205-
&& assetUrl == key.assetUrl;
203+
key is _ComponentAssetKey &&
204+
tag == key.tag &&
205+
assetUrl == key.assetUrl;
206206
}
207207

208208
@Injectable()

lib/core_dom/transcluding_component_factory.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
113113
DirectiveInjector childInjector;
114114
var childInjectorCompleter; // Used if the ViewFuture is available before the childInjector.
115115

116-
var component = _component;
117116
var contentPort = new ContentPort(element);
118117

119118
// Append the component's template as children
@@ -143,8 +142,9 @@ class BoundTranscludingComponentFactory implements BoundComponentFactory {
143142
Scope shadowScope = scope.createChild(new HashMap());
144143

145144
childInjector = new ShadowlessComponentDirectiveInjector(injector, injector.appInjector,
146-
eventHandler, shadowScope, templateLoader, new ShadowlessShadowRoot(element),
147-
contentPort);
145+
eventHandler, templateLoader, new ShadowlessShadowRoot(element), contentPort,
146+
_ref.typeKey);
147+
148148
childInjector.bindByKey(_ref.typeKey, _ref.factory, _ref.paramKeys, _ref.annotation.visibility);
149149

150150
if (childInjectorCompleter != null) {

lib/core_dom/view.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class ViewPort {
3636

3737
ViewPort(this.directiveInjector, this.scope, this.placeholder, this._animate);
3838

39-
View insertNew(ViewFactory viewFactory, { View insertAfter, Scope viewScope}) {
40-
if (viewScope == null) viewScope = scope.createChild(new PrototypeMap(scope.context));
39+
View insertNew(ViewFactory viewFactory, {View insertAfter, Scope viewScope}) {
40+
if (viewScope == null) viewScope = scope.createChild(scope.context);
4141
View view = viewFactory.call(viewScope, directiveInjector);
4242
return insert(view, insertAfter: insertAfter);
4343
}

0 commit comments

Comments
 (0)