Skip to content

Commit a61828a

Browse files
committed
Rename object 'Time' -> 'TimeChannels' since 'Time' can be confused with general time utils.
1 parent d76bb83 commit a61828a

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -285,37 +285,36 @@ Channel.select(
285285
```
286286

287287
#### Time.after()
288-
Like in Go, you can use `Time.after(duration)` to generate a channel that sends a message after the specified time has
289-
passed. You don't need to close the channel afterward.
288+
In Go, you can use `Time.After(duration)` to generate a channel that sends a message after the specified time has
289+
passed. You don't need to close the channel afterward. The Scala implementation is `TimeChannels.after(duration)`
290290

291291
```scala
292292
import com.github.yruslan.channel._
293293

294294
Channel.select(
295-
someChannel.recver{v => println(s"Got a message in time: $v")},
296-
Time.after(Duration(10, TimeUnit.SECONDS)).recver{ v => println("Time is out!") }
295+
someChannel.recver { v => println(s"Got a message in time: $v") },
296+
TimeChannels.after(Duration(10, TimeUnit.SECONDS)).recver { v => println("Time is out!") }
297297
)
298298
```
299299

300300
#### Time.newTicker()
301-
Like in Go, you can use `Time.newTicker(duration)` to generate a channel that sends a message after the specified time.
301+
In Go, you can use `Time.newTicker(duration)` to generate a channel that sends a message after the specified time.
302302
When the message is consumed, the ticker will generate another message after the same time duration.
303-
Tickers should be closed after they are not in use.
303+
Tickers should be closed after they are not in use. The Scala implementation is `TimeChannels.ticker(duration)`
304304

305305
```scala
306306
import com.github.yruslan.channel._
307307

308-
val ticker = Time.newTicker(Duration(10, TimeUnit.SECONDS))
308+
val ticker = TimeChannels.ticker(Duration(10, TimeUnit.SECONDS))
309309

310310
Channel.select(
311-
someChannel.recver{v => println(s"Got a message: $v")},
312-
ticker.recver{ _ => println("Tick.") }
311+
someChannel.recver { v => println(s"Got a message: $v") },
312+
ticker.recver { _ => println("Tick.") }
313313
)
314314

315315
ticker.close()
316316
```
317317

318-
319318
### Non-blocking methods
320319
Go supports non-blocking channel operation by the elegant `default` clause in the `select` statement. The scala port
321320
adds separate methods that support non-blocking operations: `trySend()`, `tryRecv()` and `trySelect()`. There is an

src/main/scala/com/github/yruslan/channel/Time.scala renamed to src/main/scala/com/github/yruslan/channel/TimeChannels.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ import scala.concurrent.duration.Duration
2222
/**
2323
* The object contains channel generators similar to GoLang: https://gobyexample.com/tickers
2424
*/
25-
object Time {
25+
object TimeChannels {
2626
/**
2727
* Create a channel that gets a single message after the specified duration, and then closes.
2828
*
2929
* Example:
3030
* {{{
31-
* val after = Time.after(Duration(10, TimeUnit.MILLISECONDS))
31+
* val after = TimeChannels.after(Duration(10, TimeUnit.MILLISECONDS))
3232
* after.recv()
3333
* // 10 ms has passed.
3434
* // You don't need to close the channel
@@ -49,13 +49,13 @@ object Time {
4949
* Create a ticker channel, see https://gobyexample.com/tickers
5050
*
5151
* {{{
52-
* val ticker = Time.newTicker(Duration(10, TimeUnit.MILLISECONDS))
52+
* val ticker = TimeChannels.ticker(Duration(10, TimeUnit.MILLISECONDS))
5353
* val instant = ticker.recv()
5454
* ticker.close()
5555
* }}}
5656
*
5757
*/
58-
def newTicker(duration: Duration)(implicit executor: ExecutionContext): Channel[Instant] = {
58+
def ticker(duration: Duration)(implicit executor: ExecutionContext): Channel[Instant] = {
5959
val channel = Channel.make[Instant]
6060
Future {
6161
while (!channel.isClosed) {

src/test/scala/com/github/yruslan/channel/TimeSuite.scala renamed to src/test/scala/com/github/yruslan/channel/TimeChannelsSuite.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import java.util.concurrent.TimeUnit
2222
import scala.concurrent.ExecutionContext.Implicits.global
2323
import scala.concurrent.duration.Duration
2424

25-
class TimeSuite extends AnyWordSpec {
26-
"newTicker" should {
25+
class TimeChannelsSuite extends AnyWordSpec {
26+
"ticker" should {
2727
"generate a ticker that ticks until closed" in {
2828
val start = Instant.now()
2929

30-
val ticker = Time.newTicker(Duration(10, TimeUnit.MILLISECONDS))
30+
val ticker = TimeChannels.ticker(Duration(10, TimeUnit.MILLISECONDS))
3131

3232
ticker.recv()
3333
val middle = Instant.now()
@@ -42,7 +42,7 @@ class TimeSuite extends AnyWordSpec {
4242
}
4343

4444
"generate a ticker that ticks until closed111" in {
45-
val ticker = Time.newTicker(Duration(10, TimeUnit.MILLISECONDS))
45+
val ticker = TimeChannels.ticker(Duration(10, TimeUnit.MILLISECONDS))
4646

4747
val start = Instant.now()
4848
ticker.close()
@@ -56,7 +56,7 @@ class TimeSuite extends AnyWordSpec {
5656
"generate a single shot read channel" in {
5757
val start = Instant.now()
5858

59-
val after = Time.after(Duration(10, TimeUnit.MILLISECONDS))
59+
val after = TimeChannels.after(Duration(10, TimeUnit.MILLISECONDS))
6060

6161
after.recv()
6262
val finish = Instant.now()

0 commit comments

Comments
 (0)