第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何停止去抖動的 Rxjs Observable?

如何停止去抖動的 Rxjs Observable?

慕桂英3389331 2022-01-07 10:35:29
我創(chuàng)建了一個 observable,它將在最后一次更改后 3 秒觸發(fā),并調(diào)用publishChange服務(wù)。它可以工作,但我想創(chuàng)建一個doImmediateChange函數(shù),它publishChange立即調(diào)用并停止去抖動的 observable。這怎么可能?我的組件:class MyComponent {    private updateSubject = new Subject<string>();    ngOnInit() {        this.updateSubject.pipe(            debounceTime(3000),            distinctUntilChanged()        ).subscribe(val => {            this.srv.publishChange(val);        });    }    doChange(val: string) {        this.updateSubject.next(val);    }    doImmediateChange(val: string) {        // Stop the current updateSubject if debounce is in progress and call publish immediately        // ??        this.srv.publishChange(val);    }}
查看完整描述

3 回答

?
墨色風(fēng)雨

TA貢獻(xiàn)1853條經(jīng)驗 獲得超6個贊

您可以debounceTime使用switchMap和進(jìn)行模擬delay。然后取消內(nèi)部 ObservabletakeUntil以防止發(fā)出等待值。


private updateSubject = new Subject<string>();

private interrupt = new Subject();


ngOnInit() {

  this.updateSubject.pipe(

    switchMap(val => of(val).pipe(

      delay(3000),

      takeUntil(this.interrupt)

    ))

  ).subscribe(val => publish(val));

}


doChange(val: string) {

  this.updateSubject.next(val);

}


doImmediateChange(val: string) {

  this.interrupt.next();

  publish(val);

}

https://stackblitz.com/edit/rxjs-ya93fb


查看完整回答
反對 回復(fù) 2022-01-07
?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊

使用競賽運算符:


第一個完成的observable成為唯一訂閱的observable,因此這個遞歸函數(shù)將在一次發(fā)射后完成take(1),然后重新訂閱() => this.raceRecursive()。


private timed$ = new Subject<string>();

private event$ = new Subject<string>();


ngOnInit() {

  this.raceRecursive()

}


raceRecursive() {

  race(

    this.timed$.pipe(debounceTime(1000)),

    this.event$

  )

    .pipe(take(1)) // force it to complete

    .subscribe(

      val => console.log(val), // srv call here

      err => console.error(err),

      () => this.raceRecursive() // reset it once complete

    )

}


doChange(val: string) {

  this.timed$.next(val)

}


doImmediateChange(val: string) {

  this.event$.next(val)

}



查看完整回答
反對 回復(fù) 2022-01-07
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗 獲得超21個贊

您可以使用debounce和race實現(xiàn)此行為:

使用您提供的代碼


private destroy$ = new Subject<void>();

private immediate$ = new Subject<void>();

private updateSubject$ = new Subject<string>();


constructor(private srv: PubSubService) {}


ngOnInit() {

  this.updateSubject$.pipe(

      takeUntil(this.destroy$),

      debounce(() => race(timer(3000), this.immediate$))

  ).subscribe(val => {

      this.srv.publishChange(val);

  });

}


doChange(val: string, immediate?: boolean) {

  this.updateSubject$.next(val);

  if (immediate) this.immediate$.next();

}


// don't forget to unsubscribe

ngOnDestroy() {

  this.destroy$.next();

}

立即發(fā)出更改將替換之前的正常更改(即去抖 3 秒),而不會延遲(感謝我們的種族可觀察)。

查看完整回答
反對 回復(fù) 2022-01-07
  • 3 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號