RxJS - make counter with reset stateless?

前端 未结 4 1773
独厮守ぢ
独厮守ぢ 2020-12-30 09:51

Assuming I have the following markup:


0
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 10:15

    @Jrop I like this operator Rx.Observable.when. With this one you can reproduce Bacon.update very easy. This is my code and jsbin example:

    const {when, fromEvent} = Rx.Observable;
    
    const decObs = fromEvent(document.getElementById('dec'), 'click');
    const incObs = fromEvent(document.getElementById('inc'), 'click');
    const resetObs = fromEvent(document.getElementById('res'), 'click');
    
    when(
        decObs.thenDo(_ => prev => prev - 1),
        incObs.thenDo(_ => prev => prev + 1),
        resetObs.thenDo(_ => prev => 0)
    ).startWith(0).scan((prev, f) => f(prev))
    .subscribe(v => document.getElementById('out').innerHTML = v);
    

    Also will be better if you look at this Join-calculus, New Release and Joins and this Combining sequences

提交回复
热议问题