Reactive Programming with Rxjava-Chapter6:Flow Control and Backpressure(1)

RxJava has two way of dealing with producers being more active than subscribers:

  • Various flow-control mechanisms such as sampling and batching are implemented via built-in operators
  • Subscribers can propagate theri demand and request only as many tiems as they can process by using a feedback channel known as backpressure.

Flow Control

Taking Periodic Samples and Throttling

sample() has a more advanced variant taking Observable as an argument rather than a fixed period.This second Observable (known as sampler) basically dictates when to take a sample from the upstream source:every time sampler emits any value,a new sample is taken (if any new value appeared since the last sample)

sample(Observable)

Buffering Events to a List

buffer(count)
buffer(count)

buffer(count, skip)
buffer(count, skip)

buffer(boundary)
buffer(boundary)

buffer(bufferOpenings, bufferClosingSelector)
buffer(bufferOpenings, bufferClosingSelector)

For example

Observable<Duration> insideBusinessHours = Observable
    .interval(1,SECONDS)
    .filter(x -> isBusinessHour())
    .map(x -> Duration.ofMillis(100));

Observable<Duration> outsideBusinessHours = Observable
    .interval(5,SECONDS)
    .filter(x -> !isBusinessHour())
    .map(x -> Duration.ofMillis(200));

Observable<Duration> openings = Observable.merge(
    insideBusinessHours,outsideBusinessHours);

Observable<List<TeleData>> samples = upstream
    .buffer(
        openings,
        duration -> empty()
            .delay(duration.toMillis(),MILLISECONDS));

Moving window

Because buffer() requires creating an intermediate List before the current buffer is closed and passed downstream,it might unnecessarily put pressure on garbage

Observable<KeyEvent> keyEvents = //...

//buffer
Observable<Integer> eventPerSecond = keyEvents
    .buffer(1,SECONDS)
    .map(List::size);

vs

//window
Obsevable<Observable<KeyEvent>> windows = keyEvents.window(1,SECONDS);
Observable<Integer> eventPerSecond = windows
    .flatMap(eventInSecond -> eventInSecond.count());

Skipping Stale Events by Using debounce()

Returns an Observable that mirrors the source Observable, except that it drops items emitted by the source Observable that are followed by newer items before a timeout value expires. The timer resets on each emission.

Note: If items keep being emitted by the source Observable faster than the timeout then no items will be emitted by the resulting Observable.

debounce(long,TimeUnit):
debounce(long,TimeUnit)

debounce(Func1)
debounce(Func1)

Observable<Long> timedDebounce(Observable<Long> upsteam) {
    Observable<Long> onTimeout = upstream
        .take(1)
        .concatWith(defer(() -> timedDebounce(upstream)));
    return upstream
            .debounce(100,MILLISECONDS)
            .timeout(1,SECONDS,onTimeout);
}

最后,安利一款自己写的基于MVP的Android开发框架
https://github.com/sxenon/Pure
欢迎大家拍砖,如果觉得好,麻烦多多star

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值