简体   繁体   English

如何从更快的原始流中延迟发出最新价值?

[英]How to emit latest value with delay from faster original flow?

I have 2 flows.我有 2 个流程。 First flow updates every 50ms.第一个流每 50 毫秒更新一次。 I have the second flow which is equal to first flow but i want it to produce latest value from original flow every 300ms.我有第二个流量等于第一个流量,但我希望它每 300 毫秒从原始流量中产生最新值。 I found debounce extension for flows but it doesn't work (note from docs):我找到了流的debounce扩展,但它不起作用(来自文档的注释):

Note that the resulting flow does not emit anything as long as the original flow emits items faster than every timeoutMillis milliseconds.

So when i'm trying to emit value each 300 ms with debounce it doesn't emit at all because original flow is faster than i need.因此,当我尝试使用去抖动每 300 毫秒发射一次值时,它根本不会发射,因为原始流量比我需要的要快。 So how can i make something like that:那么我怎样才能做这样的事情:

  1. delay 300ms延迟 300ms
  2. check original flow for latest value检查原始流的最新值
  3. emit this value发出这个值
  4. repeat重复

my flows right now:我现在的流程:

// fast flow (50ms)
val orientationFlow = merge(_orientationFlow, locationFlow)

val cameraBearingFlow = orientationFlow.debounce(300ms)

PS this approach doesn't fit because we are delaying value so it's not fresh after 300ms. PS 这种方法不适合,因为我们延迟了值,所以它在 300 毫秒后就不新鲜了。 I need to get the freshest value after 300ms:我需要在 300 毫秒后获取最新值:

val cameraBearingFlow = azimuthFlow.onEach { 
        delay(ORIENTATION_UPDATE_DELAY)
        it
    }

You need sample instead of denounce你需要sample而不是谴责

val f = fastFlow.sample(300)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM