Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement EventStream.periodic #31

Closed
raquo opened this issue May 24, 2020 · 3 comments
Closed

Implement EventStream.periodic #31

raquo opened this issue May 24, 2020 · 3 comments
Labels
enhancement New feature or request

Comments

@raquo
Copy link
Owner

raquo commented May 24, 2020

Need a stream that emits values at an interval when it's active. it's possible to emulate with a DIY EventBus hack, but we should have a proper way to do this.

For API design, bare minimum is to emit Unit-s. But it might be quite cheap and useful to emit an index-like number instead. But then we need an option to reset the number on deactivation. Maybe make the counter a separate method.

Also, consider putting a limit on number of emitted values? But that will be more useful as a take() or until(), and well that's more of a #23 thing, so maybe another day.

@raquo raquo added the enhancement New feature or request label May 24, 2020
@swoogles
Copy link

swoogles commented Sep 7, 2020

Until this is implemented, could you post an example of the "EventBus hack"?

I finally decided to take the plunge and rewrite one of my projects with Laminar, but the first thing I need to do is trigger updates based on a clock tick. Basically-

val diffBus = new EventBus[Int] 
dom.window.setInterval(() => +1 --> diffBus, 10000)

@keynmol
Copy link
Sponsor

keynmol commented Sep 7, 2020

Following the conversation on gitter, this seems to do the trick:

import com.raquo.laminar.api.L._
import org.scalajs.dom

val diffBus = new EventBus[Int]
dom.window.setInterval(() =>  diffBus.writer.onNext(scala.util.Random.nextInt), 1000)

val x = div(
  "Random number every second: ",
  child.text <-- diffBus.events.map(_.toString)
)

render(dom.document.getElementById("output"), x)

On ScalaFiddle: https://scalafiddle.io/sf/TbnkMEu/20

Seems like the simplest version of EventStream.periodic can be included in Airstream with caveats, as it's missing from the current docs almost completely. What do you think @raquo ?

@raquo raquo closed this as completed in b2efbff Sep 9, 2020
@raquo
Copy link
Owner Author

raquo commented Sep 9, 2020

Alright, I implemented a proper EventStream.periodic in Airstream. Will publish Airstream 0.10.1 with it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants