From 540e72231b423c252ebca3d02997090001752a79 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 8 Sep 2020 10:44:42 +0300 Subject: [PATCH] Error propagation for DeviceBase --- .../hep/dataforge/control/base/DeviceBase.kt | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/base/DeviceBase.kt b/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/base/DeviceBase.kt index 879b8ba..7ffc574 100644 --- a/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/base/DeviceBase.kt +++ b/dataforge-device-core/src/commonMain/kotlin/hep/dataforge/control/base/DeviceBase.kt @@ -5,14 +5,11 @@ import hep.dataforge.control.api.Device import hep.dataforge.control.api.DeviceListener import hep.dataforge.control.api.PropertyDescriptor import hep.dataforge.meta.MetaItem -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.* import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import kotlinx.coroutines.withContext /** * Baseline implementation of [Device] interface @@ -104,9 +101,9 @@ public abstract class DeviceBase : Device { //backup current value val currentValue = value return if (force || currentValue == null) { - val res = withContext(scope.coroutineContext) { - //all device operations should be run on device context - //TODO add error catching + //all device operations should be run on device context + //propagate error, but do not fail scope + val res = withContext(scope.coroutineContext + SupervisorJob(scope.coroutineContext[Job])) { getter(currentValue) } updateLogical(res) @@ -167,8 +164,7 @@ public abstract class DeviceBase : Device { if (item == value) return@withLock val oldValue = value //all device operations should be run on device context - withContext(scope.coroutineContext) { - //TODO add error catching + withContext(scope.coroutineContext + SupervisorJob(scope.coroutineContext[Job])) { setter(oldValue, item)?.let { updateLogical(it) } @@ -206,11 +202,14 @@ public abstract class DeviceBase : Device { override val descriptor: ActionDescriptor, private val block: suspend (MetaItem<*>?) -> MetaItem<*>?, ) : Action { - override suspend fun invoke(arg: MetaItem<*>?): MetaItem<*>? = block(arg).also { - notifyListeners { - actionExecuted(name, arg, it) + override suspend fun invoke(arg: MetaItem<*>?): MetaItem<*>? = + withContext(scope.coroutineContext + SupervisorJob(scope.coroutineContext[Job])) { + block(arg).also { + notifyListeners { + actionExecuted(name, arg, it) + } + } } - } } /**