Skip to content

Commit 68d111c

Browse files
pkozlowski-opensourcealxhub
authored andcommitted
perf(core): avoid changes Observable creation on QueryList (#53498)
The changes Observable (impl: EventEmitter) on the QueryList is initalized lazy - it is created only if someone calls a geter to get a hand on its instance. But the destroy method was calling this getter thus creating a new Observable even if no one subscribed to it. This commit changes the destroy logic to skip creation of an EventEmitter if it wasn't initialized. PR Close #53498
1 parent 17610fa commit 68d111c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

packages/core/src/linker/query_list.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class QueryList<T> implements Iterable<T> {
4747
public readonly dirty = true;
4848
private _results: Array<T> = [];
4949
private _changesDetected: boolean = false;
50-
private _changes: EventEmitter<QueryList<T>>|null = null;
50+
private _changes: EventEmitter<QueryList<T>>|undefined = undefined;
5151

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

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

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

181181
/** internal */
182182
destroy(): void {
183-
(this.changes as EventEmitter<any>).complete();
184-
(this.changes as EventEmitter<any>).unsubscribe();
183+
if (this._changes !== undefined) {
184+
this._changes.complete();
185+
this._changes.unsubscribe();
186+
}
185187
}
186188

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

0 commit comments

Comments
 (0)