From 0db1795f62693beb2106dc30ba5aed3d86da8b4a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 13 Feb 2018 16:40:49 +0100 Subject: [PATCH] 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. --- src/cdk/collections/selection.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/cdk/collections/selection.ts b/src/cdk/collections/selection.ts index d201859ff62..49e8cae22df 100644 --- a/src/cdk/collections/selection.ts +++ b/src/cdk/collections/selection.ts @@ -122,10 +122,12 @@ export class SelectionModel { this._selected = null; if (this._selectedToEmit.length || this._deselectedToEmit.length) { - const eventData = new SelectionChange(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 { * Event emitted when the value of a MatSelectionModel has changed. * @docs-private */ -export class SelectionChange { - constructor( - /** Model that dispatched the event. */ - public source: SelectionModel, - /** Options that were added to the model. */ - public added?: T[], - /** Options that were removed from the model. */ - public removed?: T[]) {} +export interface SelectionChange { + /** Model that dispatched the event. */ + source: SelectionModel; + /** Options that were added to the model. */ + added: T[]; + /** Options that were removed from the model. */ + removed: T[]; } /**