У меня есть небольшая (или большая) проблема с объединением наблюдаемых. Я реализую какие-то теги-входные данные.RxJS Объединение наблюдаемых
this._allTags
все имеющиеся метки.
У меня есть 4 потока:
this._suggestions = new this.rx.Subject;
this._searchText = new this.rx.Subject;
this._selectedIndex = new this.rx.Subject;
this._eventsStream = new this.rx.Subject;
Метод поиска:
search(searchText) {
this._searchText.onNext(searchText);
this._selectedIndex.onNext(-1);
}
метод KeyDown:
keyDown(event) {
this._eventsStream.onNext(event);
}
поиска логика:
const partitionSearchText = this._searchText
.partition((searchText) => !!searchText); //check if searchText is not empty
//put filtered array to this._suggestions stream
partitionSearchText[0]
.subscribe((searchText) => this._suggestions.onNext(
this._allTags.filter((item) => ~item.name.toLowerCase().indexOf(searchText.toLowerCase()))
));
//put empty array to this._suggestions stream if there is no searchText
partitionSearchText[1]
.subscribe((searchText) => this._suggestions.onNext([]));
И я хочу реализовать события. Если есть searchText и keyDown
событие, то я хочу увеличить this._selectedIndex
, но если this._selectedIndex
будет такой же, как this._suggestions
, то не увеличивайте его.
Это до сих пор, что я реализовал:
const eventsWithSearchText = this._searchText
.map((searchText) => !!searchText ? this._eventsStream : this.rx.Observable.empty())
.switch()
const keyDownEvents = eventsWithSearchText
.filter((event) => event.keyCode === DOWN_KEY)
keyDownEvents
.subscribe((event) => event.preventDefault())
const isNotLast = this._selectedIndex
.combineLatest(this._suggestions, (index, sugg) => index !== sugg.length - 1);
keyDownEvents
.subscribe((item) => {
this._selectedIndexValue++
this._selectedIndex.onNext(this._selectedIndexValue);
});
Итак, это увеличивающиеся this._selectedIndex
, но не останавливаясь, когда он такой же, как this._suggestions
длины.
Вы можете помочь?
https://plnkr.co/edit/eh21d0d8U0VIsUyCjlkJ?p=preview
Пожалуйста, приготовьте скрипку. – halfzebra
вот он: https://plnkr.co/edit/eh21d0d8U0VIsUyCjlkJ?p=preview – dakolech