【问题标题】:Deliver the first item as soon as it comes, debounce the following items第一个项目一到就交付,对以下项目进行去抖动
【发布时间】:2021-07-22 12:34:15
【问题描述】:

这个问题和thisthis 一样,但是关于Kotlin flow。

需要达到的目标:

  • 第一件商品一到就送达
  • 按照 debounce 功能的工作方式对以下所有项目进行 debounce

【问题讨论】:

    标签: kotlin kotlin-coroutines kotlin-flow


    【解决方案1】:

    动态去抖动超时有一个简单的解决方案:

    var firstEmission = true
    flow.debounce {
        if (firstEmission) {
            firstEmission = false
            0L
        } else DEBOUNCE_TIMEOUT
    }
    

    也可以这样做:

    merge(
        flow.take(1),
        flow.drop(1).debounce(DEBOUNCE_TIMEOUT)
    )
    

    【讨论】:

      猜你喜欢
      • 2015-07-20
      • 2023-03-29
      • 2017-09-16
      • 1970-01-01
      • 2018-09-13
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多