Skip to content

Commit b613f90

Browse files
pkozlowski-opensourcejasonaden
authored andcommitted
refactor(ivy): avoid calling query instructions from static query instructions (angular#30587)
PR Close angular#30587
1 parent 2fe6f35 commit b613f90

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

packages/core/src/render3/query.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {unusedValueExportToPlacateAjd as unused1} from './interfaces/definition'
2424
import {unusedValueExportToPlacateAjd as unused2} from './interfaces/injector';
2525
import {TContainerNode, TElementContainerNode, TElementNode, TNode, TNodeType, unusedValueExportToPlacateAjd as unused3} from './interfaces/node';
2626
import {LQueries, unusedValueExportToPlacateAjd as unused4} from './interfaces/query';
27-
import {CONTENT_QUERIES, HEADER_OFFSET, LView, QUERIES, TVIEW} from './interfaces/view';
27+
import {CONTENT_QUERIES, HEADER_OFFSET, LView, QUERIES, TVIEW, TView} from './interfaces/view';
2828
import {getCurrentQueryIndex, getIsParent, getLView, isCreationMode, setCurrentQueryIndex} from './state';
2929
import {loadInternal} from './util/view_utils';
3030
import {createElementRef, createTemplateRef} from './view_engine_compatibility';
@@ -370,12 +370,13 @@ type QueryList_<T> = QueryList<T>& {_valuesTree: any[], _static: boolean};
370370
*/
371371
function query<T>(
372372
// TODO: "read" should be an AbstractType (FW-486)
373-
lView: LView, predicate: Type<any>| string[], descend: boolean, read: any): QueryList<T> {
373+
lView: LView, predicate: Type<any>| string[], descend: boolean, read: any,
374+
isStatic: boolean): QueryList<T> {
374375
ngDevMode && assertPreviousIsParent(getIsParent());
375376
const queryList = new QueryList<T>() as QueryList_<T>;
376377
const queries = lView[QUERIES] || (lView[QUERIES] = new LQueries_(null, null, null));
377378
queryList._valuesTree = [];
378-
queryList._static = false;
379+
queryList._static = isStatic;
379380
queries.track(queryList, predicate, descend, read);
380381
storeCleanupWithContext(lView, queryList, queryList.destroy);
381382
return queryList;
@@ -415,12 +416,10 @@ export function ɵɵqueryRefresh(queryList: QueryList<any>): boolean {
415416
export function ɵɵstaticViewQuery<T>(
416417
// TODO(FW-486): "read" should be an AbstractType
417418
predicate: Type<any>| string[], descend: boolean, read: any): void {
418-
const queryList = ɵɵviewQuery(predicate, descend, read) as QueryList_<T>;
419-
const tView = getLView()[TVIEW];
420-
queryList._static = true;
421-
if (!tView.staticViewQueries) {
422-
tView.staticViewQueries = true;
423-
}
419+
const lView = getLView();
420+
const tView = lView[TVIEW];
421+
viewQueryInternal(lView, tView, predicate, descend, read, true);
422+
tView.staticViewQueries = true;
424423
}
425424

426425
/**
@@ -438,14 +437,20 @@ export function ɵɵviewQuery<T>(
438437
predicate: Type<any>| string[], descend: boolean, read: any): QueryList<T> {
439438
const lView = getLView();
440439
const tView = lView[TVIEW];
440+
return viewQueryInternal(lView, tView, predicate, descend, read, false);
441+
}
442+
443+
function viewQueryInternal<T>(
444+
lView: LView, tView: TView, predicate: Type<any>| string[], descend: boolean, read: any,
445+
isStatic: boolean): QueryList<T> {
441446
if (tView.firstTemplatePass) {
442447
tView.expandoStartIndex++;
443448
}
444449
const index = getCurrentQueryIndex();
445-
const viewQuery: QueryList<T> = query<T>(lView, predicate, descend, read);
446-
store(index - HEADER_OFFSET, viewQuery);
450+
const queryList: QueryList<T> = query<T>(lView, predicate, descend, read, isStatic);
451+
store(index - HEADER_OFFSET, queryList);
447452
setCurrentQueryIndex(index + 1);
448-
return viewQuery;
453+
return queryList;
449454
}
450455

451456
/**
@@ -477,7 +482,15 @@ export function ɵɵcontentQuery<T>(
477482
read: any): QueryList<T> {
478483
const lView = getLView();
479484
const tView = lView[TVIEW];
480-
const contentQuery: QueryList<T> = query<T>(lView, predicate, descend, read);
485+
return contentQueryInternal(lView, tView, directiveIndex, predicate, descend, read, false);
486+
}
487+
488+
function contentQueryInternal<T>(
489+
lView: LView, tView: TView, directiveIndex: number, predicate: Type<any>| string[],
490+
descend: boolean,
491+
// TODO(FW-486): "read" should be an AbstractType
492+
read: any, isStatic: boolean): QueryList<T> {
493+
const contentQuery: QueryList<T> = query<T>(lView, predicate, descend, read, isStatic);
481494
(lView[CONTENT_QUERIES] || (lView[CONTENT_QUERIES] = [])).push(contentQuery);
482495
if (tView.firstTemplatePass) {
483496
const tViewContentQueries = tView.contentQueries || (tView.contentQueries = []);
@@ -506,12 +519,10 @@ export function ɵɵstaticContentQuery<T>(
506519
directiveIndex: number, predicate: Type<any>| string[], descend: boolean,
507520
// TODO(FW-486): "read" should be an AbstractType
508521
read: any): void {
509-
const queryList = ɵɵcontentQuery(directiveIndex, predicate, descend, read) as QueryList_<T>;
510-
const tView = getLView()[TVIEW];
511-
queryList._static = true;
512-
if (!tView.staticContentQueries) {
513-
tView.staticContentQueries = true;
514-
}
522+
const lView = getLView();
523+
const tView = lView[TVIEW];
524+
contentQueryInternal(lView, tView, directiveIndex, predicate, descend, read, true);
525+
tView.staticContentQueries = true;
515526
}
516527

517528
/**

0 commit comments

Comments
 (0)