mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
fixes in flow according to time zones
This commit is contained in:
parent
db1af7818e
commit
7e58ec0873
@ -8,6 +8,8 @@
|
||||
* Supporting of timezones
|
||||
* Any `KronScheduler` now can be used for calling `next` with `DateTimeTz`
|
||||
* New type `KronSchedulerTz`
|
||||
* `SchedulerFlow` has been deprecated
|
||||
* New extension `asTzFlow` and small changes in `asFlow` logic
|
||||
|
||||
## 0.5.1
|
||||
|
||||
|
@ -1,14 +1,39 @@
|
||||
package dev.inmo.krontab.utils
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.DateTimeTz
|
||||
import dev.inmo.krontab.KronScheduler
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.delay
|
||||
import dev.inmo.krontab.next
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* This [Flow] will trigger emitting each near time which will be returned from [this] [KronScheduler] with attention to
|
||||
* time zones
|
||||
*
|
||||
* @see channelFlow
|
||||
*/
|
||||
@FlowPreview
|
||||
fun KronScheduler.asFlow(): Flow<DateTime> = SchedulerFlow(this)
|
||||
fun KronScheduler.asTzFlow(): Flow<DateTimeTz> = channelFlow {
|
||||
while (isActive) {
|
||||
val now = DateTime.now().local
|
||||
val nextTime = next(now) ?: break
|
||||
val sleepDelay = (nextTime - DateTime.now().local).millisecondsLong
|
||||
delay(sleepDelay)
|
||||
send(nextTime)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is a map for [asTzFlow] and will works the same but return flow with [DateTime]s
|
||||
*/
|
||||
@FlowPreview
|
||||
fun KronScheduler.asFlow(): Flow<DateTime> = asTzFlow().map { it.local }
|
||||
|
||||
@Deprecated(
|
||||
"It is not recommended to use this class in future. This functionality will be removed soon",
|
||||
ReplaceWith("asFlow", "dev.inmo.krontab.utils.asFlow")
|
||||
)
|
||||
@FlowPreview
|
||||
class SchedulerFlow(
|
||||
private val scheduler: KronScheduler
|
||||
|
Loading…
Reference in New Issue
Block a user