Giter VIP home page Giter VIP logo

Comments (3)

adamgfraser avatar adamgfraser commented on May 28, 2024

@strokyl I'm not sure what you are expecting but this seems like exactly the documented behavior:

Delays the emission of values by holding new values for a set duration. If no new values arrive during that time the value is emitted, however if a new value is received during the holding period the previous value is discarded and the process is repeated with the new value.

A new value is emitted every 10 milliseconds while the specified duration is 20 milliseconds. So a new value is always emitted before the specified duration and the duration is reset until all of the values have been emitted.

from zio.

strokyl avatar strokyl commented on May 28, 2024

Ok sorry, I naively expected debounce to act like debounce in fs2 or throttle of Akka stream.
I was so convinced that debounce must act like fs2 one, that I had to read twice the scaladoc to realize they are in fact totally different.
But if I am not wrong the documentation here is not valid: https://zio.dev/reference/stream/zstream/operations/#debouncing.
Also, what is the equivalent of fs2 debounce ("Debounce the stream with a minimum period of d between each element") in ZIO stream?

  /**
   * Throttles the chunks of this stream according to the given bandwidth
   * parameters using the token bucket algorithm. Allows for burst in the
   * processing of elements by allowing the token bucket to accumulate tokens up
   * to a `units + burst` threshold. Chunks that do not meet the bandwidth
   * constraints are dropped. The weight of each chunk is determined by the
   * `costFn` function.
   */
  def throttleEnforce(units: => Long, duration: => Duration, burst: => Long = 0)(
    costFn: Chunk[A] => Long
  )(implicit trace: Trace): ZStream[R, E, A] =
    self >>> ZPipeline.throttleEnforce(units, duration, burst)(costFn)

This one I guess?

from zio.

adamgfraser avatar adamgfraser commented on May 28, 2024

@strokyl The example seems completely accurate. It states:

The ZStream#debounce method debounces the stream with a minimum period of d between each element:
val stream = (
  ZStream(1, 2, 3) ++
    ZStream.fromZIO(ZIO.sleep(500.millis)) ++ ZStream(4, 5) ++
    ZStream.fromZIO(ZIO.sleep(10.millis)) ++
    ZStream(6)
).debounce(100.millis) // emit only after a pause of at least 100 ms
// Output: 3, 6

Indeed, there needs to be a minimum period of d between each element, which is precisely why no element was emitted until the end of the stream in your original example. And the example given correctly illustrates this where 1 and 2 are not emited because 3 is emitted immediately after, 3 is emitted because 4 is not emitted until 500 milliseconds after, which is greater than 100 milliseconds, 4 is not emitted because 5 is emitted immediately after, 5 is not emitted because 6 is emitted 10 milliseconds after, which is less than 100 milliseconds, and 6 is emitted because nothing is emitted within 100 milliseconds after. The comment also accurately describes this and the result is indeed 3, 6.

It sounds like one of the throttle variants is what you are looking for.

from zio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.