Revert "feat(core): default to dynamic queries (#32720)" (#32966)

This reverts commit 948b01ce55c7874604016bda87187e8a713d8acc.

PR Close #32966
This commit is contained in:
atscott 2019-10-02 10:21:24 -07:00
parent 879ad69602
commit 04bfe87764

View File

@ -37,6 +37,7 @@ export class ViewCompiler {
outputCtx: OutputContext, component: CompileDirectiveMetadata, template: TemplateAst[], outputCtx: OutputContext, component: CompileDirectiveMetadata, template: TemplateAst[],
styles: o.Expression, usedPipes: CompilePipeSummary[]): ViewCompileResult { styles: o.Expression, usedPipes: CompilePipeSummary[]): ViewCompileResult {
let embeddedViewCount = 0; let embeddedViewCount = 0;
const staticQueryIds = findStaticQueryIds(template);
let renderComponentVarName: string = undefined !; let renderComponentVarName: string = undefined !;
if (!component.isHost) { if (!component.isHost) {
@ -65,7 +66,7 @@ export class ViewCompiler {
const embeddedViewIndex = embeddedViewCount++; const embeddedViewIndex = embeddedViewCount++;
return new ViewBuilder( return new ViewBuilder(
this._reflector, outputCtx, parent, component, embeddedViewIndex, usedPipes, this._reflector, outputCtx, parent, component, embeddedViewIndex, usedPipes,
viewBuilderFactory); staticQueryIds, viewBuilderFactory);
}; };
const visitor = viewBuilderFactory(null); const visitor = viewBuilderFactory(null);
@ -115,6 +116,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
private reflector: CompileReflector, private outputCtx: OutputContext, private reflector: CompileReflector, private outputCtx: OutputContext,
private parent: ViewBuilder|null, private component: CompileDirectiveMetadata, private parent: ViewBuilder|null, private component: CompileDirectiveMetadata,
private embeddedViewIndex: number, private usedPipes: CompilePipeSummary[], private embeddedViewIndex: number, private usedPipes: CompilePipeSummary[],
private staticQueryIds: Map<TemplateAst, StaticAndDynamicQueryIds>,
private viewBuilderFactory: ViewBuilderFactory) { private viewBuilderFactory: ViewBuilderFactory) {
// TODO(tbosch): The old view compiler used to use an `any` type // 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 // for the context in any embedded view. We keep this behaivor for now
@ -137,11 +139,13 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
} }
if (!this.parent) { if (!this.parent) {
const queryIds = staticViewQueryIds(this.staticQueryIds);
this.component.viewQueries.forEach((query, queryIndex) => { this.component.viewQueries.forEach((query, queryIndex) => {
// Note: queries start with id 1 so we can use the number in a Bloom filter! // Note: queries start with id 1 so we can use the number in a Bloom filter!
const queryId = queryIndex + 1; const queryId = queryIndex + 1;
const bindingType = query.first ? QueryBindingType.First : QueryBindingType.All; const bindingType = query.first ? QueryBindingType.First : QueryBindingType.All;
const flags = NodeFlags.TypeViewQuery | calcStaticDynamicQueryFlags(query); const flags =
NodeFlags.TypeViewQuery | calcStaticDynamicQueryFlags(queryIds, queryId, query);
this.nodes.push(() => ({ this.nodes.push(() => ({
sourceSpan: null, sourceSpan: null,
nodeFlags: flags, nodeFlags: flags,
@ -408,16 +412,19 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
const hostEvents: {context: o.Expression, eventAst: BoundEventAst, dirAst: DirectiveAst}[] = []; const hostEvents: {context: o.Expression, eventAst: BoundEventAst, dirAst: DirectiveAst}[] = [];
this._visitComponentFactoryResolverProvider(ast.directives); this._visitComponentFactoryResolverProvider(ast.directives);
ast.providers.forEach(providerAst => { ast.providers.forEach((providerAst, providerIndex) => {
let dirAst: DirectiveAst = undefined !; let dirAst: DirectiveAst = undefined !;
ast.directives.forEach(localDirAst => { let dirIndex: number = undefined !;
ast.directives.forEach((localDirAst, i) => {
if (localDirAst.directive.type.reference === tokenReference(providerAst.token)) { if (localDirAst.directive.type.reference === tokenReference(providerAst.token)) {
dirAst = localDirAst; dirAst = localDirAst;
dirIndex = i;
} }
}); });
if (dirAst) { if (dirAst) {
const {hostBindings: dirHostBindings, hostEvents: dirHostEvents} = const {hostBindings: dirHostBindings, hostEvents: dirHostEvents} = this._visitDirective(
this._visitDirective(providerAst, dirAst, ast.references, ast.queryMatches, usedEvents); providerAst, dirAst, dirIndex, nodeIndex, ast.references, ast.queryMatches, usedEvents,
this.staticQueryIds.get(<any>ast) !);
hostBindings.push(...dirHostBindings); hostBindings.push(...dirHostBindings);
hostEvents.push(...dirHostEvents); hostEvents.push(...dirHostEvents);
} else { } else {
@ -472,8 +479,9 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
} }
private _visitDirective( private _visitDirective(
providerAst: ProviderAst, dirAst: DirectiveAst, refs: ReferenceAst[], providerAst: ProviderAst, dirAst: DirectiveAst, directiveIndex: number,
queryMatches: QueryMatch[], usedEvents: Map<string, any>): { elementNodeIndex: number, refs: ReferenceAst[], queryMatches: QueryMatch[],
usedEvents: Map<string, any>, queryIds: StaticAndDynamicQueryIds): {
hostBindings: hostBindings:
{context: o.Expression, inputAst: BoundElementPropertyAst, dirAst: DirectiveAst}[], {context: o.Expression, inputAst: BoundElementPropertyAst, dirAst: DirectiveAst}[],
hostEvents: {context: o.Expression, eventAst: BoundEventAst, dirAst: DirectiveAst}[] hostEvents: {context: o.Expression, eventAst: BoundEventAst, dirAst: DirectiveAst}[]
@ -484,7 +492,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
dirAst.directive.queries.forEach((query, queryIndex) => { dirAst.directive.queries.forEach((query, queryIndex) => {
const queryId = dirAst.contentQueryStartId + queryIndex; const queryId = dirAst.contentQueryStartId + queryIndex;
const flags = NodeFlags.TypeContentQuery | calcStaticDynamicQueryFlags(query); const flags =
NodeFlags.TypeContentQuery | calcStaticDynamicQueryFlags(queryIds, queryId, query);
const bindingType = query.first ? QueryBindingType.First : QueryBindingType.All; const bindingType = query.first ? QueryBindingType.First : QueryBindingType.All;
this.nodes.push(() => ({ this.nodes.push(() => ({
sourceSpan: dirAst.sourceSpan, sourceSpan: dirAst.sourceSpan,
@ -610,6 +619,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
tokenExpr: o.Expression, tokenExpr: o.Expression,
sourceSpan: ParseSourceSpan sourceSpan: ParseSourceSpan
}) { }) {
const nodeIndex = this.nodes.length;
// providerDef( // providerDef(
// flags: NodeFlags, matchedQueries: [string, QueryValueType][], token:any, // flags: NodeFlags, matchedQueries: [string, QueryValueType][], token:any,
// value: any, deps: ([DepFlags, any] | any)[]): NodeDef; // value: any, deps: ([DepFlags, any] | any)[]): NodeDef;
@ -1076,11 +1086,12 @@ function elementEventNameAndTarget(
} }
} }
function calcStaticDynamicQueryFlags(query: CompileQueryMetadata) { function calcStaticDynamicQueryFlags(
queryIds: StaticAndDynamicQueryIds, queryId: number, query: CompileQueryMetadata) {
let flags = NodeFlags.None; let flags = NodeFlags.None;
// Note: We only make queries static that query for a single item and the user specifically // Note: We only make queries static that query for a single item.
// set the to be static. This is because of backwards compatibility with the old view compiler... // This is because of backwards compatibility with the old view compiler...
if (query.first && query.static) { if (query.first && shouldResolveAsStaticQuery(queryIds, queryId, query)) {
flags |= NodeFlags.StaticQuery; flags |= NodeFlags.StaticQuery;
} else { } else {
flags |= NodeFlags.DynamicQuery; flags |= NodeFlags.DynamicQuery;
@ -1088,6 +1099,16 @@ function calcStaticDynamicQueryFlags(query: CompileQueryMetadata) {
return flags; 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 { export function elementEventFullName(target: string | null, name: string): string {
return target ? `${target}:${name}` : name; return target ? `${target}:${name}` : name;
} }