From 7a7b3a6cb9a996e8701dad61fd454baf918cc92a Mon Sep 17 00:00:00 2001 From: Jason Teplitz Date: Thu, 25 Jun 2015 13:19:51 -0700 Subject: [PATCH] perf(Compiler): do not resolve bindings for cached ProtoViews --- .../angular2/src/core/compiler/compiler.ts | 13 +++++---- .../test/core/compiler/compiler_spec.ts | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/modules/angular2/src/core/compiler/compiler.ts b/modules/angular2/src/core/compiler/compiler.ts index 6386e175f7..cf772db5ed 100644 --- a/modules/angular2/src/core/compiler/compiler.ts +++ b/modules/angular2/src/core/compiler/compiler.ts @@ -2,6 +2,7 @@ import {Binding, resolveForwardRef, Injectable} from 'angular2/di'; import { Type, isBlank, + isType, isPresent, BaseException, normalizeBlank, @@ -103,16 +104,18 @@ export class Compiler { // Create a hostView as if the compiler encountered . // Used for bootstrapping. compileInHost(componentTypeOrBinding: Type | Binding): Promise { - var componentBinding = this._bindDirective(componentTypeOrBinding); - Compiler._assertTypeIsComponent(componentBinding); + var componentType = isType(componentTypeOrBinding) ? componentTypeOrBinding : + (componentTypeOrBinding).token; - var directiveMetadata = componentBinding.metadata; + var hostAppProtoView = this._compilerCache.getHost(componentType); var hostPvPromise; - var component = componentBinding.key.token; - var hostAppProtoView = this._compilerCache.getHost(component); if (isPresent(hostAppProtoView)) { hostPvPromise = PromiseWrapper.resolve(hostAppProtoView); } else { + var componentBinding = this._bindDirective(componentTypeOrBinding); + Compiler._assertTypeIsComponent(componentBinding); + + var directiveMetadata = componentBinding.metadata; hostPvPromise = this._render.compileHost(directiveMetadata) .then((hostRenderPv) => { return this._compileNestedProtoViews(componentBinding, hostRenderPv, diff --git a/modules/angular2/test/core/compiler/compiler_spec.ts b/modules/angular2/test/core/compiler/compiler_spec.ts index 8bbf1f75dc..c5c93b61cd 100644 --- a/modules/angular2/test/core/compiler/compiler_spec.ts +++ b/modules/angular2/test/core/compiler/compiler_spec.ts @@ -373,6 +373,26 @@ export function main() { }); })); + it('should not bind directives for cached components', inject([AsyncTestCompleter], (async) => { + // set up the cache with the test proto view + var mainPv: AppProtoView = createProtoView(); + var cache: CompilerCache = new CompilerCache(); + cache.setHost(MainComponent, mainPv); + + // create the spy resolver + var reader: any = new SpyDirectiveResolver(); + + // create the compiler + var compiler = new Compiler(reader, cache, tplResolver, cmpUrlMapper, new UrlResolver(), + renderCompiler, protoViewFactory, new FakeAppRootUrl()); + compiler.compileInHost(MainComponent) + .then((protoViewRef) => { + // the test should have failed if the resolver was called, so we're good + async.done(); + }); + })); + + it('should cache compiled nested components', inject([AsyncTestCompleter], (async) => { tplResolver.setView(MainComponent, new viewAnn.View({template: '
'})); tplResolver.setView(MainComponent2, new viewAnn.View({template: '
'})); @@ -563,6 +583,13 @@ class SpyRenderCompiler extends SpyObject { noSuchMethod(m) { return super.noSuchMethod(m) } } +@proxy +@IMPLEMENTS(DirectiveResolver) +class SpyDirectiveResolver extends SpyObject { + constructor() { super(DirectiveResolver); } + noSuchMethod(m) { return super.noSuchMethod(m) } +} + class FakeAppRootUrl extends AppRootUrl { get value() { return 'http://www.app.com'; } }