Skip to content

Commit

Permalink
refactor(PropertySetter): use the global reflector
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Mar 12, 2015
1 parent c67194a commit 951a808
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 120 deletions.
6 changes: 3 additions & 3 deletions modules/angular2/src/core/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function _injectorBindings(appComponentType): List<Binding> {
}, [appComponentAnnotatedTypeToken, appDocumentToken]),

bind(appViewToken).toAsyncFactory((changeDetection, compiler, injector, appElement,
appComponentAnnotatedType, strategy, eventManager, reflector) => {
appComponentAnnotatedType, strategy, eventManager) => {
return compiler.compile(appComponentAnnotatedType.type).then(
(protoView) => {
var appProtoView = ProtoView.createRootProtoView(protoView, appElement,
Expand All @@ -66,12 +66,12 @@ function _injectorBindings(appComponentType): List<Binding> {
// The light Dom of the app element is not considered part of
// the angular application. Thus the context and lightDomInjector are
// empty.
var view = appProtoView.instantiate(null, eventManager, reflector);
var view = appProtoView.instantiate(null, eventManager);
view.hydrate(injector, null, new Object());
return view;
});
}, [ChangeDetection, Compiler, Injector, appElementToken, appComponentAnnotatedTypeToken,
ShadowDomStrategy, EventManager, Reflector]),
ShadowDomStrategy, EventManager]),

bind(appChangeDetectorToken).toFactory((rootView) => rootView.changeDetector,
[appViewToken]),
Expand Down
13 changes: 5 additions & 8 deletions modules/angular2/src/core/compiler/element_injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {ViewContainer} from 'angular2/src/core/compiler/view_container';
import {NgElement} from 'angular2/src/core/dom/element';
import {Directive, onChange, onDestroy} from 'angular2/src/core/annotations/annotations'
import {BindingPropagationConfig} from 'angular2/src/core/compiler/binding_propagation_config'
import {Reflector} from 'angular2/src/reflection/reflection';
import {reflector} from 'angular2/src/reflection/reflection';

var _MAX_DIRECTIVE_CONSTRUCTION_COUNTER = 10;

Expand Down Expand Up @@ -270,8 +270,8 @@ export class ProtoElementInjector {
}
}

