Skip to content

Commit

Permalink
perf(Compiler): do not resolve bindings for cached ProtoViews
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Teplitz committed Jun 25, 2015
1 parent 0949a4b commit 7a7b3a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
13 changes: 8 additions & 5 deletions modules/angular2/src/core/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Binding, resolveForwardRef, Injectable} from 'angular2/di';
import {
Type,
isBlank,
isType,
isPresent,
BaseException,
normalizeBlank,
Expand Down Expand Up @@ -103,16 +104,18 @@ export class Compiler {
// Create a hostView as if the compiler encountered <hostcmp></hostcmp>.
// Used for bootstrapping.
compileInHost(componentTypeOrBinding: Type | Binding): Promise<ProtoViewRef> {
var componentBinding = this._bindDirective(componentTypeOrBinding);
Compiler._assertTypeIsComponent(componentBinding);
var componentType = isType(componentTypeOrBinding) ? componentTypeOrBinding :
(<Binding>componentTypeOrBinding).token;

var directiveMetadata = componentBinding.metadata;
var hostAppProtoView = this._compilerCache.getHost(componentType);
var hostPvPromise;
var component = <Type>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,
Expand Down
27 changes: 27 additions & 0 deletions modules/angular2/test/core/compiler/compiler_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<div></div>'}));
tplResolver.setView(MainComponent2, new viewAnn.View({template: '<div></div>'}));
Expand Down Expand Up @@ -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'; }
}
Expand Down

0 comments on commit 7a7b3a6

Please sign in to comment.