Skip to content

Commit

Permalink
feat(core): default to dynamic queries (angular#32720)
Browse files Browse the repository at this point in the history
These changes switch to defaulting the `static` flag on `ViewChild` and `ContentChild` queries to `false`, in addition to removing the logic that statically determines whether a query is dynamic.

PR Close angular#32720
  • Loading branch information
crisbeto authored and atscott committed Oct 3, 2019
1 parent deaac32 commit 7806596
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions packages/compiler/src/view_compiler/view_compiler.ts
Expand Up @@ -37,7 +37,6 @@ export class ViewCompiler {
outputCtx: OutputContext, component: CompileDirectiveMetadata, template: TemplateAst[],
styles: o.Expression, usedPipes: CompilePipeSummary[]): ViewCompileResult {
let embeddedViewCount = 0;
const staticQueryIds = findStaticQueryIds(template);

let renderComponentVarName: string = undefined !;
if (!component.isHost) {
Expand Down Expand Up @@ -66,7 +65,7 @@ export class ViewCompiler {
const embeddedViewIndex = embeddedViewCount++;
return new ViewBuilder(
this._reflector, outputCtx, parent, component, embeddedViewIndex, usedPipes,
staticQueryIds, viewBuilderFactory);
viewBuilderFactory);
};

const visitor = viewBuilderFactory(null);
Expand Down Expand Up @@ -116,7 +115,6 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
private reflector: CompileReflector, private outputCtx: OutputContext,
private parent: ViewBuilder|null, private component: CompileDirectiveMetadata,
private embeddedViewIndex: number, private usedPipes: CompilePipeSummary[],
private staticQueryIds: Map<TemplateAst, StaticAndDynamicQueryIds>,
private viewBuilderFactory: ViewBuilderFactory) {
// TODO(tbosch): The old view compiler used to use an `any` type
// for the context in any embedded view. We keep this behaivor for now
Expand All @@ -139,13 +137,11 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
}

if (!this.parent) {
const queryIds = staticViewQueryIds(this.staticQueryIds);
this.component.viewQueries.forEach((query, queryIndex) => {
// Note: queries start with id 1 so we can use the number in a Bloom filter!
const queryId = queryIndex + 1;
const bindingType = query.first ? QueryBindingType.First : QueryBindingType.All;
const flags =
NodeFlags.TypeViewQuery | calcStaticDynamicQueryFlags(queryIds, queryId, query);
const flags = NodeFlags.TypeViewQuery | calcStaticDynamicQueryFlags(query);
this.nodes.push(() => ({
sourceSpan: null,
nodeFlags: flags,
Expand Down Expand Up @@ -412,19 +408,16 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
const hostEvents: {context: o.Expression, eventAst: BoundEventAst, dirAst: DirectiveAst}[] = [];
this._visitComponentFactoryResolverProvider(ast.directives);

ast.providers.forEach((providerAst, providerIndex) => {
ast.providers.forEach(providerAst => {
let dirAst: DirectiveAst = undefined !;
let dirIndex: number = undefined !;
ast.directives.forEach((localDirAst, i) => {
ast.directives.forEach(localDirAst => {
if (localDirAst.directive.type.reference === tokenReference(providerAst.token)) {
dirAst = localDirAst;
dirIndex = i;
}
});
if (dirAst) {
const {hostBindings: dirHostBindings, hostEvents: dirHostEvents} = this._visitDirective(
providerAst, dirAst, dirIndex, nodeIndex, ast.references, ast.queryMatches, usedEvents,
this.staticQueryIds.get(<any>ast) !);
const {hostBindings: dirHostBindings, hostEvents: dirHostEvents} =
this._visitDirective(providerAst, dirAst, ast.references, ast.queryMatches, usedEvents);
hostBindings.push(...dirHostBindings);
hostEvents.push(...dirHostEvents);
} else {
Expand Down Expand Up @@ -479,9 +472,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
}

private _visitDirective(
providerAst: ProviderAst, dirAst: DirectiveAst, directiveIndex: number,
elementNodeIndex: number, refs: ReferenceAst[], queryMatches: QueryMatch[],
usedEvents: Map<string, any>, queryIds: StaticAndDynamicQueryIds): {
providerAst: ProviderAst, dirAst: DirectiveAst, refs: ReferenceAst[],
queryMatches: QueryMatch[], usedEvents: Map<string, any>): {
hostBindings:
{context: o.Expression, inputAst: BoundElementPropertyAst, dirAst: DirectiveAst}[],
hostEvents: {context: o.Expression, eventAst: BoundEventAst, dirAst: DirectiveAst}[]
Expand All @@ -492,8 +484,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {

dirAst.directive.queries.forEach((query, queryIndex) => {
const queryId = dirAst.contentQueryStartId + queryIndex;
const flags =
NodeFlags.TypeContentQuery | calcStaticDynamicQueryFlags(queryIds, queryId, query);
const flags = NodeFlags.TypeContentQuery | calcStaticDynamicQueryFlags(query);
const bindingType = query.first ? QueryBindingType.First : QueryBindingType.All;
this.nodes.push(() => ({
sourceSpan: dirAst.sourceSpan,
Expand Down Expand Up @@ -619,7 +610,6 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
tokenExpr: o.Expression,
sourceSpan: ParseSourceSpan
}) {
const nodeIndex = this.nodes.length;
// providerDef(
// flags: NodeFlags, matchedQueries: [string, QueryValueType][], token:any,
// value: any, deps: ([DepFlags, any] | any)[]): NodeDef;
Expand Down Expand Up @@ -1086,29 +1076,18 @@ function elementEventNameAndTarget(
}
}

function calcStaticDynamicQueryFlags(
queryIds: StaticAndDynamicQueryIds, queryId: number, query: CompileQueryMetadata) {
function calcStaticDynamicQueryFlags(query: CompileQueryMetadata) {
let flags = NodeFlags.None;
// Note: We only make queries static that query for a single item.
// This is because of backwards compatibility with the old view compiler...
if (query.first && shouldResolveAsStaticQuery(queryIds, queryId, query)) {
// Note: We only make queries static that query for a single item and the user specifically
// set the to be static. This is because of backwards compatibility with the old view compiler...
if (query.first && query.static) {
flags |= NodeFlags.StaticQuery;
} else {
flags |= NodeFlags.DynamicQuery;
}
return flags;
}

function shouldResolveAsStaticQuery(
queryIds: StaticAndDynamicQueryIds, queryId: number, query: CompileQueryMetadata): boolean {
// If query.static has been set by the user, use that value to determine whether
// the query is static. If none has been set, sort the query into static/dynamic
// based on query results (i.e. dynamic if CD needs to run to get all results).
return query.static ||
query.static == null &&
(queryIds.staticQueryIds.has(queryId) || !queryIds.dynamicQueryIds.has(queryId));
}

export function elementEventFullName(target: string | null, name: string): string {
return target ? `${target}:${name}` : name;
}

0 comments on commit 7806596

Please sign in to comment.