mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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.
This commit is contained in:
committed by
Jeremy Elbourn
parent
10e26a5311
commit
0db1795f62
@@ -122,10 +122,12 @@ export class SelectionModel<T> {
|
||||
this._selected = null;
|
||||
|
||||
if (this._selectedToEmit.length || this._deselectedToEmit.length) {
|
||||
const eventData = new SelectionChange<T>(this, this._selectedToEmit, this._deselectedToEmit);
|
||||
|
||||
if (this.onChange) {
|
||||
this.onChange.next(eventData);
|
||||
this.onChange.next({
|
||||
source: this,
|
||||
added: this._selectedToEmit,
|
||||
removed: this._deselectedToEmit
|
||||
});
|
||||
}
|
||||
|
||||
this._deselectedToEmit = [];
|
||||
@@ -181,14 +183,13 @@ export class SelectionModel<T> {
|
||||
* Event emitted when the value of a MatSelectionModel has changed.
|
||||
* @docs-private
|
||||
*/
|
||||
export class SelectionChange<T> {
|
||||
constructor(
|
||||
/** Model that dispatched the event. */
|
||||
public source: SelectionModel<T>,
|
||||
/** Options that were added to the model. */
|
||||
public added?: T[],
|
||||
/** Options that were removed from the model. */
|
||||
public removed?: T[]) {}
|
||||
export interface SelectionChange<T> {
|
||||
/** Model that dispatched the event. */
|
||||
source: SelectionModel<T>;
|
||||
/** Options that were added to the model. */
|
||||
added: T[];
|
||||
/** Options that were removed from the model. */
|
||||
removed: T[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user