Skip to content

Commit fe759ee

Browse files
benleshmatsko
authored andcommitted
refactor(ivy): Update query-related comments (angular#29342)
Just updating comments in query-related things to make it easier for the next person that has to grok this for the first time. Also adds a demo from @mhevery to one of the query specs Related angular#29031 PR Close angular#29342
1 parent 105cfaf commit fe759ee

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

packages/core/src/linker/query_list.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,33 @@ export class QueryList<T>/* implements Iterable<T> */ {
9393
return this._results.some(fn);
9494
}
9595

96+
/**
97+
* Returns a copy of the internal results list as an Array.
98+
*/
9699
toArray(): T[] { return this._results.slice(); }
97100

98101
[getSymbolIterator()](): Iterator<T> { return (this._results as any)[getSymbolIterator()](); }
99102

100103
toString(): string { return this._results.toString(); }
101104

102-
reset(res: Array<T|any[]>): void {
103-
this._results = flatten(res);
105+
/**
106+
* Updates the stored data of the query list, and resets the `dirty` flag to `false`, so that
107+
* on change detection, it will not notify of changes to the queries, unless a new change
108+
* occurs.
109+
*
110+
* @param resultsTree The results tree to store
111+
*/
112+
reset(resultsTree: Array<T|any[]>): void {
113+
this._results = depthFirstFlatten(resultsTree);
104114
(this as{dirty: boolean}).dirty = false;
105115
(this as{length: number}).length = this._results.length;
106116
(this as{last: T}).last = this._results[this.length - 1];
107117
(this as{first: T}).first = this._results[0];
108118
}
109119

120+
/**
121+
* Triggers a change event by emitting on the `changes` {@link EventEmitter}.
122+
*/
110123
notifyOnChanges(): void { (this.changes as EventEmitter<any>).emit(this); }
111124

112125
/** internal */
@@ -119,9 +132,9 @@ export class QueryList<T>/* implements Iterable<T> */ {
119132
}
120133
}
121134

122-
function flatten<T>(list: Array<T|T[]>): T[] {
135+
function depthFirstFlatten<T>(list: Array<T|T[]>): T[] {
123136
return list.reduce((flat: any[], item: T | T[]): T[] => {
124-
const flatItem = Array.isArray(item) ? flatten(item) : item;
137+
const flatItem = Array.isArray(item) ? depthFirstFlatten(item) : item;
125138
return (<T[]>flat).concat(flatItem);
126139
}, []);
127140
}

packages/core/src/render3/query.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ export function query<T>(
365365
/**
366366
* Refreshes a query by combining matches from all active views and removing matches from deleted
367367
* views.
368-
* Returns true if a query got dirty during change detection, false otherwise.
368+
*
369+
* @returns `true` if a query got dirty during change detection or if this is a static query
370+
* resolving in creation mode, `false` otherwise.
369371
*/
370372
export function queryRefresh(queryList: QueryList<any>): boolean {
371373
const queryListImpl = (queryList as any as QueryList_<any>);

packages/core/test/render3/query_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ describe('query', () => {
16031603
});
16041604

16051605
// https://stackblitz.com/edit/angular-7vvo9j?file=src%2Fapp%2Fapp.component.ts
1606+
// https://stackblitz.com/edit/angular-xzwp6n
16061607
it('should report results when the same TemplateRef is inserted into different ViewContainerRefs',
16071608
() => {
16081609
let tpl: TemplateRef<{}>;

tools/public_api_guard/core/core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ export declare class QueryList<T> {
721721
map<U>(fn: (item: T, index: number, array: T[]) => U): U[];
722722
notifyOnChanges(): void;
723723
reduce<U>(fn: (prevValue: U, curValue: T, curIndex: number, array: T[]) => U, init: U): U;
724-
reset(res: Array<T | any[]>): void;
724+
reset(resultsTree: Array<T | any[]>): void;
725725
setDirty(): void;
726726
some(fn: (value: T, index: number, array: T[]) => boolean): boolean;
727727
toArray(): T[];

0 commit comments

Comments
 (0)