Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(core): avoid changes Observable creation on QueryList #53498

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/core/src/linker/query_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class QueryList<T> implements Iterable<T> {
public readonly dirty = true;
private _results: Array<T> = [];
private _changesDetected: boolean = false;
private _changes: EventEmitter<QueryList<T>>|null = null;
private _changes: EventEmitter<QueryList<T>>|undefined = undefined;

readonly length: number = 0;
readonly first: T = undefined!;
Expand All @@ -57,7 +57,7 @@ export class QueryList<T> implements Iterable<T> {
* Returns `Observable` of `QueryList` notifying the subscriber of changes.
*/
get changes(): Observable<any> {
return this._changes || (this._changes = new EventEmitter());
return this._changes ??= new EventEmitter();
}

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ export class QueryList<T> implements Iterable<T> {
* Triggers a change event by emitting on the `changes` {@link EventEmitter}.
*/
notifyOnChanges(): void {
if (this._changes && (this._changesDetected || !this._emitDistinctChangesOnly))
if (this._changes !== undefined && (this._changesDetected || !this._emitDistinctChangesOnly))
this._changes.emit(this);
}

Expand All @@ -180,8 +180,10 @@ export class QueryList<T> implements Iterable<T> {

/** internal */
destroy(): void {
(this.changes as EventEmitter<any>).complete();
(this.changes as EventEmitter<any>).unsubscribe();
if (this._changes !== undefined) {
this._changes.complete();
this._changes.unsubscribe();
}
}

// The implementation of `Symbol.iterator` should be declared here, but this would cause
Expand Down