Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion airframe-rx/src/main/scala/wvlet/airframe/rx/Rx.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ trait Rx[+A] extends RxOps[A] {

def withName(name: String): Rx[A] = NamedOp(this, name)

/**
* Emit the new value after the given time interval.
* @param interval
* @param unit
* @return
*/
def delay(interval: Long, unit: TimeUnit): Rx[A] = DelayOp(this, interval, unit)

/**
* Applies `f` to the input value and return the result.
* @param f
Expand Down Expand Up @@ -1031,7 +1039,9 @@ object Rx extends LogSupport {
case class TimerOp(interval: Long, unit: TimeUnit) extends Rx[Long] {
override def parents: Seq[RxOps[_]] = Seq.empty
}

case class DelayOp[A](input: RxOps[A], interval: Long, unit: TimeUnit) extends UnaryRx[A, A] {
override def parents: Seq[RxOps[_]] = Seq(input)
}
case class TakeOp[A](input: RxOps[A], n: Long) extends Rx[A] {
override def parents: Seq[RxOps[_]] = Seq(input)
}
Expand Down
12 changes: 12 additions & 0 deletions airframe-rx/src/main/scala/wvlet/airframe/rx/RxRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ class RxRunner(
effect(OnError(e))
}
}
case DelayOp(in, interval, unit) =>
val delayMillis = TimeUnit.MILLISECONDS.convert(interval, unit).max(1)
run(in) {
case OnNext(v) =>
// Delay the emission of the value
compat.scheduleOnce(delayMillis) {
effect(OnNext(v.asInstanceOf[A]))
}
RxResult.Continue
case other =>
effect(other)
}
case ThrottleFirstOp(in, interval, unit) =>
var lastUpdateTimeNanos = -interval
run(in) {
Expand Down
Loading