2015-09-18 10:33:23 -07:00
|
|
|
import {CompileTypeMetadata} from './directive_metadata';
|
|
|
|
import {SourceExpressions, moduleRef} from './source_module';
|
2015-09-08 09:57:51 -07:00
|
|
|
import {
|
|
|
|
ChangeDetectorJITGenerator
|
|
|
|
} from 'angular2/src/core/change_detection/change_detection_jit_generator';
|
2015-12-02 10:35:51 -08:00
|
|
|
import {AbstractChangeDetector} from 'angular2/src/core/change_detection/abstract_change_detector';
|
|
|
|
import {ChangeDetectionUtil} from 'angular2/src/core/change_detection/change_detection_util';
|
|
|
|
import {ChangeDetectorState} from 'angular2/src/core/change_detection/constants';
|
2015-09-08 09:57:51 -07:00
|
|
|
|
|
|
|
import {createChangeDetectorDefinitions} from './change_definition_factory';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {IS_DART, isJsObject, CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-09-08 09:57:51 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
ChangeDetectorGenConfig,
|
|
|
|
ChangeDetectorDefinition,
|
|
|
|
DynamicProtoChangeDetector,
|
|
|
|
ChangeDetectionStrategy
|
|
|
|
} from 'angular2/src/core/change_detection/change_detection';
|
|
|
|
|
|
|
|
import {TemplateAst} from './template_ast';
|
|
|
|
import {Codegen} from 'angular2/src/transform/template_compiler/change_detector_codegen';
|
2015-11-05 14:07:57 -08:00
|
|
|
import {MODULE_SUFFIX} from './util';
|
2015-09-14 15:59:09 -07:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-09-08 09:57:51 -07:00
|
|
|
|
|
|
|
const ABSTRACT_CHANGE_DETECTOR = "AbstractChangeDetector";
|
|
|
|
const UTIL = "ChangeDetectionUtil";
|
2015-10-27 10:55:01 -07:00
|
|
|
const CHANGE_DETECTOR_STATE = "ChangeDetectorState";
|
2015-09-08 09:57:51 -07:00
|
|
|
|
2015-12-02 10:35:51 -08:00
|
|
|
export const CHANGE_DETECTION_JIT_IMPORTS = CONST_EXPR({
|
|
|
|
'AbstractChangeDetector': AbstractChangeDetector,
|
|
|
|
'ChangeDetectionUtil': ChangeDetectionUtil,
|
|
|
|
'ChangeDetectorState': ChangeDetectorState
|
|
|
|
});
|
|
|
|
|
2015-10-01 10:07:49 -07:00
|
|
|
var ABSTRACT_CHANGE_DETECTOR_MODULE = moduleRef(
|
|
|
|
`package:angular2/src/core/change_detection/abstract_change_detector${MODULE_SUFFIX}`);
|
|
|
|
var UTIL_MODULE =
|
|
|
|
moduleRef(`package:angular2/src/core/change_detection/change_detection_util${MODULE_SUFFIX}`);
|
|
|
|
var PREGEN_PROTO_CHANGE_DETECTOR_MODULE = moduleRef(
|
|
|
|
`package:angular2/src/core/change_detection/pregen_proto_change_detector${MODULE_SUFFIX}`);
|
2015-10-27 10:55:01 -07:00
|
|
|
var CONSTANTS_MODULE =
|
|
|
|
moduleRef(`package:angular2/src/core/change_detection/constants${MODULE_SUFFIX}`);
|
2015-09-08 09:57:51 -07:00
|
|
|
|
2015-09-14 15:59:09 -07:00
|
|
|
@Injectable()
|
2015-09-08 09:57:51 -07:00
|
|
|
export class ChangeDetectionCompiler {
|
|
|
|
constructor(private _genConfig: ChangeDetectorGenConfig) {}
|
|
|
|
|
2015-09-18 10:33:23 -07:00
|
|
|
compileComponentRuntime(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
|
2015-09-08 09:57:51 -07:00
|
|
|
parsedTemplate: TemplateAst[]): Function[] {
|
|
|
|
var changeDetectorDefinitions =
|
|
|
|
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
|
|
|
|
return changeDetectorDefinitions.map(definition =>
|
|
|
|
this._createChangeDetectorFactory(definition));
|
|
|
|
}
|
|
|
|
|
|
|
|
private _createChangeDetectorFactory(definition: ChangeDetectorDefinition): Function {
|
2015-12-02 10:35:51 -08:00
|
|
|
var proto = new DynamicProtoChangeDetector(definition);
|
|
|
|
return () => proto.instantiate();
|
2015-09-08 09:57:51 -07:00
|
|
|
}
|
|
|
|
|
2015-09-18 10:33:23 -07:00
|
|
|
compileComponentCodeGen(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
|
|
|
|
parsedTemplate: TemplateAst[]): SourceExpressions {
|
2015-09-08 09:57:51 -07:00
|
|
|
var changeDetectorDefinitions =
|
|
|
|
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
|
|
|
|
var factories = [];
|
2015-10-01 10:07:49 -07:00
|
|
|
var index = 0;
|
2015-09-08 09:57:51 -07:00
|
|
|
var sourceParts = changeDetectorDefinitions.map(definition => {
|
|
|
|
var codegen: any;
|
2015-11-02 08:39:14 -08:00
|
|
|
var sourcePart: string;
|
2015-09-08 09:57:51 -07:00
|
|
|
// TODO(tbosch): move the 2 code generators to the same place, one with .dart and one with .ts
|
|
|
|
// suffix
|
|
|
|
// and have the same API for calling them!
|
|
|
|
if (IS_DART) {
|
2015-09-14 15:59:09 -07:00
|
|
|
codegen = new Codegen(PREGEN_PROTO_CHANGE_DETECTOR_MODULE);
|
2015-10-29 11:24:02 -07:00
|
|
|
var className = `_${definition.id}`;
|
2015-10-01 10:07:49 -07:00
|
|
|
var typeRef = (index === 0 && componentType.isHost) ?
|
|
|
|
'dynamic' :
|
|
|
|
`${moduleRef(componentType.moduleUrl)}${componentType.name}`;
|
|
|
|
codegen.generate(typeRef, className, definition);
|
2015-11-02 08:39:14 -08:00
|
|
|
factories.push(`${className}.newChangeDetector`);
|
2015-10-01 10:07:49 -07:00
|
|
|
sourcePart = codegen.toString();
|
2015-09-08 09:57:51 -07:00
|
|
|
} else {
|
2015-09-14 15:59:09 -07:00
|
|
|
codegen = new ChangeDetectorJITGenerator(
|
|
|
|
definition, `${UTIL_MODULE}${UTIL}`,
|
2015-10-27 10:55:01 -07:00
|
|
|
`${ABSTRACT_CHANGE_DETECTOR_MODULE}${ABSTRACT_CHANGE_DETECTOR}`,
|
|
|
|
`${CONSTANTS_MODULE}${CHANGE_DETECTOR_STATE}`);
|
2015-12-02 10:35:51 -08:00
|
|
|
factories.push(`function() { return new ${codegen.typeName}(); }`);
|
2015-10-01 10:07:49 -07:00
|
|
|
sourcePart = codegen.generateSource();
|
2015-09-08 09:57:51 -07:00
|
|
|
}
|
2015-10-01 10:07:49 -07:00
|
|
|
index++;
|
|
|
|
return sourcePart;
|
2015-09-08 09:57:51 -07:00
|
|
|
});
|
2015-09-18 10:33:23 -07:00
|
|
|
return new SourceExpressions(sourceParts, factories);
|
2015-09-08 09:57:51 -07:00
|
|
|
}
|
|
|
|
}
|