为了账号安全,请及时绑定邮箱和手机立即绑定

根据流中的值暂停 RxJS 流

根据流中的值暂停 RxJS 流

慕森卡 2021-07-09 14:15:37
我有一个简单的组件,带有一个按钮,可以启动和暂停由 RxJS 计时器生成的数字流。import { Component, OnInit } from '@angular/core';import { BehaviorSubject, Observable, timer, merge } from 'rxjs';import { filter, bufferToggle, windowToggle, mergeMap, mergeAll, share } from 'rxjs/operators';@Component({  selector: 'my-app',  template: `<button (click)="toggle()">{{ (active$ | async) ? 'Pause' : 'Play' }}</button>`,  styleUrls: [ './app.component.css' ]})export class AppComponent implements OnInit {  active$ = new BehaviorSubject<boolean>(true);  ngOnInit(): void {    const on$ = this.active$.pipe(filter(v => v));    const off$ = this.active$.pipe(filter(v => !v));    const stream$ = timer(500, 500).pipe(share());    const out$ = merge(      stream$.pipe(        bufferToggle(off$, () => on$),        mergeAll(),      ),      stream$.pipe(        windowToggle(on$, () => off$),        mergeAll(),      ),    );    out$.subscribe(v => console.log(v));  }  toggle(): void {    this.active$.next(!this.active$.value);  }这非常有效,但我需要再添加一项功能!我需要根据满足条件的流中的值自动暂停流。例如,如果最新值是 5 的倍数,则暂停流。你有什么想法如何做到这一点吗?
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 225 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信