Rename Clock to TimeSource, ClockMark to TimeMark

Step 2: rename classes and interfaces.
Provide deprecated typealiases to smooth migration.
This commit is contained in:
Ilya Gorbunov
2020-01-13 21:08:00 +03:00
parent a11a25087a
commit 1336da8453
9 changed files with 194 additions and 155 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -10,20 +10,20 @@ import org.w3c.performance.Performance
@SinceKotlin("1.3")
@ExperimentalTime
public actual object MonoClock : Clock {
internal actual object MonotonicTimeSource : TimeSource {
private val actualClock: Clock = run {
private val actualSource: TimeSource = run {
val isNode: Boolean = js("typeof process !== 'undefined' && process.versions && !!process.versions.node")
if (isNode)
HrTimeClock(js("process").unsafeCast<Process>())
HrTimeSource(js("process").unsafeCast<Process>())
else
js("self").unsafeCast<GlobalPerformance?>()?.performance?.let(::PerformanceClock)
?: DateNowClock
js("self").unsafeCast<GlobalPerformance?>()?.performance?.let(::PerformanceTimeSource)
?: DateNowTimeSource
}
override fun markNow(): ClockMark = actualClock.markNow()
override fun markNow(): TimeMark = actualSource.markNow()
}
internal external interface Process {
@@ -32,27 +32,27 @@ internal external interface Process {
@SinceKotlin("1.3")
@ExperimentalTime
internal class HrTimeClock(val process: Process) : Clock {
internal class HrTimeSource(val process: Process) : TimeSource {
override fun markNow(): ClockMark = object : ClockMark() {
override fun markNow(): TimeMark = object : TimeMark() {
val startedAt = process.hrtime()
override fun elapsedNow(): Duration =
process.hrtime(startedAt).let { (seconds, nanos) -> seconds.seconds + nanos.nanoseconds }
}
override fun toString(): String = "Clock(process.hrtime())"
override fun toString(): String = "TimeSource(process.hrtime())"
}
@SinceKotlin("1.3")
@ExperimentalTime
internal class PerformanceClock(val performance: Performance) : AbstractDoubleClock(unit = DurationUnit.MILLISECONDS) {
internal class PerformanceTimeSource(val performance: Performance) : AbstractDoubleTimeSource(unit = DurationUnit.MILLISECONDS) {
override fun read(): Double = performance.now()
override fun toString(): String = "Clock(self.performance.now())"
override fun toString(): String = "TimeSource(self.performance.now())"
}
@SinceKotlin("1.3")
@ExperimentalTime
internal object DateNowClock : AbstractDoubleClock(unit = DurationUnit.MILLISECONDS) {
internal object DateNowTimeSource : AbstractDoubleTimeSource(unit = DurationUnit.MILLISECONDS) {
override fun read(): Double = kotlin.js.Date.now()
override fun toString(): String = "Clock(Date.now())"
override fun toString(): String = "TimeSource(Date.now())"
}