instantiate(parent:ElementInjector, host:ElementInjector, reflector: Reflector):ElementInjector {
return new ElementInjector(this, parent, host, reflector);
instantiate(parent:ElementInjector, host:ElementInjector):ElementInjector {
return new ElementInjector(this, parent, host);
}

directParent(): ProtoElementInjector {
Expand Down Expand Up @@ -324,10 +324,8 @@ export class ElementInjector extends TreeNode {
_obj9:any;
_preBuiltObjects;
_constructionCounter;
_refelector: Reflector;

constructor(proto:ProtoElementInjector, parent:ElementInjector, host:ElementInjector,
reflector: Reflector) {
constructor(proto:ProtoElementInjector, parent:ElementInjector, host:ElementInjector) {
super(parent);
if (isPresent(parent) && isPresent(host)) {
throw new BaseException('Only either parent or host is allowed');
Expand All @@ -340,7 +338,6 @@ export class ElementInjector extends TreeNode {
}

this._proto = proto;
this._refelector = reflector;

//we cannot call clearDirectives because fields won't be detected
this._preBuiltObjects = null;
Expand Down Expand Up @@ -518,7 +515,7 @@ export class ElementInjector extends TreeNode {
_buildPropSetter(dep) {
var ngElement = this._getPreBuiltObjectByKeyId(StaticKeys.instance().ngElementId);
var domElement = ngElement.domElement;
var setter = this._refelector.setter(dep.propSetterName);
var setter = reflector.setter(dep.propSetterName);
return function(v) { setter(domElement, v) };
}

Expand Down
26 changes: 10 additions & 16 deletions modules/angular2/src/core/compiler/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import {ShadowDomStrategy} from './shadow_dom_strategy';
import {ViewPool} from './view_pool';
import {EventManager} from 'angular2/src/core/events/event_manager';

import {Reflector} from 'angular2/src/reflection/reflection';

const NG_BINDING_CLASS = 'ng-binding';
const NG_BINDING_CLASS_SELECTOR = '.ng-binding';

Expand Down Expand Up @@ -320,23 +318,19 @@ export class ProtoView {
}

// TODO(rado): hostElementInjector should be moved to hydrate phase.
instantiate(hostElementInjector: ElementInjector, eventManager: EventManager,
reflector: Reflector):View {
if (this._viewPool.length() == 0) this._preFillPool(hostElementInjector, eventManager,
reflector);
instantiate(hostElementInjector: ElementInjector, eventManager: EventManager):View {
if (this._viewPool.length() == 0) this._preFillPool(hostElementInjector, eventManager);
var view = this._viewPool.pop();
return isPresent(view) ? view : this._instantiate(hostElementInjector, eventManager, reflector);
return isPresent(view) ? view : this._instantiate(hostElementInjector, eventManager);
}

_preFillPool(hostElementInjector: ElementInjector, eventManager: EventManager,
reflector: Reflector) {
_preFillPool(hostElementInjector: ElementInjector, eventManager: EventManager) {
for (var i = 0; i < VIEW_POOL_PREFILL; i++) {
this._viewPool.push(this._instantiate(hostElementInjector, eventManager, reflector));
this._viewPool.push(this._instantiate(hostElementInjector, eventManager));
}
}

_instantiate(hostElementInjector: ElementInjector, eventManager: EventManager,
reflector: Reflector): View {
_instantiate(hostElementInjector: ElementInjector, eventManager: EventManager): View {
var rootElementClone = this.instantiateInPlace ? this.element : DOM.importIntoDoc(this.element);
var elementsWithBindingsDynamic;
if (this.isTemplateElement) {
Expand Down Expand Up @@ -389,9 +383,9 @@ export class ProtoView {
if (isPresent(protoElementInjector)) {
if (isPresent(protoElementInjector.parent)) {
var parentElementInjector = elementInjectors[protoElementInjector.parent.index];
elementInjector = protoElementInjector.instantiate(parentElementInjector, null, reflector);
elementInjector = protoElementInjector.instantiate(parentElementInjector, null);
} else {
elementInjector = protoElementInjector.instantiate(null, hostElementInjector, reflector);
elementInjector = protoElementInjector.instantiate(null, hostElementInjector);
ListWrapper.push(rootElementInjectors, elementInjector);
}
}
Expand All @@ -418,7 +412,7 @@ export class ProtoView {
var bindingPropagationConfig = null;
if (isPresent(binder.componentDirective)) {
var strategy = this.shadowDomStrategy;
var childView = binder.nestedProtoView.instantiate(elementInjector, eventManager, reflector);
var childView = binder.nestedProtoView.instantiate(elementInjector, eventManager);
view.changeDetector.addChild(childView.changeDetector);

lightDom = strategy.constructLightDom(view, childView, element);
Expand All @@ -434,7 +428,7 @@ export class ProtoView {
if (isPresent(binder.viewportDirective)) {
var destLightDom = this._directParentElementLightDom(protoElementInjector, preBuiltObjects);
viewContainer = new ViewContainer(view, element, binder.nestedProtoView, elementInjector,
eventManager, reflector, destLightDom);
eventManager, destLightDom);
ListWrapper.push(viewContainers, viewContainer);
}

Expand Down
7 changes: 1 addition & 6 deletions modules/angular2/src/core/compiler/view_container.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Injector} from 'angular2/di';
import * as eiModule from 'angular2/src/core/compiler/element_injector';
import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {EventManager} from 'angular2/src/core/events/event_manager';
import {Reflector} from 'angular2/src/reflection/reflection';

export class ViewContainer {
parentView: viewModule.View;
Expand All @@ -15,7 +14,6 @@ export class ViewContainer {
_views: List<viewModule.View>;
_lightDom: any;
_eventManager: EventManager;
_reflector: Reflector;
elementInjector: eiModule.ElementInjector;
appInjector: Injector;
hostElementInjector: eiModule.ElementInjector;
Expand All @@ -25,14 +23,12 @@ export class ViewContainer {
defaultProtoView: viewModule.ProtoView,
elementInjector: eiModule.ElementInjector,
eventManager: EventManager,
reflector: Reflector,
lightDom = null) {
this.parentView = parentView;
this.templateElement = templateElement;
this.defaultProtoView = defaultProtoView;
this.elementInjector = elementInjector;
this._lightDom = lightDom;
this._reflector = reflector;

// The order in this list matches the DOM order.
this._views = [];
Expand Down Expand Up @@ -81,8 +77,7 @@ export class ViewContainer {
if (!this.hydrated()) throw new BaseException(
'Cannot create views on a dehydrated ViewContainer');
// TODO(rado): replace with viewFactory.
var newView = this.defaultProtoView.instantiate(this.hostElementInjector, this._eventManager,
this._reflector);
var newView = this.defaultProtoView.instantiate(this.hostElementInjector, this._eventManager);
// insertion must come before hydration so that element injector trees are attached.
this.insert(newView, atIndex);
newView.hydrate(this.appInjector, this.hostElementInjector, this.parentView.context);
Expand Down
27 changes: 13 additions & 14 deletions modules/angular2/test/core/compiler/element_injector_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {NgElement} from 'angular2/src/core/dom/element';
import {LightDom, SourceLightDom, DestinationLightDom} from 'angular2/src/core/compiler/shadow_dom_emulation/light_dom';
import {Directive} from 'angular2/src/core/annotations/annotations';
import {BindingPropagationConfig} from 'angular2/src/core/compiler/binding_propagation_config';
import {reflector} from 'angular2/src/reflection/reflection';
import {DynamicProtoChangeDetector} from 'angular2/change_detection';

@proxy
Expand Down Expand Up @@ -131,7 +130,7 @@ export function main() {
if (isBlank(lightDomAppInjector)) lightDomAppInjector = new Injector([]);

var proto = new ProtoElementInjector(null, 0, bindings, isPresent(shadowDomAppInjector));
var inj = proto.instantiate(null, null, reflector);
var inj = proto.instantiate(null, null);
var preBuilt = isPresent(preBuiltObjects) ? preBuiltObjects : defaultPreBuiltObjects;

inj.instantiateDirectives(lightDomAppInjector, shadowDomAppInjector, preBuilt);
Expand All @@ -144,12 +143,12 @@ export function main() {
var inj = new Injector([]);

var protoParent = new ProtoElementInjector(null, 0, parentBindings);
var parent = protoParent.instantiate(null, null, reflector);
var parent = protoParent.instantiate(null, null);

parent.instantiateDirectives(inj, null, parentPreBuildObjects);

var protoChild = new ProtoElementInjector(protoParent, 1, childBindings, false, 1);
var child = protoChild.instantiate(parent, null, reflector);
var child = protoChild.instantiate(parent, null);
child.instantiateDirectives(inj, null, defaultPreBuiltObjects);

return child;
Expand All @@ -162,11 +161,11 @@ export function main() {
var shadowInj = inj.createChild([]);

var protoParent = new ProtoElementInjector(null, 0, hostBindings, true);
var host = protoParent.instantiate(null, null, reflector);
var host = protoParent.instantiate(null, null);
host.instantiateDirectives(inj, shadowInj, hostPreBuildObjects);

var protoChild = new ProtoElementInjector(protoParent, 0, shadowBindings, false, 1);
var shadow = protoChild.instantiate(null, host, reflector);
var shadow = protoChild.instantiate(null, host);
shadow.instantiateDirectives(shadowInj, null, null);

return shadow;
Expand Down Expand Up @@ -199,9 +198,9 @@ export function main() {
var protoChild1 = new ProtoElementInjector(protoParent, 1, []);
var protoChild2 = new ProtoElementInjector(protoParent, 2, []);

var p = protoParent.instantiate(null, null, reflector);
var c1 = protoChild1.instantiate(p, null, reflector);
var c2 = protoChild2.instantiate(p, null, reflector);
var p = protoParent.instantiate(null, null);
var c1 = protoChild1.instantiate(p, null);
var c2 = protoChild2.instantiate(p, null);

expect(humanize(p, [
[p, 'parent'],
Expand All @@ -216,8 +215,8 @@ export function main() {
var protoParent = new ProtoElementInjector(null, 0, []);
var protoChild = new ProtoElementInjector(protoParent, 1, [], false, distance);

var p = protoParent.instantiate(null, null, reflector);
var c = protoChild.instantiate(p, null, reflector);
var p = protoParent.instantiate(null, null);
var c = protoChild.instantiate(p, null);

expect(c.directParent()).toEqual(p);
});
Expand All @@ -227,8 +226,8 @@ export function main() {
var protoParent = new ProtoElementInjector(null, 0, []);
var protoChild = new ProtoElementInjector(protoParent, 1, [], false, distance);

var p = protoParent.instantiate(null, null, reflector);
var c = protoChild.instantiate(p, null, reflector);
var p = protoParent.instantiate(null, null);
var c = protoChild.instantiate(p, null);

expect(c.directParent()).toEqual(null);
});
Expand Down Expand Up @@ -435,7 +434,7 @@ export function main() {
});

it('should return viewContainer', function () {
var viewContainer = new ViewContainer(null, null, null, null, null, null);
var viewContainer = new ViewContainer(null, null, null, null, null);
var inj = injector([], null, null, new PreBuiltObjects(null, null, viewContainer, null, null));

expect(inj.get(ViewContainer)).toEqual(viewContainer);
Expand Down
8 changes: 1 addition & 7 deletions modules/angular2/test/core/compiler/integration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import {If} from 'angular2/src/directives/if';

import {ViewContainer} from 'angular2/src/core/compiler/view_container';

import {reflector} from 'angular2/src/reflection/reflection';

export function main() {
describe('integration tests', function() {
var compiler, tplResolver;
Expand Down Expand Up @@ -58,11 +56,7 @@ export function main() {
var view, ctx, cd;
function createView(pv) {
ctx = new MyComp();
view = pv.instantiate(
null,
null,
reflector
);
view = pv.instantiate(null, null);
view.hydrate(new Injector([]), null, ctx);
cd = view.changeDetector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function main() {

function instantiateView(protoView) {
evalContext = new Context();
view = protoView.instantiate(null, null, null);
view = protoView.instantiate(null, null);
view.hydrate(new Injector([]), null, evalContext);
changeDetector = view.changeDetector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import {ViewContainer} from 'angular2/src/core/compiler/view_container';

import {BrowserDomAdapter} from 'angular2/src/dom/browser_adapter';

import {reflector} from 'angular2/src/reflection/reflection';

export function main() {
BrowserDomAdapter.makeCurrent();
describe('integration tests', function() {
Expand Down Expand Up @@ -357,7 +355,7 @@ class MyComp {
}

function createView(pv) {
var view = pv.instantiate(null, null, reflector);
var view = pv.instantiate(null, null);
view.hydrate(new Injector([]), null, {});
return view;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function main() {
var host = el('<div></div>');
var nodes = el('<div>view</div>');
var pv = new ProtoView(nodes, new DynamicProtoChangeDetector(null), null);
var view = pv.instantiate(null, null, null);
var view = pv.instantiate(null, null);

strategy.attachTemplate(host, view);
var shadowRoot = DOM.getShadowRoot(host);
Expand Down Expand Up @@ -83,7 +83,7 @@ export function main() {
var host = el('<div><span>original content</span></div>');
var nodes = el('<div>view</div>');
var pv = new ProtoView(nodes, new DynamicProtoChangeDetector(null), null);
var view = pv.instantiate(null, null, null);
var view = pv.instantiate(null, null);

strategy.attachTemplate(host, view);
var firstChild = DOM.firstChild(host);
Expand Down Expand Up @@ -218,7 +218,7 @@ export function main() {
var host = el('<div><span>original content</span></div>');
var nodes = el('<div>view</div>');
var pv = new ProtoView(nodes, new DynamicProtoChangeDetector(null), null);
var view = pv.instantiate(null, null, null);
var view = pv.instantiate(null, null);

strategy.attachTemplate(host, view);
var firstChild = DOM.firstChild(host);
Expand Down
7 changes: 3 additions & 4 deletions modules/angular2/test/core/compiler/view_container_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {Injector} from 'angular2/di';
import {ProtoElementInjector, ElementInjector} from 'angular2/src/core/compiler/element_injector';
import {NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
import {DynamicProtoChangeDetector, ChangeDetector, Lexer, Parser} from 'angular2/change_detection';
import {reflector} from 'angular2/src/reflection/reflection';

function createView(nodes) {
var view = new View(null, nodes, new DynamicProtoChangeDetector(null), MapWrapper.create());
Expand Down Expand Up @@ -72,9 +71,9 @@ export function main() {
parentView = createView([dom.childNodes[0]]);
protoView = new ProtoView(el('<div>hi</div>'), new DynamicProtoChangeDetector(null),
new NativeShadowDomStrategy(null));
elementInjector = new ElementInjector(null, null, null, reflector);
elementInjector = new ElementInjector(null, null, null);
viewContainer = new ViewContainer(parentView, insertionElement, protoView, elementInjector,
null, reflector);
null);
customViewWithOneNode = createView([el('<div>single</div>')]);
customViewWithTwoNodes = createView([el('<div>one</div>'), el('<div>two</div>')]);
});
Expand Down Expand Up @@ -219,7 +218,7 @@ export function main() {
new DynamicProtoChangeDetector(null), new NativeShadowDomStrategy(null));
pv.bindElement(new ProtoElementInjector(null, 1, [SomeDirective]));
pv.bindTextNode(0, parser.parseBinding('foo', null));
fancyView = pv.instantiate(null, null, reflector);
fancyView = pv.instantiate(null, null);
});

it('hydrating should update rootElementInjectors and parent change detector', () => {
Expand Down
Loading

0 comments on commit 951a808

Please sign in to comment.