feat(change_detection): updated handling ON_PUSH detectors so they get notified when their bindings change

This commit is contained in:
vsavkin
2015-04-14 08:54:09 -07:00
parent dc9c614da2
commit 68faddbf5c
13 changed files with 249 additions and 60 deletions

View File

@ -1,6 +1,7 @@
import {ABSTRACT, CONST, normalizeBlank, isPresent} from 'angular2/src/facade/lang';
import {ListWrapper, List} from 'angular2/src/facade/collection';
import {Injectable} from 'angular2/di';
import {DEFAULT} from 'angular2/change_detection';
// type StringMap = {[idx: string]: string};
@ -553,7 +554,7 @@ export class Component extends Directive {
hostListeners,
injectables,
lifecycle,
changeDetection
changeDetection = DEFAULT
}:{
selector:string,
properties:Object,

View File

@ -8,7 +8,7 @@ import * as viewModule from 'angular2/src/core/compiler/view';
import {ViewContainer} from 'angular2/src/core/compiler/view_container';
import {NgElement} from 'angular2/src/core/compiler/ng_element';
import {Directive, Component, onChange, onDestroy, onAllChangesDone} from 'angular2/src/core/annotations/annotations';
import {ChangeDetectorRef} from 'angular2/change_detection';
import {ChangeDetector, ChangeDetectorRef} from 'angular2/change_detection';
import {QueryList} from './query_list';
var _MAX_DIRECTIVE_CONSTRUCTION_COUNTER = 10;
@ -277,6 +277,15 @@ export class DirectiveBinding extends ResolvedBinding {
}
}
get changeDetection() {
if (this.annotation instanceof Component) {
var c:Component = this.annotation;
return c.changeDetection;
} else {
return null;
}
}
static createFromBinding(b:Binding, annotation:Directive):DirectiveBinding {
var rb = b.resolve();
var deps = ListWrapper.map(rb.dependencies, DirectiveDependency.createFrom);
@ -298,13 +307,13 @@ export class PreBuiltObjects {
view:viewModule.AppView;
element:NgElement;
viewContainer:ViewContainer;
changeDetectorRef:ChangeDetectorRef;
changeDetector:ChangeDetector;
constructor(view, element:NgElement, viewContainer:ViewContainer,
changeDetectorRef:ChangeDetectorRef) {
changeDetector:ChangeDetector) {
this.view = view;
this.element = element;
this.viewContainer = viewContainer;
this.changeDetectorRef = changeDetectorRef;
this.changeDetector = changeDetector;
}
}
@ -603,6 +612,10 @@ export class ElementInjector extends TreeNode {
return this._preBuiltObjects.element;
}
getChangeDetector() {
return this._preBuiltObjects.changeDetector;
}
getComponent() {
if (this._proto._binding0IsComponent) {
return this._obj0;
@ -885,7 +898,7 @@ export class ElementInjector extends TreeNode {
if (keyId === staticKeys.viewId) return this._preBuiltObjects.view;
if (keyId === staticKeys.ngElementId) return this._preBuiltObjects.element;
if (keyId === staticKeys.viewContainerId) return this._preBuiltObjects.viewContainer;
if (keyId === staticKeys.changeDetectorRefId) return this._preBuiltObjects.changeDetectorRef;
if (keyId === staticKeys.changeDetectorRefId) return this._preBuiltObjects.changeDetector.ref;
//TODO add other objects as needed
return _undefined;

View File

@ -256,11 +256,16 @@ export class AppView {
}
}
directive(directive:DirectiveRecord) {
var elementInjector:ElementInjector = this.elementInjectors[directive.elementIndex];
getDirectiveFor(directive:DirectiveRecord) {
var elementInjector = this.elementInjectors[directive.elementIndex];
return elementInjector.getDirectiveAtIndex(directive.directiveIndex);
}
getDetectorFor(directive:DirectiveRecord) {
var elementInjector = this.elementInjectors[directive.elementIndex];
return elementInjector.getChangeDetector();
}
setDynamicComponentChildView(boundElementIndex, view:AppView) {
if (!this.proto.elementBinders[boundElementIndex].hasDynamicComponent()) {
throw new BaseException(`There is no dynamic component directive at element ${boundElementIndex}`);
@ -458,9 +463,11 @@ export class AppProtoView {
if (!MapWrapper.contains(this._directiveRecordsMap, id)) {
var binding = protoElementInjector.getDirectiveBindingAtIndex(directiveIndex);
var changeDetection = binding.changeDetection;
MapWrapper.set(this._directiveRecordsMap, id,
new DirectiveRecord(elementInjectorIndex, directiveIndex,
binding.callOnAllChangesDone, binding.callOnChange));
binding.callOnAllChangesDone, binding.callOnChange, changeDetection));
}
return MapWrapper.get(this._directiveRecordsMap, id);

View File

@ -5,7 +5,6 @@ import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
import {NgElement} from 'angular2/src/core/compiler/ng_element';
import * as vcModule from './view_container';
import * as viewModule from './view';
import {ChangeDetectorRef} from 'angular2/change_detection';
// TODO(tbosch): Make this an OpaqueToken as soon as our transpiler supports this!
export const VIEW_POOL_CAPACITY = 'ViewFactory.viewPoolCapacity';
@ -77,13 +76,12 @@ export class ViewFactory {
elementInjectors[binderIdx] = elementInjector;
// componentChildViews
var changeDetectorRef = null;
var childChangeDetector = null;
if (binder.hasStaticComponent()) {
var childView = this._createView(binder.nestedProtoView);
changeDetector.addShadowDomChild(childView.changeDetector);
changeDetectorRef = new ChangeDetectorRef(childView.changeDetector);
childChangeDetector = childView.changeDetector;
changeDetector.addShadowDomChild(childChangeDetector);
componentChildViews[binderIdx] = childView;
}
@ -97,7 +95,7 @@ export class ViewFactory {
// preBuiltObjects
if (isPresent(elementInjector)) {
preBuiltObjects[binderIdx] = new eli.PreBuiltObjects(view, new NgElement(view, binderIdx), viewContainer,
changeDetectorRef);
childChangeDetector);
}
}