diff --git a/.gitignore b/.gitignore index 9d766410e18..1d2d1b0e7ff 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ publishing.sh local.* local/ + +.kotlin/ diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt index ea2f738fe30..5e7c6380027 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/LaunchSafely.kt @@ -8,34 +8,34 @@ fun CoroutineScope.launchSafely( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyExceptionHandler, - block: suspend CoroutineScope.() -> Unit + block: suspend () -> Unit ) = launch(context, start) { - runCatchingSafely(onException) { block() } + runCatchingSafely(onException, block = block) } fun CoroutineScope.launchSafelyWithoutExceptions( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, - block: suspend CoroutineScope.() -> Unit + block: suspend () -> Unit ) = launch(context, start) { - runCatchingSafelyWithoutExceptions(onException) { block() } + runCatchingSafelyWithoutExceptions(onException, block = block) } fun CoroutineScope.asyncSafely( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyExceptionHandler, - block: suspend CoroutineScope.() -> T + block: suspend () -> T ) = async(context, start) { - runCatchingSafely(onException) { block() } + runCatchingSafely(onException, block = block) } fun CoroutineScope.asyncSafelyWithoutExceptions( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onException: ExceptionHandler = defaultSafelyWithoutExceptionHandlerWithNull, - block: suspend CoroutineScope.() -> T + block: suspend () -> T ) = async(context, start) { - runCatchingSafelyWithoutExceptions(onException) { block() } + runCatchingSafelyWithoutExceptions(onException, block = block) } diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SupervisorJob.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SupervisorJob.kt index df4131ddecd..8c4c3d010dc 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SupervisorJob.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SupervisorJob.kt @@ -10,8 +10,12 @@ fun CoroutineScope.LinkedSupervisorJob( additionalContext: CoroutineContext? = null ) = coroutineContext.LinkedSupervisorJob(additionalContext) -fun CoroutineScope.LinkedSupervisorScope( + +fun CoroutineContext.LinkedSupervisorScope( additionalContext: CoroutineContext? = null ) = CoroutineScope( - coroutineContext + LinkedSupervisorJob(additionalContext) + this + LinkedSupervisorJob(additionalContext) ) +fun CoroutineScope.LinkedSupervisorScope( + additionalContext: CoroutineContext? = null +) = coroutineContext.LinkedSupervisorScope(additionalContext) diff --git a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt index 2c4ffe1c5d7..5a2751d6db0 100644 --- a/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt +++ b/fsm/common/src/commonMain/kotlin/dev/inmo/micro_utils/fsm/common/StatesMachine.kt @@ -1,7 +1,6 @@ package dev.inmo.micro_utils.fsm.common import dev.inmo.micro_utils.common.Optional -import dev.inmo.micro_utils.common.onPresented import dev.inmo.micro_utils.coroutines.* import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler @@ -118,23 +117,28 @@ open class DefaultStatesMachine ( * [launchStateHandling] will returns some [State] then [statesManager] [StatesManager.update] will be used, otherwise * [StatesManager.endChain]. */ - override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions { - (statesManager.getActiveStates().asFlow() + statesManager.onStartChain).subscribeSafelyWithoutExceptions(this) { - launch { performStateUpdate(Optional.absent(), it, scope.LinkedSupervisorScope()) } - } - statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(this) { - launch { performStateUpdate(Optional.presented(it.first), it.second, scope.LinkedSupervisorScope()) } - } - statesManager.onEndChain.subscribeSafelyWithoutExceptions(this) { removedState -> - launch { - statesJobsMutex.withLock { - val stateInMap = statesJobs.keys.firstOrNull { stateInMap -> stateInMap == removedState } - if (stateInMap === removedState) { - statesJobs[stateInMap] ?.cancel() + override fun start(scope: CoroutineScope): Job { + val supervisorScope = scope.LinkedSupervisorScope() + supervisorScope.launchSafelyWithoutExceptions { + (statesManager.getActiveStates().asFlow() + statesManager.onStartChain).subscribeSafelyWithoutExceptions(supervisorScope) { + supervisorScope.launch { performStateUpdate(Optional.absent(), it, supervisorScope) } + } + statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(supervisorScope) { + supervisorScope.launch { performStateUpdate(Optional.presented(it.first), it.second, supervisorScope) } + } + statesManager.onEndChain.subscribeSafelyWithoutExceptions(supervisorScope) { removedState -> + supervisorScope.launch { + statesJobsMutex.withLock { + val stateInMap = statesJobs.keys.firstOrNull { stateInMap -> stateInMap == removedState } + if (stateInMap === removedState) { + statesJobs[stateInMap] ?.cancel() + } } } } } + + return supervisorScope.coroutineContext.job } /** diff --git a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/TemporalFilesRoutingConfigurator.kt b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/TemporalFilesRoutingConfigurator.kt index a017e87a7a2..dd45d06741a 100644 --- a/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/TemporalFilesRoutingConfigurator.kt +++ b/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/ktor/server/TemporalFilesRoutingConfigurator.kt @@ -41,7 +41,7 @@ class TemporalFilesRoutingConfigurator( filesMutex: Mutex, onNewFileFlow: Flow ): Job = scope.launchSafelyWithoutExceptions { - while (isActive) { + while (currentCoroutineContext().isActive) { val filesWithCreationInfo = filesMap.mapNotNull { (fileId, file) -> fileId to ((Files.getAttribute(file.toPath(), "creationTime") as? FileTime) ?.toMillis() ?: return@mapNotNull null) }