refactor(ChangeDetector): use View/ShadowDom & Content/LightDom consistently

This commit is contained in:
Victor Berchet 2015-10-15 15:57:16 -07:00
parent 5d9b1e90dc
commit c56efc0c5f
7 changed files with 43 additions and 44 deletions

View File

@ -26,8 +26,8 @@ class _Context {
}
export class AbstractChangeDetector<T> implements ChangeDetector {
lightDomChildren: any[] = [];
shadowDomChildren: any[] = [];
contentChildren: any[] = [];
viewChildren: any[] = [];
parent: ChangeDetector;
ref: ChangeDetectorRef;
@ -50,21 +50,21 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
this.ref = new ChangeDetectorRef_(this);
}
addChild(cd: ChangeDetector): void {
this.lightDomChildren.push(cd);
addContentChild(cd: ChangeDetector): void {
this.contentChildren.push(cd);
cd.parent = this;
}
removeChild(cd: ChangeDetector): void { ListWrapper.remove(this.lightDomChildren, cd); }
removeContentChild(cd: ChangeDetector): void { ListWrapper.remove(this.contentChildren, cd); }
addShadowDomChild(cd: ChangeDetector): void {
this.shadowDomChildren.push(cd);
addViewChild(cd: ChangeDetector): void {
this.viewChildren.push(cd);
cd.parent = this;
}
removeShadowDomChild(cd: ChangeDetector): void { ListWrapper.remove(this.shadowDomChildren, cd); }
removeViewChild(cd: ChangeDetector): void { ListWrapper.remove(this.viewChildren, cd); }
remove(): void { this.parent.removeChild(this); }
remove(): void { this.parent.removeContentChild(this); }
handleEvent(eventName: string, elIndex: number, locals: Locals): boolean {
var res = this.handleEventInternal(eventName, elIndex, locals);
@ -86,10 +86,10 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
this.detectChangesInRecords(throwOnChange);
this._detectChangesInLightDomChildren(throwOnChange);
this._detectChangesContentChildren(throwOnChange);
if (!throwOnChange) this.afterContentLifecycleCallbacks();
this._detectChangesInShadowDomChildren(throwOnChange);
this._detectChangesInViewChildren(throwOnChange);
if (!throwOnChange) this.afterViewLifecycleCallbacks();
if (this.mode === ChangeDetectionStrategy.CheckOnce)
@ -130,7 +130,7 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
// This method is not intended to be overridden. Subclasses should instead provide an
// implementation of `hydrateDirectives`.
hydrate(context: T, locals: Locals, directives: any, pipes: any): void {
hydrate(context: T, locals: Locals, directives: any, pipes: Pipes): void {
this.mode = ChangeDetectionUtil.changeDetectionMode(this.strategy);
this.context = context;
@ -183,16 +183,16 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
afterViewLifecycleCallbacksInternal(): void {}
/** @internal */
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
var c = this.lightDomChildren;
_detectChangesContentChildren(throwOnChange: boolean): void {
var c = this.contentChildren;
for (var i = 0; i < c.length; ++i) {
c[i].runDetectChanges(throwOnChange);
}
}
/** @internal */
_detectChangesInShadowDomChildren(throwOnChange: boolean): void {
var c = this.shadowDomChildren;
_detectChangesInViewChildren(throwOnChange: boolean): void {
var c = this.viewChildren;
for (var i = 0; i < c.length; ++i) {
c[i].runDetectChanges(throwOnChange);
}

View File

@ -47,7 +47,6 @@ export class BindingRecord {
return isBlank(this.directiveRecord) || this.directiveRecord.isDefaultChangeDetection();
}
static createDirectiveDoCheck(directiveRecord: DirectiveRecord): BindingRecord {
return new BindingRecord(DIRECTIVE_LIFECYCLE, null, 0, null, null, "DoCheck", directiveRecord);
}

View File

@ -23,7 +23,7 @@ export const CONTEXT_ACCESSOR = "context";
export const CONTEXT_INDEX = 0;
const _FIELD_PREFIX = 'this.';
var _whiteSpaceRegExp = RegExpWrapper.create("\\W", "g");
var _whiteSpaceRegExp = /\W/g;
/**
* Returns `s` with all non-identifier characters removed.

View File

@ -22,10 +22,10 @@ export interface ChangeDetector {
mode: ChangeDetectionStrategy;
ref: ChangeDetectorRef;
addChild(cd: ChangeDetector): void;
addShadowDomChild(cd: ChangeDetector): void;
removeChild(cd: ChangeDetector): void;
removeShadowDomChild(cd: ChangeDetector): void;
addContentChild(cd: ChangeDetector): void;
addViewChild(cd: ChangeDetector): void;
removeContentChild(cd: ChangeDetector): void;
removeViewChild(cd: ChangeDetector): void;
remove(): void;
hydrate(context: any, locals: Locals, directives: any, pipes: any): void;
dehydrate(): void;

View File

@ -102,7 +102,7 @@ export class AppViewManagerUtils {
currentView.init(protoView.changeDetectorFactory(currentView), elementInjectors,
rootElementInjectors, preBuiltObjects, views, elementRefs, viewContainers);
if (isPresent(parentView) && protoView.type === viewModule.ViewType.COMPONENT) {
parentView.changeDetector.addShadowDomChild(currentView.changeDetector);
parentView.changeDetector.addViewChild(currentView.changeDetector);
}
elementOffset += protoView.elementBinders.length;
textOffset += protoView.textBindingCount;
@ -122,7 +122,7 @@ export class AppViewManagerUtils {
contextView = parentView;
contextBoundElementIndex = boundElementIndex;
}
parentView.changeDetector.addChild(view.changeDetector);
parentView.changeDetector.addContentChild(view.changeDetector);
var viewContainer = parentView.viewContainers[boundElementIndex];
if (isBlank(viewContainer)) {
viewContainer = new viewModule.AppViewContainer();

View File

@ -634,7 +634,7 @@ export function main() {
it('should be called before processing view children', () => {
var parent = _createWithoutHydrate('directNoDispatcher').changeDetector;
var child = _createWithoutHydrate('directNoDispatcher').changeDetector;
parent.addShadowDomChild(child);
parent.addViewChild(child);
var orderOfOperations = [];
@ -753,7 +753,7 @@ export function main() {
it('should be called after processing view children', () => {
var parent = _createWithoutHydrate('directNoDispatcher').changeDetector;
var child = _createWithoutHydrate('directNoDispatcher').changeDetector;
parent.addShadowDomChild(child);
parent.addViewChild(child);
var orderOfOperations = [];
@ -902,32 +902,32 @@ export function main() {
child = _createChangeDetector('"str"').changeDetector;
});
it('should add light dom children', () => {
parent.addChild(child);
it('should add content children', () => {
parent.addContentChild(child);
expect(parent.lightDomChildren.length).toEqual(1);
expect(parent.lightDomChildren[0]).toBe(child);
expect(parent.contentChildren.length).toEqual(1);
expect(parent.contentChildren[0]).toBe(child);
});
it('should add shadow dom children', () => {
parent.addShadowDomChild(child);
it('should add view children', () => {
parent.addViewChild(child);
expect(parent.shadowDomChildren.length).toEqual(1);
expect(parent.shadowDomChildren[0]).toBe(child);
expect(parent.viewChildren.length).toEqual(1);
expect(parent.viewChildren[0]).toBe(child);
});
it('should remove light dom children', () => {
parent.addChild(child);
parent.removeChild(child);
it('should remove content children', () => {
parent.addContentChild(child);
parent.removeContentChild(child);
expect(parent.lightDomChildren).toEqual([]);
expect(parent.contentChildren).toEqual([]);
});
it('should remove shadow dom children', () => {
parent.addShadowDomChild(child);
parent.removeShadowDomChild(child);
it('should remove view children', () => {
parent.addViewChild(child);
parent.removeViewChild(child);
expect(parent.shadowDomChildren.length).toEqual(0);
expect(parent.viewChildren.length).toEqual(0);
});
});
@ -1142,7 +1142,7 @@ export function main() {
function changeDetector(mode, parent) {
var val = _createChangeDetector('10');
val.changeDetector.mode = mode;
if (isPresent(parent)) parent.addChild(val.changeDetector);
if (isPresent(parent)) parent.addContentChild(val.changeDetector);
return val.changeDetector;
}

View File

@ -285,7 +285,7 @@ function setUpChangeDetection(protoChangeDetectorFactory: Function, iterations,
for (var i = 0; i < iterations; ++i) {
var cd = proto.instantiate(dispatcher);
cd.hydrate(object, null, new FakeDirectives(targetObj), null);
parentCd.addChild(cd);
parentCd.addContentChild(cd);
}
return parentCd;
}