refactor(ivy): reduce code duplication and code size (#31489)
PR Close #31489
This commit is contained in:
parent
d52ae7cbab
commit
cb848b9410
|
@ -194,32 +194,23 @@ class TQuery_ implements TQuery {
|
||||||
|
|
||||||
private matchTNode(tView: TView, tNode: TNode): void {
|
private matchTNode(tView: TView, tNode: TNode): void {
|
||||||
if (Array.isArray(this.metadata.predicate)) {
|
if (Array.isArray(this.metadata.predicate)) {
|
||||||
this.matchTNodeByLocalNames(tView, tNode, this.metadata.predicate as string[]);
|
const localNames = this.metadata.predicate as string[];
|
||||||
|
for (let i = 0; i < localNames.length; i++) {
|
||||||
|
this.matchTNodeWithReadOption(tView, tNode, getIdxOfMatchingSelector(tNode, localNames[i]));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const typePredicate = this.metadata.predicate as any;
|
const typePredicate = this.metadata.predicate as any;
|
||||||
if (typePredicate === ViewEngine_TemplateRef) {
|
if (typePredicate === ViewEngine_TemplateRef) {
|
||||||
this.matchTNodeByTemplateRef(tView, tNode);
|
if (tNode.type === TNodeType.Container) {
|
||||||
|
this.matchTNodeWithReadOption(tView, tNode, -1);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.matchTNodeByType(tView, tNode, typePredicate);
|
this.matchTNodeWithReadOption(
|
||||||
|
tView, tNode, locateDirectiveOrProvider(tNode, tView, typePredicate, false, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private matchTNodeByLocalNames(tView: TView, tNode: TNode, localNames: string[]): void {
|
|
||||||
for (let i = 0; i < localNames.length; i++) {
|
|
||||||
this.matchTNodeWithReadOption(tView, tNode, getIdxOfMatchingSelector(tNode, localNames[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private matchTNodeByTemplateRef(tView: TView, tNode: TNode): void {
|
|
||||||
this.matchTNodeWithReadOption(tView, tNode, tNode.type === TNodeType.Container ? -1 : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private matchTNodeByType(tView: TView, tNode: TNode, typePredicate: Type<any>): void {
|
|
||||||
this.matchTNodeWithReadOption(
|
|
||||||
tView, tNode, locateDirectiveOrProvider(tNode, tView, typePredicate, false, false));
|
|
||||||
}
|
|
||||||
|
|
||||||
private matchTNodeWithReadOption(tView: TView, tNode: TNode, nodeMatchIdx: number|null): void {
|
private matchTNodeWithReadOption(tView: TView, tNode: TNode, nodeMatchIdx: number|null): void {
|
||||||
if (nodeMatchIdx !== null) {
|
if (nodeMatchIdx !== null) {
|
||||||
const read = this.metadata.read;
|
const read = this.metadata.read;
|
||||||
|
@ -346,45 +337,37 @@ function materializeViewResults<T>(lView: LView, tQuery: TQuery, queryIndex: num
|
||||||
* A helper function that collects (already materialized) query results from a tree of views,
|
* A helper function that collects (already materialized) query results from a tree of views,
|
||||||
* starting with a provided LView.
|
* starting with a provided LView.
|
||||||
*/
|
*/
|
||||||
function collectQueryResults<T>(
|
function collectQueryResults<T>(lView: LView, queryIndex: number, result: T[]): T[] {
|
||||||
lView: LView, tQuery: TQuery, queryIndex: number, result: T[]): T[] {
|
const tQuery = lView[TVIEW].queries !.getByIndex(queryIndex);
|
||||||
ngDevMode &&
|
const tQueryMatches = tQuery.matches;
|
||||||
assertDefined(
|
if (tQueryMatches !== null) {
|
||||||
tQuery.matches, 'Query results can only be collected for queries with existing matches.');
|
const lViewResults = materializeViewResults<T>(lView, tQuery, queryIndex);
|
||||||
|
|
||||||
const tQueryMatches = tQuery.matches !;
|
for (let i = 0; i < tQueryMatches.length; i += 2) {
|
||||||
const lViewResults = materializeViewResults<T>(lView, tQuery, queryIndex);
|
const tNodeIdx = tQueryMatches[i];
|
||||||
|
if (tNodeIdx > 0) {
|
||||||
|
const viewResult = lViewResults[i / 2];
|
||||||
|
ngDevMode && assertDefined(viewResult, 'materialized query result should be defined');
|
||||||
|
result.push(viewResult as T);
|
||||||
|
} else {
|
||||||
|
const childQueryIndex = tQueryMatches[i + 1];
|
||||||
|
|
||||||
for (let i = 0; i < tQueryMatches.length; i += 2) {
|
const declarationLContainer = lView[-tNodeIdx] as LContainer;
|
||||||
const tNodeIdx = tQueryMatches[i];
|
ngDevMode && assertLContainer(declarationLContainer);
|
||||||
if (tNodeIdx > 0) {
|
|
||||||
const viewResult = lViewResults[i / 2];
|
|
||||||
ngDevMode && assertDefined(viewResult, 'materialized query result should be defined');
|
|
||||||
result.push(viewResult as T);
|
|
||||||
} else {
|
|
||||||
const childQueryIndex = tQueryMatches[i + 1];
|
|
||||||
|
|
||||||
const declarationLContainer = lView[-tNodeIdx] as LContainer;
|
// collect matches for views inserted in this container
|
||||||
ngDevMode && assertLContainer(declarationLContainer);
|
for (let i = CONTAINER_HEADER_OFFSET; i < declarationLContainer.length; i++) {
|
||||||
|
const embeddedLView = declarationLContainer[i];
|
||||||
// collect matches for views inserted in this container
|
if (embeddedLView[DECLARATION_LCONTAINER] === embeddedLView[PARENT]) {
|
||||||
for (let i = CONTAINER_HEADER_OFFSET; i < declarationLContainer.length; i++) {
|
collectQueryResults(embeddedLView, childQueryIndex, result);
|
||||||
const embeddedLView = declarationLContainer[i];
|
|
||||||
if (embeddedLView[DECLARATION_LCONTAINER] === embeddedLView[PARENT]) {
|
|
||||||
const tquery = getTQuery(embeddedLView[TVIEW], childQueryIndex);
|
|
||||||
if (tquery.matches !== null) {
|
|
||||||
collectQueryResults(embeddedLView, tquery, childQueryIndex, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// collect matches for views created from this declaration container and inserted into
|
// collect matches for views created from this declaration container and inserted into
|
||||||
// different containers
|
// different containers
|
||||||
if (declarationLContainer[MOVED_VIEWS] !== null) {
|
if (declarationLContainer[MOVED_VIEWS] !== null) {
|
||||||
for (let embeddedLView of declarationLContainer[MOVED_VIEWS] !) {
|
for (let embeddedLView of declarationLContainer[MOVED_VIEWS] !) {
|
||||||
const tquery = getTQuery(embeddedLView[TVIEW], childQueryIndex);
|
collectQueryResults(embeddedLView, childQueryIndex, result);
|
||||||
if (tquery.matches !== null) {
|
|
||||||
collectQueryResults(embeddedLView, tquery, childQueryIndex, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -413,7 +396,7 @@ export function ɵɵqueryRefresh(queryList: QueryList<any>): boolean {
|
||||||
if (tQuery.matches === null) {
|
if (tQuery.matches === null) {
|
||||||
queryList.reset([]);
|
queryList.reset([]);
|
||||||
} else {
|
} else {
|
||||||
const result = tQuery.crossesNgTemplate ? collectQueryResults(lView, tQuery, queryIndex, []) :
|
const result = tQuery.crossesNgTemplate ? collectQueryResults(lView, queryIndex, []) :
|
||||||
materializeViewResults(lView, tQuery, queryIndex);
|
materializeViewResults(lView, tQuery, queryIndex);
|
||||||
queryList.reset(result);
|
queryList.reset(result);
|
||||||
queryList.notifyOnChanges();
|
queryList.notifyOnChanges();
|
||||||
|
|
Loading…
Reference in New Issue