Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 31, 2025

This PR adds a new delay operation to the RxOps trait that inserts an artificial delay before emitting each element from the source stream. This is similar to the tap operator but adds a configurable delay.

Changes

  • Added DelayOp[A] case class: A new operator that extends UnaryRx[A, A] for delaying stream elements
  • Added delay method to RxOps trait: Allows chaining .delay(interval, unit) on any Rx stream
  • Implemented DelayOp handling in RxRunner: Uses compat.scheduleOnce to asynchronously delay each element
  • Added comprehensive tests: Demonstrates timing behavior and value preservation using AirSpec async testing
  • Updated documentation: Clarified the difference between the existing timer-based Rx.delay and the new stream-based delay operation

Usage

import wvlet.airframe.rx.Rx
import java.util.concurrent.TimeUnit

// Delay each element by 100ms before emission
val rx = Rx.sequence(1, 2, 3)
  .delay(100, TimeUnit.MILLISECONDS)
  .tap(x => println(s"Received: $x"))

val c = rx.run()
// Elements will be emitted with 100ms delay between each

Key Features

  • Preserves original values: Like tap, the delay operation passes through values unchanged
  • Configurable timing: Supports any TimeUnit for flexible delay configuration
  • Cross-platform support: Works on JVM, JS, and Native platforms
  • Async implementation: Uses platform-specific scheduling for non-blocking delays
  • Composable: Can be chained with other Rx operators

Distinction from existing Rx.delay

The existing Rx.delay(interval, unit) creates a timer stream that emits 0 once after a delay period. This new operation delays elements in an existing stream, making it more useful for throttling or artificial latency simulation.

Fixes #3871.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: xerial <57538+xerial@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] feature: Add Rx.delay(..) operation to insert artificial delay feature: Add Rx.delay(..) operation to insert artificial delay May 31, 2025
@Copilot Copilot AI requested a review from xerial May 31, 2025 15:30
Copilot finished work on behalf of xerial May 31, 2025 15:30
@xerial xerial marked this pull request as ready for review June 2, 2025 17:22
@xerial xerial merged commit 61f0d32 into main Jun 2, 2025
33 checks passed
@xerial xerial deleted the copilot/fix-3871-2 branch June 2, 2025 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: Add Rx.delay(..) operation to insert artificial delay
2 participants