Skip to content

Commit abf2c07

Browse files
crisbetoandrewseguin
authored andcommitted
refactor(selection-model): avoid unnecessary null checks in change event (#9850)
* Makes the `added` and `removed` properties on the `SelectionChange` event required since they're guaranteed to be an empty array. This makes it more convenient to consume, because it avoids having to null check them every time. * Turns the `SelectionChange` into an interface to reduce the amount of code being generated.
1 parent 5a1e7fe commit abf2c07

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/cdk/collections/selection.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ export class SelectionModel<T> {
122122
this._selected = null;
123123

124124
if (this._selectedToEmit.length || this._deselectedToEmit.length) {
125-
const eventData = new SelectionChange<T>(this, this._selectedToEmit, this._deselectedToEmit);
126-
127125
if (this.onChange) {
128-
this.onChange.next(eventData);
126+
this.onChange.next({
127+
source: this,
128+
added: this._selectedToEmit,
129+
removed: this._deselectedToEmit
130+
});
129131
}
130132

131133
this._deselectedToEmit = [];
@@ -181,14 +183,13 @@ export class SelectionModel<T> {
181183
* Event emitted when the value of a MatSelectionModel has changed.
182184
* @docs-private
183185
*/
184-
export class SelectionChange<T> {
185-
constructor(
186-
/** Model that dispatched the event. */
187-
public source: SelectionModel<T>,
188-
/** Options that were added to the model. */
189-
public added?: T[],
190-
/** Options that were removed from the model. */
191-
public removed?: T[]) {}
186+
export interface SelectionChange<T> {
187+
/** Model that dispatched the event. */
188+
source: SelectionModel<T>;
189+
/** Options that were added to the model. */
190+
added: T[];
191+
/** Options that were removed from the model. */
192+
removed: T[];
192193
}
193194

194195
/**

0 commit comments

Comments
 (0)