Skip to content

Commit

Permalink
fix: improve the speed of the subjOptionsSelected calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ert78gb committed Sep 30, 2018
1 parent ad2f9a1 commit dd574d7
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/app/lib/ngx-select/ngx-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,24 @@ export class NgxSelectComponent implements INgxSelectOptions, ControlValueAccess
.toArray()
)
.combineLatest(subjActualValue, (optionsFlat: NgxSelectOption[], actualValue: any[]) => {
Observable.from(optionsFlat)
.filter((option: NgxSelectOption) => actualValue.indexOf(option.value) !== -1)
.toArray()
.filter((options: NgxSelectOption[]) => {
if (this.keepSelectedItems) {
const optionValues = options.map((option: NgxSelectOption) => option.value);
const keptSelectedOptions = this.subjOptionsSelected.value
.filter((selOption: NgxSelectOption) => optionValues.indexOf(selOption.value) === -1);
options = keptSelectedOptions.concat(options);
}
return !_.isEqual(options, this.subjOptionsSelected.value);
})
.subscribe((options: NgxSelectOption[]) => {
this.subjOptionsSelected.next(options);
this.cd.markForCheck();
});
const optionsSelected = [];
for (const option of optionsFlat) {
if (actualValue.indexOf(option.value) > -1) {
optionsSelected.push(option);
}
}

if (this.keepSelectedItems) {
const optionValues = optionsSelected.map((option: NgxSelectOption) => option.value);
const keptSelectedOptions = this.subjOptionsSelected.value
.filter((selOption: NgxSelectOption) => optionValues.indexOf(selOption.value) === -1);
optionsSelected.push(...keptSelectedOptions);
}

if (!_.isEqual(optionsSelected, this.subjOptionsSelected.value)) {
this.subjOptionsSelected.next(optionsSelected);
this.cd.markForCheck();
}
})
.subscribe();

Expand Down

0 comments on commit dd574d7

Please sign in to comment.