yruslan / channel_scala

Scala implementation of the concurrency primitive similar to GoLang channels.
Apache License 2.0
8 stars 0 forks source link

Add support for `time.After()` and `time.Tick()` #24

Closed yruslan closed 7 months ago

yruslan commented 10 months ago

Describe the feature

Adding support of time.After() and time.Tick() can be helpful for implementing some GoLang patterns in Scala.

Here is more info about the API in Go: https://pkg.go.dev/time

Both the ticker and timeout can return java.time.Instant

An example code can look like:

import scala.concurrent.duration.{Duration, SECONDS, MILLISECONDS}
import com.github.yruslan.channel._

val channel = Channel.make[Int]
val ticker = TimeTickr(Duration(200, MILLISECONDS)
val timeout = TimeAfter(Duration(2, SECONDS)

select(
  channel .recver(n => {
    println(s"Received $n")
  }),
  ticker.recver(tickInstant => {
    println(s"Received $tickInstant")
  }),
  ticker .recver(timeoutInstant => {
    println(s"Received $timeoutInstant ")
  })
)