refactor(change_detect): Move (de)hydrate logic into dedicated methods

Call new `(de)hydrateDirectives` methods from `(de)hydrate`. Add a null
implementation in `AbstractChangeDetector` and only override if
necessary for the specific change detector.

Update to #3248
This commit is contained in:
Tim Blasi
2015-07-27 18:31:22 -07:00
parent a9efc48e71
commit d84993faf1
4 changed files with 98 additions and 35 deletions

View File

@ -60,8 +60,12 @@ export class AbstractChangeDetector implements ChangeDetector {
hydrate(context: any, locals: Locals, directives: any, pipes: any): void {}
hydrateDirectives(directives: any): void {}
dehydrate(): void {}
dehydrateDirectives(destroyPipes: boolean): void {}
callOnAllChangesDone(): void {}
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
@ -95,4 +99,4 @@ export class AbstractChangeDetector implements ChangeDetector {
null;
throw new ChangeDetectionError(proto, exception, stack, context);
}
}
}

View File

@ -33,17 +33,18 @@ var ALREADY_CHECKED_ACCESSOR = "this.alreadyChecked";
export class ChangeDetectorJITGenerator {
_names: CodegenNameUtil;
_typeName: string;
constructor(public id: string, public changeDetectionStrategy: string,
public records: List<ProtoRecord>, public directiveRecords: List<any>,
private generateCheckNoChanges: boolean) {
this._names = new CodegenNameUtil(this.records, this.directiveRecords, 'this._', UTIL);
this._typeName = sanitizeName(`ChangeDetector_${this.id}`);
}
generate(): Function {
var typeName = sanitizeName(`ChangeDetector_${this.id}`);
var classDefinition = `
var ${typeName} = function ${typeName}(dispatcher, protos, directiveRecords) {
var ${this._typeName} = function ${this._typeName}(dispatcher, protos, directiveRecords) {
${ABSTRACT_CHANGE_DETECTOR}.call(this, ${JSON.stringify(this.id)}, dispatcher);
${PROTOS_ACCESSOR} = protos;
${DIRECTIVES_ACCESSOR} = directiveRecords;
@ -51,12 +52,12 @@ export class ChangeDetectorJITGenerator {
${CURRENT_PROTO} = null;
${PIPES_ACCESSOR} = null;
${ALREADY_CHECKED_ACCESSOR} = false;
${this._names.genDehydrateFields()}
this.dehydrateDirectives(false);
}
${typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype);
${this._typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype);
${typeName}.prototype.detectChangesInRecords = function(throwOnChange) {
${this._typeName}.prototype.detectChangesInRecords = function(throwOnChange) {
if (!this.hydrated()) {
${UTIL}.throwDehydrated();
}
@ -67,7 +68,7 @@ export class ChangeDetectorJITGenerator {
}
}
${typeName}.prototype.__detectChangesInRecords = function(throwOnChange) {
${this._typeName}.prototype.__detectChangesInRecords = function(throwOnChange) {
${CURRENT_PROTO} = null;
${this._names.genInitLocals()}
@ -81,35 +82,37 @@ export class ChangeDetectorJITGenerator {
${ALREADY_CHECKED_ACCESSOR} = true;
}
${this._genCheckNoChanges(typeName)}
${this._genCheckNoChanges()}
${typeName}.prototype.callOnAllChangesDone = function() {
${this._typeName}.prototype.callOnAllChangesDone = function() {
${this._genCallOnAllChangesDoneBody()}
}
${typeName}.prototype.hydrate = function(context, locals, directives, pipes) {
${this._typeName}.prototype.hydrate = function(context, locals, directives, pipes) {
${MODE_ACCESSOR} = "${ChangeDetectionUtil.changeDetectionMode(this.changeDetectionStrategy)}";
${this._names.getContextName()} = context;
${LOCALS_ACCESSOR} = locals;
${this._genHydrateDirectives()}
${this._genHydrateDetectors()}
this.hydrateDirectives(directives);
${PIPES_ACCESSOR} = pipes;
${ALREADY_CHECKED_ACCESSOR} = false;
}
${typeName}.prototype.dehydrate = function() {
${this._names.genPipeOnDestroy()}
${this._names.genDehydrateFields()}
${this._maybeGenHydrateDirectives()}
${this._typeName}.prototype.dehydrate = function() {
this.dehydrateDirectives(true);
${LOCALS_ACCESSOR} = null;
${PIPES_ACCESSOR} = null;
}
${typeName}.prototype.hydrated = function() {
${this._maybeGenDehydrateDirectives()}
${this._typeName}.prototype.hydrated = function() {
return ${this._names.getContextName()} !== null;
}
return function(dispatcher) {
return new ${typeName}(dispatcher, protos, directiveRecords);
return new ${this._typeName}(dispatcher, protos, directiveRecords);
}
`;
@ -118,6 +121,29 @@ export class ChangeDetectorJITGenerator {
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
}
_maybeGenDehydrateDirectives(): string {
var destroyPipesCode = this._names.genPipeOnDestroy();
if (destroyPipesCode) {
destroyPipesCode = `if (destroyPipes) { ${destroyPipesCode} }`;
}
var dehydrateFieldsCode = this._names.genDehydrateFields();
if (!destroyPipesCode && !dehydrateFieldsCode) return '';
return `${this._typeName}.prototype.dehydrateDirectives = function(destroyPipes) {
${destroyPipesCode}
${dehydrateFieldsCode}
}`;
}
_maybeGenHydrateDirectives(): string {
var hydrateDirectivesCode = this._genHydrateDirectives();
var hydrateDetectorsCode = this._genHydrateDetectors();
if (!hydrateDirectivesCode && !hydrateDetectorsCode) return '';
return `${this._typeName}.prototype.hydrateDirectives = function(directives) {
${hydrateDirectivesCode}
${hydrateDetectorsCode}
}`;
}
_genHydrateDirectives(): string {
var directiveFieldNames = this._names.getAllDirectiveNames();
var lines = ListWrapper.createFixedSize(directiveFieldNames.length);
@ -345,9 +371,9 @@ export class ChangeDetectorJITGenerator {
}
}
_genCheckNoChanges(typeName: string): string {
_genCheckNoChanges(): string {
if (this.generateCheckNoChanges) {
return `${typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
return `${this._typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
} else {
return '';
}