From d67be582af5da37e8078e3c9bb2674963648b5ed Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 20 Jun 2023 11:35:57 +0600 Subject: [PATCH] nullable since in asFlowWithoutDelays and asTzFlowWithoutDelays --- CHANGELOG.md | 8 ++++++++ .../kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt | 14 ++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c5af0..e1b9da8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## 2.1.1 +* `asFlowWithoutDelays` and `asTzFlowWithoutDelays` will have nullable `since` parameters with default to `null` + to avoid any inconsistency of `Flow` idiom. +
+ About the reason of changes + Cold flows should not contain some state by default. So, it was not right to save some `DateTime`/`DateTimeTz` + by default. Now it will not use some external state unless developers will set it manually +
+ ## 2.1.0 * Versions diff --git a/src/commonMain/kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt b/src/commonMain/kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt index 55971d2..a3bf055 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/utils/SchedulerFlow.kt @@ -18,10 +18,11 @@ import kotlinx.coroutines.isActive * Will emit all the [KronScheduler.next] as soon as possible. In case [KronScheduler.next] return null, flow will * be completed * - * @param since Will be used as the first parameter for [KronScheduler.next] fun + * @param since Will be used as the first parameter for [KronScheduler.next] fun. If passed null, `flow` + * will always start since the moment of collecting start */ -fun KronScheduler.asTzFlowWithoutDelays(since: DateTimeTz = DateTime.nowLocal()): Flow = flow { - var previous = since +fun KronScheduler.asTzFlowWithoutDelays(since: DateTimeTz? = null): Flow = flow { + var previous = since ?: DateTime.nowLocal() while (currentCoroutineContext().isActive) { val next = next(previous) ?: break emit(next) @@ -57,10 +58,11 @@ fun KronScheduler.asTzFlow(): Flow = asTzFlowWithDelays() * Will emit all the [KronScheduler.next] as soon as possible. In case [KronScheduler.next] return null, flow will * be completed * - * @param since Will be used as the first parameter for [KronScheduler.next] fun + * @param since Will be used as the first parameter for [KronScheduler.next] fun. If passed null, `flow` + * will always start since the moment of collecting start */ -fun KronScheduler.asFlowWithoutDelays(since: DateTime = DateTime.now()): Flow = flow { - var previous = since +fun KronScheduler.asFlowWithoutDelays(since: DateTime? = null): Flow = flow { + var previous = since ?: DateTime.now() while (currentCoroutineContext().isActive) { val next = next(previous) ?: break emit(next)