@@ -34,10 +34,12 @@ import {
3434 Subscription ,
3535} from 'rxjs' ;
3636import {
37+ distinctUntilChanged ,
3738 filter ,
3839 map ,
3940 mergeMap ,
4041 scan ,
42+ startWith ,
4143 take ,
4244} from 'rxjs/operators' ;
4345
@@ -118,8 +120,10 @@ export class DsDynamicRelationGroupComponent extends DynamicFormControlComponent
118120 }
119121
120122 ngOnInit ( ) {
121- this . initChipsFromModelValue ( ) ;
122- this . valueChangeSubscription = this . model . valueChanges . subscribe ( ( ) => {
123+ this . valueChangeSubscription = this . model . valueChanges . pipe (
124+ startWith ( [ ] ) ,
125+ distinctUntilChanged ( ( a , b ) => JSON . stringify ( a ) === JSON . stringify ( b ) ) ,
126+ ) . subscribe ( ( ) => {
123127 this . initChipsFromModelValue ( ) ;
124128 } ) ;
125129 }
@@ -174,23 +178,28 @@ export class DsDynamicRelationGroupComponent extends DynamicFormControlComponent
174178 }
175179 modalRef . result . then ( ( ) => {
176180 // close
177- this . selectedChipItemIndex = null ;
178- this . selectedChipItem = null ;
179- this . valueChangeSubscription = this . model . valueChanges . subscribe ( ( ) => {
180- this . initChipsFromModelValue ( ) ;
181- } ) ;
181+ this . handleModalResult ( ) ;
182182 } , ( ) => {
183183 // dismiss
184- this . selectedChipItemIndex = null ;
185- this . selectedChipItem = null ;
186- this . valueChangeSubscription = this . model . valueChanges . subscribe ( ( ) => {
187- this . initChipsFromModelValue ( ) ;
188- } ) ;
184+ this . handleModalResult ( ) ;
189185 } ) ;
190186
191187 return modalRef ;
192188 }
193189
190+ private handleModalResult ( ) : void {
191+ this . selectedChipItemIndex = null ;
192+ this . selectedChipItem = null ;
193+ if ( this . valueChangeSubscription ) {
194+ this . valueChangeSubscription . unsubscribe ( ) ;
195+ }
196+ this . valueChangeSubscription = this . model . valueChanges . pipe (
197+ distinctUntilChanged ( ( a , b ) => JSON . stringify ( a ) === JSON . stringify ( b ) ) ,
198+ ) . subscribe ( ( ) => {
199+ this . initChipsFromModelValue ( ) ;
200+ } ) ;
201+ }
202+
194203 private initChipsFromModelValue ( ) {
195204 let initChipsValue$ : Observable < any [ ] > ;
196205 if ( this . model . isEmpty ( ) ) {
@@ -236,7 +245,7 @@ export class DsDynamicRelationGroupComponent extends DynamicFormControlComponent
236245 return acc ;
237246 } , [ ] ) ,
238247 filter ( ( modelValues : any [ ] ) => this . model . getGroupValue ( ) . length === modelValues . length ) ,
239- ) . subscribe ( ( modelValue ) => {
248+ ) . pipe ( distinctUntilChanged ( ( a , b ) => JSON . stringify ( a ) === JSON . stringify ( b ) ) ) . subscribe ( ( modelValue ) => {
240249 this . model . value = modelValue ;
241250 this . initChips ( modelValue ) ;
242251 this . cdr . markForCheck ( ) ;
0 commit comments