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 { 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[];
} 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 {
for (let i = 0; i < localNames.length; i++) { for (let i = 0; i < localNames.length; i++) {
this.matchTNodeWithReadOption(tView, tNode, getIdxOfMatchingSelector(tNode, localNames[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);
} }
} else {
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( this.matchTNodeWithReadOption(
tView, tNode, locateDirectiveOrProvider(tNode, tView, typePredicate, false, false)); 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) {
@ -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, * 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 tQueryMatches = tQuery.matches !;
const lViewResults = materializeViewResults<T>(lView, tQuery, queryIndex); const lViewResults = materializeViewResults<T>(lView, tQuery, queryIndex);
for (let i = 0; i < tQueryMatches.length; i += 2) { 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++) { for (let i = CONTAINER_HEADER_OFFSET; i < declarationLContainer.length; i++) {
const embeddedLView = declarationLContainer[i]; const embeddedLView = declarationLContainer[i];
if (embeddedLView[DECLARATION_LCONTAINER] === embeddedLView[PARENT]) { if (embeddedLView[DECLARATION_LCONTAINER] === embeddedLView[PARENT]) {
const tquery = getTQuery(embeddedLView[TVIEW], childQueryIndex); collectQueryResults(embeddedLView, childQueryIndex, result);
if (tquery.matches !== null) {
collectQueryResults(embeddedLView, tquery, childQueryIndex, result);
}
} }
} }
@ -382,9 +367,7 @@ function collectQueryResults<T>(
// 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();