Skip to content

Commit 7a7b3a6

Browse files
author
Jason Teplitz
committed
perf(Compiler): do not resolve bindings for cached ProtoViews
1 parent 0949a4b commit 7a7b3a6

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

modules/angular2/src/core/compiler/compiler.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Binding, resolveForwardRef, Injectable} from 'angular2/di';
22
import {
33
Type,
44
isBlank,
5+
isType,
56
isPresent,
67
BaseException,
78
normalizeBlank,
@@ -103,16 +104,18 @@ export class Compiler {
103104
// Create a hostView as if the compiler encountered <hostcmp></hostcmp>.
104105
// Used for bootstrapping.
105106
compileInHost(componentTypeOrBinding: Type | Binding): Promise<ProtoViewRef> {
106-
var componentBinding = this._bindDirective(componentTypeOrBinding);
107-
Compiler._assertTypeIsComponent(componentBinding);
107+
var componentType = isType(componentTypeOrBinding) ? componentTypeOrBinding :
108+
(<Binding>componentTypeOrBinding).token;
108109

109-
var directiveMetadata = componentBinding.metadata;
110+
var hostAppProtoView = this._compilerCache.getHost(componentType);
110111
var hostPvPromise;
111-
var component = <Type>componentBinding.key.token;
112-
var hostAppProtoView = this._compilerCache.getHost(component);
113112
if (isPresent(hostAppProtoView)) {
114113
hostPvPromise = PromiseWrapper.resolve(hostAppProtoView);
115114
} else {
115+
var componentBinding = this._bindDirective(componentTypeOrBinding);
116+
Compiler._assertTypeIsComponent(componentBinding);
117+
118+
var directiveMetadata = componentBinding.metadata;
116119
hostPvPromise = this._render.compileHost(directiveMetadata)
117120
.then((hostRenderPv) => {
118121
return this._compileNestedProtoViews(componentBinding, hostRenderPv,

modules/angular2/test/core/compiler/compiler_spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,26 @@ export function main() {
373373
});
374374
}));
375375

376+
it('should not bind directives for cached components', inject([AsyncTestCompleter], (async) => {
377+
// set up the cache with the test proto view
378+
var mainPv: AppProtoView = createProtoView();
379+
var cache: CompilerCache = new CompilerCache();
380+
cache.setHost(MainComponent, mainPv);
381+
382+
// create the spy resolver
383+
var reader: any = new SpyDirectiveResolver();
384+
385+
// create the compiler
386+
var compiler = new Compiler(reader, cache, tplResolver, cmpUrlMapper, new UrlResolver(),
387+
renderCompiler, protoViewFactory, new FakeAppRootUrl());
388+
compiler.compileInHost(MainComponent)
389+
.then((protoViewRef) => {
390+
// the test should have failed if the resolver was called, so we're good
391+
async.done();
392+
});
393+
}));
394+
395+
376396
it('should cache compiled nested components', inject([AsyncTestCompleter], (async) => {
377397
tplResolver.setView(MainComponent, new viewAnn.View({template: '<div></div>'}));
378398
tplResolver.setView(MainComponent2, new viewAnn.View({template: '<div></div>'}));
@@ -563,6 +583,13 @@ class SpyRenderCompiler extends SpyObject {
563583
noSuchMethod(m) { return super.noSuchMethod(m) }
564584
}
565585

586+
@proxy
587+
@IMPLEMENTS(DirectiveResolver)
588+
class SpyDirectiveResolver extends SpyObject {
589+
constructor() { super(DirectiveResolver); }
590+
noSuchMethod(m) { return super.noSuchMethod(m) }
591+
}
592+
566593
class FakeAppRootUrl extends AppRootUrl {
567594
get value() { return 'http://www.app.com'; }
568595
}

0 commit comments

Comments
 (0)