Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 使用filter()运算符筛选值时触发副作用_Rxjs_Rxjs6_Rxjs Pipeable Operators - Fatal编程技术网

Rxjs 使用filter()运算符筛选值时触发副作用

Rxjs 使用filter()运算符筛选值时触发副作用,rxjs,rxjs6,rxjs-pipeable-operators,Rxjs,Rxjs6,Rxjs Pipeable Operators,我正在处理一个用户名搜索输入,该输入在debounceTime()之后触发。我还过滤掉了值。如果值被过滤,我想触发一些副作用(例如设置错误消息和停止加载程序) 我现在通过使用tap()并检查稍后在filter()函数中检查的相同谓词来实现这一点。我觉得这很糟糕/有一个更合适的方法来实现这一点 private setUserSearchObservable() { this.userSearch = userSearch$.pipe( tap(() => this.loa

我正在处理一个用户名搜索输入,该输入在debounceTime()之后触发。我还过滤掉了值。如果值被过滤,我想触发一些副作用(例如设置错误消息和停止加载程序)

我现在通过使用tap()并检查稍后在filter()函数中检查的相同谓词来实现这一点。我觉得这很糟糕/有一个更合适的方法来实现这一点

private setUserSearchObservable() {
    this.userSearch = userSearch$.pipe(
      tap(() => this.loading = true),
      debounceTime(500),
      this.filterValuesWithErrorMessages(),
      ......
    );
}

private filterValuesWithErrorMessages() {
    return pipe(
      tap((val: string) => { if (this.usernamesMatch(val)) { this.errorMessage = 'You will be added to the frame automatically'; this.loading = false; }}),
      tap((val: string) => { if (this.usernameInArray(val)) { this.errorMessage = 'User is already added'; this.loading = false; }}),
      tap((val: string) => { if (val.length < 2) { this.errorMessage = ''; this.loading = false; }}),
      filter((val: string) => (val.length >= 2 && !this.usernamesMatch(val) && !this.usernameInArray(val))),
    );
}
private setUserSearchObservable(){
this.userSearch=userSearch$.pipe(
点击(()=>this.loading=true),
去BounceTime(500),
this.filterValuesWithErrorMessages(),
......
);
}
private FilterValue SwitherRorMessages(){
回流管(
点击((val:string)=>{if(this.usernamesMatch(val)){this.errorMessage='您将自动添加到帧';this.loading=false;}}}),
点击((val:string)=>{if(this.usernameInArray(val)){this.errorMessage='用户已添加';this.loading=false;}}),
点击((val:string)=>{if(val.length<2){this.errorMessage='';this.loading=false;}}),
过滤器((val:string)=>(val.length>=2&&!this.usernamesMatch(val)&&!this.usernameInArray(val)),
);
}

如您所见,在filter()上使用条件之前,我使用tap()显式检查了完全相同的条件。是否有一个操作符/不同的模式可以让我以更简洁的方式实现这一点?

对代码进行了一点重构,没有特定的操作符来处理错误检查,您可以使用
开关映射
和内部可观察的.g的
从不
来控制结果是否应该通过

private setUserSearchObservable() {
    this.userSearch = userSearch$.pipe(
      tap(() => this.loading = true),
      debounceTime(500),
      map((value) => this.filterValuesWithErrorMessages(value)),
      swtichMap(msg => {
        if (msg !== false) {
          this.errorMessage = result
          this.loading = false
          return never()
        }
        return of(true)
      })
    );
  }

  private filterValuesWithErrorMessages(val) {
    if (this.usernamesMatch(val)) return 'You will be added to the frame automatically'
    if (this.usernameInArray(val)) return 'User is already added'
    if (val.length < 2) return ''
    return false
  }
private setUserSearchObservable(){
this.userSearch=userSearch$.pipe(
点击(()=>this.loading=true),
去BounceTime(500),
映射((值)=>this.filterValuesWithErrorMessages(值)),
swtichMap(msg=>{
如果(消息!==false){
this.errorMessage=结果
此参数为0。加载=错误
永不返回()
}
返回(真)
})
);
}
专用筛选器值SwitherRorMessages(val){
如果(this.usernamesMatch(val))返回“您将自动添加到帧”
如果(this.usernameInArray(val))返回“用户已添加”
如果(值长度<2)返回“”
返回错误
}

对代码进行了一点重构,没有特定的操作符来处理错误检查,您可以使用
开关映射
和内部可观察项,例如
从不
来控制结果是否应该通过

private setUserSearchObservable() {
    this.userSearch = userSearch$.pipe(
      tap(() => this.loading = true),
      debounceTime(500),
      map((value) => this.filterValuesWithErrorMessages(value)),
      swtichMap(msg => {
        if (msg !== false) {
          this.errorMessage = result
          this.loading = false
          return never()
        }
        return of(true)
      })
    );
  }

  private filterValuesWithErrorMessages(val) {
    if (this.usernamesMatch(val)) return 'You will be added to the frame automatically'
    if (this.usernameInArray(val)) return 'User is already added'
    if (val.length < 2) return ''
    return false
  }
private setUserSearchObservable(){
this.userSearch=userSearch$.pipe(
点击(()=>this.loading=true),
去BounceTime(500),
映射((值)=>this.filterValuesWithErrorMessages(值)),
swtichMap(msg=>{
如果(消息!==false){
this.errorMessage=结果
此参数为0。加载=错误
永不返回()
}
返回(真)
})
);
}
专用筛选器值SwitherRorMessages(val){
如果(this.usernamesMatch(val))返回“您将自动添加到帧”
如果(this.usernameInArray(val))返回“用户已添加”
如果(值长度<2)返回“”
返回错误
}

我喜欢这个主意,不知道never();RxJS最难的部分是以新的方式思考。谢谢我喜欢这个主意,不知道怎么做;RxJS最难的部分是以新的方式思考。谢谢