From c43381f2f9b03532f8053968a7aef4f193f29336 Mon Sep 17 00:00:00 2001 From: y9vad9 Date: Sun, 24 Mar 2024 02:20:09 +0100 Subject: [PATCH] feat: `SmartValue.map` function --- .../org/timemates/remote/value/MappingSmartValue.kt | 12 ++++++++++++ .../kotlin/org/timemates/remote/value/SmartValue.kt | 4 ++++ 2 files changed, 16 insertions(+) create mode 100644 foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/MappingSmartValue.kt diff --git a/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/MappingSmartValue.kt b/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/MappingSmartValue.kt new file mode 100644 index 0000000..85084d1 --- /dev/null +++ b/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/MappingSmartValue.kt @@ -0,0 +1,12 @@ +package org.timemates.remote.value + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +internal class MappingSmartValue( + source: SmartValue, + mapper: suspend (T?) -> R? +) : SmartValue { + override val localValue: Flow = source.localValue.map(mapper) + override val remoteValue: Flow = source.remoteValue.map(mapper) +} \ No newline at end of file diff --git a/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/SmartValue.kt b/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/SmartValue.kt index 3128a58..2e8297c 100644 --- a/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/SmartValue.kt +++ b/foundation/smart-value/src/commonMain/kotlin/org/timemates/remote/value/SmartValue.kt @@ -63,4 +63,8 @@ public interface SmartValue { ) } } +} + +public fun SmartValue.map(transform: suspend (T?) -> R?): SmartValue { + return MappingSmartValue(this, transform) } \ No newline at end of file