gpt4 book ai didi

javascript - RxJS 组合可观察对象

转载 作者:行者123 更新时间:2023-11-29 19:15:22 24 4
gpt4 key购买 nike

我在合并 observables 时遇到了一个小(或大)问题。我正在实现某种标签输入。

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

最佳答案

我成功了!这是代码:

  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 keyUpEvents = eventsWithSearchText
.filter((event) => event.keyCode === UP_KEY)

keyUpEvents
.subscribe((event) => event.preventDefault())

const enterEvents = eventsWithSearchText
.filter((event) => event.keyCode === ENTER_KEY)

enterEvents
.subscribe((event) => event.preventDefault())

const isNotLast = this._selectedIndex
.combineLatest(this._suggestions, (index, sugg) => index !== sugg.length - 1);

const keyDownAndNotLast = keyDownEvents
.map(() => +1)
.withLatestFrom(isNotLast, (value, notLast) => notLast ? value : false)
.filter((item) => item)

const keyUpEventsAndNotFirst = keyUpEvents
.map(() => -1)
.withLatestFrom(this._selectedIndex, (value, index) => !!index ? value : false)
.filter((item) => item)

this.rx.Observable.merge(
keyDownAndNotLast,
keyUpEventsAndNotFirst,
enterEvents
.map(() => ({reset: true}))
)
.scan((acc, value) => value.reset ? -1 : acc + value, -1)
.subscribe((item) => {
this._selectedIndex.onNext(item);
});

https://plnkr.co/edit/soaChC?p=preview

希望对大家有所帮助。

关于javascript - RxJS 组合可观察对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35801139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com