简体   繁体   English

RxJS结合可观察物

[英]RxJS Combining observables

I have a little (or big) problem with combining observables. 我在结合可观察变量时有一个小(或大)问题。 I'm implementing some kind of tags-input. 我正在实现某种标签输入。

this._allTags are all available tags. this._allTags是所有可用的标签。

I have 4 streams: 我有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 method: 搜索方式:

search(searchText) {
  this._searchText.onNext(searchText);
  this._selectedIndex.onNext(-1);
}

KeyDown method: KeyDown方法:

keyDown(event) {
  this._eventsStream.onNext(event);
}

searching logic: 搜索逻辑:

  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([]));

And I want to implement events. 我想实施事件。 If there is a searchText and keyDown event, then I want to increment this._selectedIndex , but if this._selectedIndex will be same as this._suggestions length, then don't increment it. 如果有searchText和keyDown事件,那么我要增加this._selectedIndex ,但是如果this._selectedIndexthis._suggestions长度相同,则不要增加它。

This is so far what I implemented: 到目前为止,这是我实现的:

  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);
    });

So, it's incrementing this._selectedIndex but not stopping when it's same as this._suggestions length. 因此,它会增加this._selectedIndex但与this._suggestions长度this._selectedIndex同时不会停止。

Can you help? 你能帮我吗?

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

I made it! 我做到了! Here is the code: 这是代码:

  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 https://plnkr.co/edit/soaChC?p=preview

Hope it'll help somebody. 希望对别人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM