Tobias Bosch 2f87eb52fe feat(core): add initial view engine (#14014)
The new view engine allows our codegen to produce less code,
as it can interpret view definitions during runtime.

The view engine is not feature complete yet, but already
allows to implement a tree benchmark based on it.

Part of #14013
2017-01-20 13:10:57 -08:00

63 lines
2.3 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {devModeEqual} from '../change_detection/change_detection';
import {SimpleChange} from '../change_detection/change_detection_util';
import {looseIdentical} from '../facade/lang';
import {ExpressionChangedAfterItHasBeenCheckedError} from '../linker/errors';
import {Renderer} from '../render/api';
import {NodeData, NodeDef, NodeFlags, NodeType, ViewData, ViewDefinition} from './types';
export function setBindingDebugInfo(
renderer: Renderer, renderNode: any, propName: string, value: any) {
try {
renderer.setBindingDebugInfo(
renderNode, `ng-reflect-${camelCaseToDashCase(propName)}`, value ? value.toString() : null);
} catch (e) {
renderer.setBindingDebugInfo(
renderNode, `ng-reflect-${camelCaseToDashCase(propName)}`,
'[ERROR] Exception while trying to serialize the value');
}
}
const CAMEL_CASE_REGEXP = /([A-Z])/g;
function camelCaseToDashCase(input: string): string {
return input.replace(CAMEL_CASE_REGEXP, (...m: any[]) => '-' + m[1].toLowerCase());
}
export function checkBindingNoChanges(
view: ViewData, def: NodeDef, bindingIdx: number, value: any) {
const oldValue = view.oldValues[def.bindingIndex + bindingIdx];
if (view.firstChange || !devModeEqual(oldValue, value)) {
throw new ExpressionChangedAfterItHasBeenCheckedError(oldValue, value, view.firstChange);
}
}
export function checkAndUpdateBinding(
view: ViewData, def: NodeDef, bindingIdx: number, value: any): boolean {
const oldValues = view.oldValues;
if (view.firstChange || !looseIdentical(oldValues[def.bindingIndex + bindingIdx], value)) {
oldValues[def.bindingIndex + bindingIdx] = value;
return true;
}
return false;
}
export function checkAndUpdateBindingWithChange(
view: ViewData, def: NodeDef, bindingIdx: number, value: any): SimpleChange {
const oldValues = view.oldValues;
const oldValue = oldValues[def.bindingIndex + bindingIdx];
if (view.firstChange || !looseIdentical(oldValue, value)) {
oldValues[def.bindingIndex + bindingIdx] = value;
return new SimpleChange(oldValue, value, view.firstChange);
}
return null;
}