refactor(ivy): reduce code duplication and code size (#31489)

PR Close #31489
This commit is contained in:
Pawel Kozlowski 2019-07-19 11:30:06 +02:00 committed by Kara Erickson
parent d52ae7cbab
commit cb848b9410
1 changed files with 35 additions and 52 deletions

View File

@ -194,31 +194,22 @@ class TQuery_ implements TQuery {
private matchTNode(tView: TView, tNode: TNode): void {
if (Array.isArray(this.metadata.predicate)) {
this.matchTNodeByLocalNames(tView, tNode, this.metadata.predicate as string[]);
} else {
const typePredicate = this.metadata.predicate as any;
if (typePredicate === ViewEngine_TemplateRef) {
this.matchTNodeByTemplateRef(tView, tNode);
} else {
this.matchTNodeByType(tView, tNode, typePredicate);
}
}
}
private matchTNodeByLocalNames(tView: TView, tNode: TNode, localNames: string[]): void {
const localNames = this.metadata.predicate as string[];
for (let i = 0; i < localNames.length; i++) {
this.matchTNodeWithReadOption(tView, tNode, getIdxOfMatchingSelector(tNode, localNames[i]));
}
} else {
const typePredicate = this.metadata.predicate as any;
if (typePredicate === ViewEngine_TemplateRef) {
if (tNode.type === TNodeType.Container) {
this.matchTNodeWithReadOption(tView, tNode, -1);
}
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 {
} else {
this.matchTNodeWithReadOption(
tView, tNode, locateDirectiveOrProvider(tNode, tView, typePredicate, false, false));
}
}
}
private matchTNodeWithReadOption(tView: TView, tNode: TNode, nodeMatchIdx: number|null): void {
if (nodeMatchIdx !== null) {
@ -346,13 +337,10 @@ function materializeViewResults<T>(lView: LView, tQuery: TQuery, queryIndex: num
* A helper function that collects (already materialized) query results from a tree of views,
* starting with a provided LView.
*/
function collectQueryResults<T>(
lView: LView, tQuery: TQuery, queryIndex: number, result: T[]): T[] {
ngDevMode &&
assertDefined(
tQuery.matches, 'Query results can only be collected for queries with existing matches.');
const tQueryMatches = tQuery.matches !;
function collectQueryResults<T>(lView: LView, queryIndex: number, result: T[]): T[] {
const tQuery = lView[TVIEW].queries !.getByIndex(queryIndex);
const tQueryMatches = tQuery.matches;
if (tQueryMatches !== null) {
const lViewResults = materializeViewResults<T>(lView, tQuery, queryIndex);
for (let i = 0; i < tQueryMatches.length; i += 2) {
@ -371,10 +359,7 @@ function collectQueryResults<T>(
for (let i = CONTAINER_HEADER_OFFSET; i < declarationLContainer.length; i++) {
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);
}
collectQueryResults(embeddedLView, childQueryIndex, result);
}
}
@ -382,9 +367,7 @@ function collectQueryResults<T>(
// different containers
if (declarationLContainer[MOVED_VIEWS] !== null) {
for (let embeddedLView of declarationLContainer[MOVED_VIEWS] !) {
const tquery = getTQuery(embeddedLView[TVIEW], childQueryIndex);
if (tquery.matches !== null) {
collectQueryResults(embeddedLView, tquery, childQueryIndex, result);
collectQueryResults(embeddedLView, childQueryIndex, result);
}
}
}
@ -413,7 +396,7 @@ export function ɵɵqueryRefresh(queryList: QueryList<any>): boolean {
if (tQuery.matches === null) {
queryList.reset([]);
} else {
const result = tQuery.crossesNgTemplate ? collectQueryResults(lView, tQuery, queryIndex, []) :
const result = tQuery.crossesNgTemplate ? collectQueryResults(lView, queryIndex, []) :
materializeViewResults(lView, tQuery, queryIndex);
queryList.reset(result);
queryList.notifyOnChanges();