refactor(change_detection): replaced devMode with ChangeDetectorGenConfig

This commit is contained in:
vsavkin 2015-08-20 13:50:33 -07:00 committed by Victor Savkin
parent 764726d78e
commit b6146394ae
14 changed files with 90 additions and 39 deletions

View File

@ -5,10 +5,21 @@ import {IterableDiffers, IterableDifferFactory} from './differs/iterable_differs
import {DefaultIterableDifferFactory} from './differs/default_iterable_differ';
import {KeyValueDiffers, KeyValueDifferFactory} from './differs/keyvalue_differs';
import {DefaultKeyValueDifferFactory} from './differs/default_keyvalue_differ';
import {ChangeDetection, ProtoChangeDetector, ChangeDetectorDefinition} from './interfaces';
import {
ChangeDetection,
ProtoChangeDetector,
ChangeDetectorDefinition,
ChangeDetectorGenConfig
} from './interfaces';
import {Injector, Inject, Injectable, OpaqueToken, Optional, Binding} from 'angular2/di';
import {List, StringMap, StringMapWrapper} from 'angular2/src/facade/collection';
import {CONST, CONST_EXPR, isPresent, BaseException} from 'angular2/src/facade/lang';
import {
CONST,
CONST_EXPR,
isPresent,
BaseException,
assertionsEnabled
} from 'angular2/src/facade/lang';
export {
ASTWithSource,
@ -34,7 +45,8 @@ export {
ChangeDispatcher,
ChangeDetection,
ChangeDetectorDefinition,
DebugContext
DebugContext,
ChangeDetectorGenConfig
} from './interfaces';
export {CHECK_ONCE, CHECK_ALWAYS, DETACHED, CHECKED, ON_PUSH, DEFAULT} from './constants';
export {DynamicProtoChangeDetector} from './proto_change_detector';
@ -101,6 +113,10 @@ export class PreGeneratedChangeDetection extends ChangeDetection {
}
get generateDetectors(): boolean { return true; }
get genConfig(): ChangeDetectorGenConfig {
return new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
}
}
@ -116,6 +132,10 @@ export class DynamicChangeDetection extends ChangeDetection {
}
get generateDetectors(): boolean { return true; }
get genConfig(): ChangeDetectorGenConfig {
return new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
}
}
/**
@ -134,4 +154,8 @@ export class JitChangeDetection extends ChangeDetection {
}
get generateDetectors(): boolean { return true; }
get genConfig(): ChangeDetectorGenConfig {
return new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
}
}

View File

@ -10,6 +10,7 @@ import {CodegenNameUtil, sanitizeName} from './codegen_name_util';
import {CodegenLogicUtil} from './codegen_logic_util';
import {EventBinding} from './event_binding';
import {BindingTarget} from './binding_record';
import {ChangeDetectorGenConfig} from './interfaces';
/**
@ -34,7 +35,7 @@ export class ChangeDetectorJITGenerator {
constructor(private id: string, private changeDetectionStrategy: string,
private records: List<ProtoRecord>, private propertyBindingTargets: BindingTarget[],
private eventBindings: EventBinding[], private directiveRecords: List<any>,
private devMode: boolean) {
private genConfig: ChangeDetectorGenConfig) {
this._names =
new CodegenNameUtil(this.records, this.eventBindings, this.directiveRecords, UTIL);
this._logic = new CodegenLogicUtil(this._names, UTIL, changeDetectionStrategy);
@ -87,7 +88,8 @@ export class ChangeDetectorJITGenerator {
}
_genPropertyBindingTargets(): string {
var targets = this._logic.genPropertyBindingTargets(this.propertyBindingTargets, true);
var targets = this._logic.genPropertyBindingTargets(this.propertyBindingTargets,
this.genConfig.genDebugInfo);
return `${this._typeName}.gen_propertyBindingTargets = ${targets};`;
}
@ -318,7 +320,7 @@ export class ChangeDetectorJITGenerator {
}
_genThrowOnChangeCheck(oldValue: string, newValue: string): string {
if (this.devMode) {
if (this.genConfig.genCheckNoChanges) {
return `
if(throwOnChange) {
this.throwOnChangeError(${oldValue}, ${newValue});
@ -330,7 +332,7 @@ export class ChangeDetectorJITGenerator {
}
_genCheckNoChanges(): string {
if (this.devMode) {
if (this.genConfig.genCheckNoChanges) {
return `${this._typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
} else {
return '';

View File

@ -124,11 +124,12 @@ export class CodegenLogicUtil {
}
}
genPropertyBindingTargets(propertyBindingTargets: BindingTarget[], devMode: boolean): string {
genPropertyBindingTargets(propertyBindingTargets: BindingTarget[],
genDebugInfo: boolean): string {
var bs = propertyBindingTargets.map(b => {
if (isBlank(b)) return "null";
var debug = devMode ? codify(b.debug) : "null";
var debug = genDebugInfo ? codify(b.debug) : "null";
return `${this._utilName}.bindingTarget(${codify(b.mode)}, ${b.elementIndex}, ${codify(b.name)}, ${codify(b.unit)}, ${debug})`;
});
return `[${bs.join(", ")}]`;

View File

@ -12,6 +12,7 @@ import {EventBinding} from './event_binding';
import {BindingRecord, BindingTarget} from './binding_record';
import {DirectiveRecord, DirectiveIndex} from './directive_record';
import {Locals} from './parser/locals';
import {ChangeDetectorGenConfig} from './interfaces';
import {ChangeDetectionUtil, SimpleChange} from './change_detection_util';
import {ProtoRecord, RecordType} from './proto_record';
@ -26,7 +27,8 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
constructor(id: string, dispatcher: any, numberOfPropertyProtoRecords: number,
propertyBindingTargets: BindingTarget[], directiveIndices: DirectiveIndex[],
modeOnHydrate: string, private records: ProtoRecord[],
private eventBindings: EventBinding[], private directiveRecords: DirectiveRecord[]) {
private eventBindings: EventBinding[], private directiveRecords: DirectiveRecord[],
private genConfig: ChangeDetectorGenConfig) {
super(id, dispatcher, numberOfPropertyProtoRecords, propertyBindingTargets, directiveIndices,
modeOnHydrate);
var len = records.length + 1;

View File

@ -37,6 +37,8 @@ export class ChangeDetection {
}
get generateDetectors(): boolean { return null; }
get genConfig(): ChangeDetectorGenConfig { return null; }
}
export class DebugContext {
@ -71,8 +73,13 @@ export interface ChangeDetector {
export interface ProtoChangeDetector { instantiate(dispatcher: ChangeDispatcher): ChangeDetector; }
export class ChangeDetectorGenConfig {
constructor(public genCheckNoChanges: boolean, public genDebugInfo: boolean) {}
}
export class ChangeDetectorDefinition {
constructor(public id: string, public strategy: string, public variableNames: List<string>,
public bindingRecords: BindingRecord[], public eventRecords: BindingRecord[],
public directiveRecords: DirectiveRecord[], public devMode: boolean) {}
public directiveRecords: DirectiveRecord[],
public genConfig: ChangeDetectorGenConfig) {}
}

View File

@ -25,7 +25,7 @@ export class JitProtoChangeDetector implements ProtoChangeDetector {
return new ChangeDetectorJITGenerator(
definition.id, definition.strategy, propertyBindingRecords, propertyBindingTargets,
eventBindingRecords, this.definition.directiveRecords, this.definition.devMode)
eventBindingRecords, this.definition.directiveRecords, this.definition.genConfig)
.generate();
}
}

View File

@ -54,7 +54,8 @@ export class DynamicProtoChangeDetector implements ProtoChangeDetector {
this.definition.id, dispatcher, this._propertyBindingRecords.length,
this._propertyBindingTargets, this._directiveIndices,
ChangeDetectionUtil.changeDetectionMode(this.definition.strategy),
this._propertyBindingRecords, this._eventBindingRecords, this.definition.directiveRecords);
this._propertyBindingRecords, this._eventBindingRecords, this.definition.directiveRecords,
this.definition.genConfig);
}
}

View File

@ -12,6 +12,7 @@ import {
ProtoChangeDetector,
DEFAULT,
ChangeDetectorDefinition,
ChangeDetectorGenConfig,
ASTWithSource
} from 'angular2/src/change_detection/change_detection';
@ -234,9 +235,9 @@ export class ProtoViewFactory {
nestedPvVariableNames: string[][],
allRenderDirectiveMetadata: any[]): ProtoChangeDetector[] {
if (this._changeDetection.generateDetectors) {
var changeDetectorDefs =
_getChangeDetectorDefinitions(hostComponentBinding.metadata, nestedPvsWithIndex,
nestedPvVariableNames, allRenderDirectiveMetadata);
var changeDetectorDefs = _getChangeDetectorDefinitions(
hostComponentBinding.metadata, nestedPvsWithIndex, nestedPvVariableNames,
allRenderDirectiveMetadata, this._changeDetection.genConfig);
return changeDetectorDefs.map(changeDetectorDef =>
this._changeDetection.getProtoChangeDetector(
changeDetectorDef.id, changeDetectorDef));
@ -254,11 +255,13 @@ export class ProtoViewFactory {
*/
export function getChangeDetectorDefinitions(
hostComponentMetadata: RenderDirectiveMetadata, rootRenderProtoView: ProtoViewDto,
allRenderDirectiveMetadata: List<RenderDirectiveMetadata>): List<ChangeDetectorDefinition> {
allRenderDirectiveMetadata: List<RenderDirectiveMetadata>, genConfig: ChangeDetectorGenConfig):
List<ChangeDetectorDefinition> {
var nestedPvsWithIndex = _collectNestedProtoViews(rootRenderProtoView);
var nestedPvVariableNames = _collectNestedProtoViewsVariableNames(nestedPvsWithIndex);
return _getChangeDetectorDefinitions(hostComponentMetadata, nestedPvsWithIndex,
nestedPvVariableNames, allRenderDirectiveMetadata);
nestedPvVariableNames, allRenderDirectiveMetadata,
genConfig);
}
function _collectNestedProtoViews(
@ -285,7 +288,8 @@ function _collectNestedProtoViews(
function _getChangeDetectorDefinitions(
hostComponentMetadata: RenderDirectiveMetadata,
nestedPvsWithIndex: List<RenderProtoViewWithIndex>, nestedPvVariableNames: List<List<string>>,
allRenderDirectiveMetadata: List<RenderDirectiveMetadata>): List<ChangeDetectorDefinition> {
allRenderDirectiveMetadata: List<RenderDirectiveMetadata>, genConfig: ChangeDetectorGenConfig):
List<ChangeDetectorDefinition> {
return ListWrapper.map(nestedPvsWithIndex, (pvWithIndex) => {
var elementBinders = pvWithIndex.renderProtoView.elementBinders;
var bindingRecordsCreator = new BindingRecordsCreator();
@ -302,7 +306,7 @@ function _getChangeDetectorDefinitions(
var id = _protoViewId(hostComponentMetadata, pvWithIndex);
var variableNames = nestedPvVariableNames[pvWithIndex.index];
return new ChangeDetectorDefinition(id, strategyName, variableNames, propBindingRecords,
eventBindingRecords, directiveRecords, assertionsEnabled());
eventBindingRecords, directiveRecords, genConfig);
});
}

View File

@ -80,7 +80,7 @@ class _CodegenState {
final List<EventBinding> _eventBindings;
final CodegenLogicUtil _logic;
final CodegenNameUtil _names;
final bool _devMode;
final ChangeDetectorGenConfig _genConfig;
final List<BindingTarget> _propertyBindingTargets;
_CodegenState._(
@ -94,7 +94,7 @@ class _CodegenState {
this._directiveRecords,
this._logic,
this._names,
this._devMode)
this._genConfig)
: _changeDetectionMode =
ChangeDetectionUtil.changeDetectionMode(changeDetectionStrategy);
@ -117,7 +117,7 @@ class _CodegenState {
def.directiveRecords,
logic,
names,
def.devMode);
def.genConfig);
}
void _writeToBuf(StringBuffer buf) {
@ -170,7 +170,7 @@ class _CodegenState {
}
String _genPropertyBindingTargets() {
var targets = _logic.genPropertyBindingTargets(_propertyBindingTargets, this._devMode);
var targets = _logic.genPropertyBindingTargets(_propertyBindingTargets, this._genConfig.genDebugInfo);
return "static var gen_propertyBindingTargets = ${targets}";
}
@ -418,7 +418,7 @@ class _CodegenState {
}
String _genThrowOnChangeCheck(String oldValue, String newValue) {
if (this._devMode) {
if (this._genConfig.genCheckNoChanges) {
return '''
if(throwOnChange) {
this.throwOnChangeError(${oldValue}, ${newValue});
@ -430,7 +430,7 @@ class _CodegenState {
}
String _genCheckNoChanges() {
if (this._devMode) {
if (this._genConfig.genCheckNoChanges) {
return 'void checkNoChanges() { runDetectChanges(true); }';
} else {
return '';

View File

@ -4,6 +4,7 @@ import 'dart:async';
import 'package:angular2/src/change_detection/parser/lexer.dart' as ng;
import 'package:angular2/src/change_detection/parser/parser.dart' as ng;
import 'package:angular2/src/change_detection/interfaces.dart';
import 'package:angular2/src/core/compiler/proto_view_factory.dart';
import 'package:angular2/src/dom/dom_adapter.dart';
import 'package:angular2/src/render/api.dart';
@ -19,6 +20,7 @@ import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/services/url_resolver.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/xhr_impl.dart';
import 'package:angular2/src/facade/lang.dart';
import 'package:barback/barback.dart';
import 'change_detector_codegen.dart' as change;
@ -56,9 +58,10 @@ Future<String> processTemplates(AssetReader reader, AssetId entryPoint,
}
if (generateChangeDetectors) {
var saved = reflector.reflectionCapabilities;
var genConfig = new ChangeDetectorGenConfig(assertionsEnabled(), assertionsEnabled());
reflector.reflectionCapabilities = const NullReflectionCapabilities();
var defs = getChangeDetectorDefinitions(viewDefEntry.hostMetadata,
protoView, viewDefEntry.viewDef.directives);
protoView, viewDefEntry.viewDef.directives, genConfig);
for (var i = 0; i < defs.length; ++i) {
changeDetectorClasses.generate('${rType.typeName}',
'_${rType.typeName}_ChangeDetector$i', defs[i]);

View File

@ -23,7 +23,7 @@ export function main() {
beforeEach(() => {
proto = new SpyProtoChangeDetector();
def = new ChangeDetectorDefinition('id', null, [], [], [], [], true);
def = new ChangeDetectorDefinition('id', null, [], [], [], [], null);
});
it("should return a proto change detector when one is available", () => {

View File

@ -9,7 +9,8 @@ import {
DirectiveRecord,
Lexer,
Locals,
Parser
Parser,
ChangeDetectorGenConfig
} from 'angular2/src/change_detection/change_detection';
import {reflector} from 'angular2/src/reflection/reflection';
import {ReflectionCapabilities} from 'angular2/src/reflection/reflection_capabilities';
@ -65,6 +66,7 @@ export var PROP_NAME = 'propName';
* In this case, we expect `id` and `expression` to be the same string.
*/
export function getDefinition(id: string): TestDefinition {
var genConfig = new ChangeDetectorGenConfig(true, true);
var testDef = null;
if (StringMapWrapper.contains(_ExpressionWithLocals.availableDefinitions, id)) {
let val = StringMapWrapper.get(_ExpressionWithLocals.availableDefinitions, id);
@ -90,23 +92,23 @@ export function getDefinition(id: string): TestDefinition {
var eventRecords = _createBindingRecords(id);
var directiveRecords = [];
let cdDef = new ChangeDetectorDefinition(id, strategy, variableBindings, eventRecords, [],
directiveRecords, true);
directiveRecords, genConfig);
testDef = new TestDefinition(id, cdDef, null);
} else if (ListWrapper.indexOf(_availableEventDefinitions, id) >= 0) {
var eventRecords = _createEventRecords(id);
let cdDef = new ChangeDetectorDefinition(id, null, [], [], eventRecords, [], true);
let cdDef = new ChangeDetectorDefinition(id, null, [], [], eventRecords, [], genConfig);
testDef = new TestDefinition(id, cdDef, null);
} else if (ListWrapper.indexOf(_availableHostEventDefinitions, id) >= 0) {
var eventRecords = _createHostEventRecords(id, _DirectiveUpdating.basicRecords[0]);
let cdDef = new ChangeDetectorDefinition(id, null, [], [], eventRecords,
[_DirectiveUpdating.basicRecords[0]], true);
[_DirectiveUpdating.basicRecords[0]], genConfig);
testDef = new TestDefinition(id, cdDef, null);
} else if (id == "onPushObserve") {
var records = _createBindingRecords("a");
let cdDef = new ChangeDetectorDefinition(id, "ON_PUSH_OBSERVE", [], records, [], [], false);
let cdDef = new ChangeDetectorDefinition(id, "ON_PUSH_OBSERVE", [], records, [], [], genConfig);
testDef = new TestDefinition(id, cdDef, null);
}
@ -148,8 +150,9 @@ class _ExpressionWithLocals {
var variableBindings = _convertLocalsToVariableBindings(this.locals);
var bindingRecords = _createBindingRecords(this._expression);
var directiveRecords = [];
var genConfig = new ChangeDetectorGenConfig(true, true);
return new ChangeDetectorDefinition('(empty id)', strategy, variableBindings, bindingRecords,
[], directiveRecords, true);
[], directiveRecords, genConfig);
}
/**
@ -207,8 +210,10 @@ class _ExpressionWithMode {
_createHostEventRecords("(host-event)='false'", dirRecordWithOnPush))
}
var genConfig = new ChangeDetectorGenConfig(true, true);
return new ChangeDetectorDefinition('(empty id)', this._strategy, variableBindings,
bindingRecords, eventRecords, directiveRecords, true);
bindingRecords, eventRecords, directiveRecords, genConfig);
}
/**
@ -231,9 +236,11 @@ class _DirectiveUpdating {
createChangeDetectorDefinition(): ChangeDetectorDefinition {
var strategy = null;
var variableBindings = [];
var genConfig = new ChangeDetectorGenConfig(true, true);
return new ChangeDetectorDefinition('(empty id)', strategy, variableBindings,
this._bindingRecords, [], this._directiveRecords, true);
this._bindingRecords, [], this._directiveRecords,
genConfig);
}
static updateA(expression: string, dirRecord): BindingRecord {

View File

@ -980,7 +980,7 @@ export function main() {
});
it("should inject ChangeDetectorRef of the component's view into the component", () => {
var cd = new DynamicChangeDetector(null, null, 0, [], [], null, [], [], []);
var cd = new DynamicChangeDetector(null, null, 0, [], [], null, [], [], [], null);
var view = <any>new DummyView();
var childView = new DummyView();
childView.changeDetector = cd;
@ -993,7 +993,7 @@ export function main() {
});
it("should inject ChangeDetectorRef of the containing component into directives", () => {
var cd = new DynamicChangeDetector(null, null, 0, [], [], null, [], [], []);
var cd = new DynamicChangeDetector(null, null, 0, [], [], null, [], [], [], null);
var view = <any>new DummyView();
view.changeDetector =cd;
var binding = DirectiveBinding.createFromType(DirectiveNeedsChangeDetectorRef, new DirectiveMetadata());

View File

@ -66,7 +66,7 @@ export function main() {
it('should create a ChangeDetectorDefinition for the root render proto view', () => {
var renderPv = createRenderProtoView();
var defs =
getChangeDetectorDefinitions(bindDirective(MainComponent).metadata, renderPv, []);
getChangeDetectorDefinitions(bindDirective(MainComponent).metadata, renderPv, [], null);
expect(defs.length).toBe(1);
expect(defs[0].id).toEqual(`${stringify(MainComponent)}_comp_0`);
});