feat(core): support ngOnDestroy on providers of a directive.

Such providers also become eager as they will be instantiated
anyways on destruction.
This commit is contained in:
Tobias Bosch
2016-08-02 01:37:42 -07:00
parent ff3b71f7b3
commit c161ed415d
10 changed files with 87 additions and 42 deletions

View File

@ -43,7 +43,7 @@ export class CompileElement extends CompileNode {
public appElement: o.ReadPropExpr;
public elementRef: o.Expression;
public injector: o.Expression;
private _instances = new CompileIdentifierMap<CompileTokenMetadata, o.Expression>();
public instances = new CompileIdentifierMap<CompileTokenMetadata, o.Expression>();
private _resolvedProviders: CompileIdentifierMap<CompileTokenMetadata, ProviderAst>;
private _queryCount = 0;
@ -52,7 +52,6 @@ export class CompileElement extends CompileNode {
public contentNodesByNgContentIndex: Array<o.Expression>[] = null;
public embeddedView: CompileView;
public directiveInstances: o.Expression[];
public referenceTokens: {[key: string]: CompileTokenMetadata};
constructor(
@ -66,10 +65,10 @@ export class CompileElement extends CompileNode {
references.forEach(ref => this.referenceTokens[ref.name] = ref.value);
this.elementRef = o.importExpr(Identifiers.ElementRef).instantiate([this.renderNode]);
this._instances.add(identifierToken(Identifiers.ElementRef), this.elementRef);
this.instances.add(identifierToken(Identifiers.ElementRef), this.elementRef);
this.injector = o.THIS_EXPR.callMethod('injector', [o.literal(this.nodeIndex)]);
this._instances.add(identifierToken(Identifiers.Injector), this.injector);
this._instances.add(identifierToken(Identifiers.Renderer), o.THIS_EXPR.prop('renderer'));
this.instances.add(identifierToken(Identifiers.Injector), this.injector);
this.instances.add(identifierToken(Identifiers.Renderer), o.THIS_EXPR.prop('renderer'));
if (this.hasViewContainer || this.hasEmbeddedView || isPresent(this.component)) {
this._createAppElement();
}
@ -89,7 +88,7 @@ export class CompileElement extends CompileNode {
.toStmt();
this.view.createMethod.addStmt(statement);
this.appElement = o.THIS_EXPR.prop(fieldName);
this._instances.add(identifierToken(Identifiers.AppElement), this.appElement);
this.instances.add(identifierToken(Identifiers.AppElement), this.appElement);
}
public createComponentFactoryResolver(entryComponents: CompileIdentifierMetadata[]) {
@ -139,7 +138,7 @@ export class CompileElement extends CompileNode {
beforeChildren(): void {
if (this.hasViewContainer) {
this._instances.add(
this.instances.add(
identifierToken(Identifiers.ViewContainerRef), this.appElement.prop('vcRef'));
}
@ -168,18 +167,16 @@ export class CompileElement extends CompileNode {
return convertValueToOutputAst(provider.useValue);
}
});
var propName = `_${resolvedProvider.token.name}_${this.nodeIndex}_${this._instances.size}`;
var propName = `_${resolvedProvider.token.name}_${this.nodeIndex}_${this.instances.size}`;
var instance = createProviderProperty(
propName, resolvedProvider, providerValueExpressions, resolvedProvider.multiProvider,
resolvedProvider.eager, this);
this._instances.add(resolvedProvider.token, instance);
this.instances.add(resolvedProvider.token, instance);
});
this.directiveInstances =
this._directives.map((directive) => this._instances.get(identifierToken(directive.type)));
for (var i = 0; i < this.directiveInstances.length; i++) {
var directiveInstance = this.directiveInstances[i];
for (var i = 0; i < this._directives.length; i++) {
var directive = this._directives[i];
var directiveInstance = this.instances.get(identifierToken(directive.type));
directive.queries.forEach((queryMeta) => { this._addQuery(queryMeta, directiveInstance); });
}
var queriesWithReads: _QueryWithRead[] = [];
@ -193,7 +190,7 @@ export class CompileElement extends CompileNode {
var token = this.referenceTokens[varName];
var varValue: o.Expression;
if (isPresent(token)) {
varValue = this._instances.get(token);
varValue = this.instances.get(token);
} else {
varValue = this.renderNode;
}
@ -207,12 +204,12 @@ export class CompileElement extends CompileNode {
var value: o.Expression;
if (isPresent(queryWithRead.read.identifier)) {
// query for an identifier
value = this._instances.get(queryWithRead.read);
value = this.instances.get(queryWithRead.read);
} else {
// query for a reference
var token = this.referenceTokens[queryWithRead.read.value];
if (isPresent(token)) {
value = this._instances.get(token);
value = this.instances.get(token);
} else {
value = this.elementRef;
}
@ -241,7 +238,7 @@ export class CompileElement extends CompileNode {
// Note: afterChildren is called after recursing into children.
// This is good so that an injector match in an element that is closer to a requesting element
// matches first.
var providerExpr = this._instances.get(resolvedProvider.token);
var providerExpr = this.instances.get(resolvedProvider.token);
// Note: view providers are only visible on the injector of that element.
// This is not fully correct as the rules during codegen don't allow a directive
// to get hold of a view provdier on the same element. We still do this semantic
@ -263,7 +260,7 @@ export class CompileElement extends CompileNode {
}
getComponent(): o.Expression {
return isPresent(this.component) ? this._instances.get(identifierToken(this.component.type)) :
return isPresent(this.component) ? this.instances.get(identifierToken(this.component.type)) :
null;
}
@ -342,7 +339,7 @@ export class CompileElement extends CompileNode {
resolvedProvider.providerType === ProviderAstType.PrivateService) {
return null;
}
result = this._instances.get(dep.token);
result = this.instances.get(dep.token);
}
}
return result;