mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-18 14:59:24 +00:00
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
827cf32c1b | |||
98ad6dbeb2 | |||
63c8f642ec | |||
3bfe64f797 | |||
ec98029467 | |||
ab58478686 | |||
90247667d1 | |||
e661185534 | |||
d73e4e8e1f | |||
a6905e73cb | |||
93b054d55e | |||
5db4c5c717 | |||
5e04521929 | |||
30440b4ed1 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -17,3 +17,5 @@ publishing.sh
|
|||||||
|
|
||||||
local.*
|
local.*
|
||||||
local/
|
local/
|
||||||
|
|
||||||
|
.kotlin/
|
||||||
|
18
CHANGELOG.md
18
CHANGELOG.md
@@ -1,5 +1,23 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.21.1
|
||||||
|
|
||||||
|
* `KSP`:
|
||||||
|
* Module has been initialized
|
||||||
|
* `Generator`:
|
||||||
|
* Module has been initialized
|
||||||
|
* `Sealed`:
|
||||||
|
* Module has been initialized
|
||||||
|
|
||||||
|
## 0.21.0
|
||||||
|
|
||||||
|
**THIS UPDATE CONTAINS BREAKING CHANGES IN `safely*`-ORIENTED FUNCTIONS**
|
||||||
|
|
||||||
|
* `Coroutines`:
|
||||||
|
* **All `safely` functions lost their `supervisorScope` in favor to wrapping `runCatching`**
|
||||||
|
* `runCatchingSafely` is the main handling function of all `safely` functions
|
||||||
|
* `launchSafely*` and `asyncSafely*` blocks lost `CoroutineScope` as their receiver
|
||||||
|
|
||||||
## 0.20.52
|
## 0.20.52
|
||||||
|
|
||||||
* `Coroutines`:
|
* `Coroutines`:
|
||||||
|
@@ -0,0 +1,44 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
|
|
||||||
|
typealias ExceptionHandler<T> = suspend (Throwable) -> T
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This instance will be used in all calls of [safely] where exception handler has not been passed
|
||||||
|
*/
|
||||||
|
var defaultSafelyExceptionHandler: ExceptionHandler<Nothing> = { throw it }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This instance will be used in all calls of [safelyWithoutExceptions] as an exception handler for [safely] call
|
||||||
|
*/
|
||||||
|
var defaultSafelyWithoutExceptionHandler: ExceptionHandler<Unit> = {
|
||||||
|
try {
|
||||||
|
defaultSafelyExceptionHandler(it)
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This key can (and will) be used to get [ContextSafelyExceptionHandler] from [coroutineContext] of suspend functions
|
||||||
|
* and in [ContextSafelyExceptionHandler] for defining of its [CoroutineContext.Element.key]
|
||||||
|
*
|
||||||
|
* @see safelyWithContextExceptionHandler
|
||||||
|
* @see ContextSafelyExceptionHandler
|
||||||
|
*/
|
||||||
|
object ContextSafelyExceptionHandlerKey : CoroutineContext.Key<ContextSafelyExceptionHandler>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ExceptionHandler] wrapper which was created to make possible to use [handler] across all coroutines calls
|
||||||
|
*
|
||||||
|
* @see safelyWithContextExceptionHandler
|
||||||
|
* @see ContextSafelyExceptionHandlerKey
|
||||||
|
*/
|
||||||
|
class ContextSafelyExceptionHandler(
|
||||||
|
val handler: ExceptionHandler<Unit>
|
||||||
|
) : CoroutineContext.Element {
|
||||||
|
override val key: CoroutineContext.Key<*>
|
||||||
|
get() = ContextSafelyExceptionHandlerKey
|
||||||
|
}
|
@@ -4,46 +4,58 @@ import kotlinx.coroutines.*
|
|||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.coroutines.coroutineContext
|
import kotlin.coroutines.coroutineContext
|
||||||
|
|
||||||
typealias ExceptionHandler<T> = suspend (Throwable) -> T
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This instance will be used in all calls of [safely] where exception handler has not been passed
|
* Launching [block] in [runCatching]. In case of failure, it will:
|
||||||
|
*
|
||||||
|
* * Try to get [ContextSafelyExceptionHandler] from current [coroutineContext] and call its
|
||||||
|
* [ContextSafelyExceptionHandler.handler] invoke. **Thrown exception from its handler
|
||||||
|
* will pass out of [runCatchingSafely]**
|
||||||
|
* * Execute [onException] inside of new [runCatching] and return its result. Throws exception
|
||||||
|
* will be caught by [runCatching] and wrapped in [Result]
|
||||||
|
*
|
||||||
|
* @return [Result] with result of [block] if no exceptions or [Result] from [onException] execution
|
||||||
*/
|
*/
|
||||||
var defaultSafelyExceptionHandler: ExceptionHandler<Nothing> = { throw it }
|
suspend inline fun <T> runCatchingSafely(
|
||||||
|
onException: ExceptionHandler<T>,
|
||||||
/**
|
block: suspend () -> T
|
||||||
* This instance will be used in all calls of [safelyWithoutExceptions] as an exception handler for [safely] call
|
): Result<T> {
|
||||||
*/
|
return runCatching {
|
||||||
var defaultSafelyWithoutExceptionHandler: ExceptionHandler<Unit> = {
|
block()
|
||||||
try {
|
}.onFailure {
|
||||||
defaultSafelyExceptionHandler(it)
|
coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?.invoke(it)
|
||||||
} catch (e: Throwable) {
|
return runCatching {
|
||||||
// do nothing
|
onException(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
suspend inline fun <T, R> R.runCatchingSafely(
|
||||||
* This key can (and will) be used to get [ContextSafelyExceptionHandler] from [coroutineContext] of suspend functions
|
onException: ExceptionHandler<T>,
|
||||||
* and in [ContextSafelyExceptionHandler] for defining of its [CoroutineContext.Element.key]
|
block: suspend R.() -> T
|
||||||
*
|
): Result<T> = runCatchingSafely<T>(onException) {
|
||||||
* @see safelyWithContextExceptionHandler
|
block()
|
||||||
* @see ContextSafelyExceptionHandler
|
}
|
||||||
*/
|
|
||||||
object ContextSafelyExceptionHandlerKey : CoroutineContext.Key<ContextSafelyExceptionHandler>
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [ExceptionHandler] wrapper which was created to make possible to use [handler] across all coroutines calls
|
* Launching [runCatchingSafely] with [defaultSafelyExceptionHandler] as `onException` parameter
|
||||||
*
|
|
||||||
* @see safelyWithContextExceptionHandler
|
|
||||||
* @see ContextSafelyExceptionHandlerKey
|
|
||||||
*/
|
*/
|
||||||
class ContextSafelyExceptionHandler(
|
suspend inline fun <T> runCatchingSafely(
|
||||||
val handler: ExceptionHandler<Unit>
|
block: suspend () -> T
|
||||||
) : CoroutineContext.Element {
|
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
|
||||||
override val key: CoroutineContext.Key<*>
|
|
||||||
get() = ContextSafelyExceptionHandlerKey
|
suspend inline fun <T, R> R.runCatchingSafely(
|
||||||
|
block: suspend R.() -> T
|
||||||
|
): Result<T> = runCatchingSafely<T> {
|
||||||
|
block()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//suspend inline fun <T, R> T.runCatchingSafely(
|
||||||
|
// onException: ExceptionHandler<R>,
|
||||||
|
// block: suspend T.() -> R
|
||||||
|
//): Result<R> = runCatchingSafely(onException) {
|
||||||
|
// block()
|
||||||
|
//}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return [ContextSafelyExceptionHandler] from [coroutineContext] by key [ContextSafelyExceptionHandlerKey] if
|
* @return [ContextSafelyExceptionHandler] from [coroutineContext] by key [ContextSafelyExceptionHandlerKey] if
|
||||||
* exists
|
* exists
|
||||||
@@ -64,7 +76,7 @@ suspend fun contextSafelyExceptionHandler() = coroutineContext[ContextSafelyExce
|
|||||||
suspend fun <T> safelyWithContextExceptionHandler(
|
suspend fun <T> safelyWithContextExceptionHandler(
|
||||||
contextExceptionHandler: ExceptionHandler<Unit>,
|
contextExceptionHandler: ExceptionHandler<Unit>,
|
||||||
safelyExceptionHandler: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
safelyExceptionHandler: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend () -> T
|
||||||
): T {
|
): T {
|
||||||
val contextSafelyExceptionHandler = contextSafelyExceptionHandler() ?.handler ?.let { oldHandler ->
|
val contextSafelyExceptionHandler = contextSafelyExceptionHandler() ?.handler ?.let { oldHandler ->
|
||||||
ContextSafelyExceptionHandler {
|
ContextSafelyExceptionHandler {
|
||||||
@@ -78,57 +90,35 @@ suspend fun <T> safelyWithContextExceptionHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions
|
* Calls [runCatchingSafely] and getting the result via [Result.getOrThrow]
|
||||||
*
|
*
|
||||||
* Priorities of [ExceptionHandler]s:
|
* @see runCatchingSafely
|
||||||
*
|
|
||||||
* * [onException] In case if custom (will be used anyway if not [defaultSafelyExceptionHandler])
|
|
||||||
* * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key
|
|
||||||
* * [defaultSafelyExceptionHandler]
|
|
||||||
*
|
|
||||||
* Remember, that [ExceptionHandler] from [CoroutineContext.get] will be used anyway if it is available. After it will
|
|
||||||
* be called [onException]
|
|
||||||
*
|
|
||||||
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
|
|
||||||
* exception will be available for catching
|
|
||||||
*
|
|
||||||
* @see defaultSafelyExceptionHandler
|
|
||||||
* @see safelyWithoutExceptions
|
|
||||||
* @see safelyWithContextExceptionHandler
|
|
||||||
*/
|
*/
|
||||||
suspend fun <T> safely(
|
suspend inline fun <T> safely(
|
||||||
onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
onException: ExceptionHandler<T>,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend () -> T
|
||||||
): T {
|
): T = runCatchingSafely(onException, block).getOrThrow()
|
||||||
return try {
|
|
||||||
supervisorScope(block)
|
|
||||||
} catch (e: Throwable) {
|
|
||||||
coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?.invoke(e)
|
|
||||||
onException(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun <T> runCatchingSafely(
|
/**
|
||||||
onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
* Calls [safely] with passing of [defaultSafelyExceptionHandler] as `onException`
|
||||||
block: suspend CoroutineScope.() -> T
|
*
|
||||||
): Result<T> = runCatching {
|
* @see runCatchingSafely
|
||||||
safely(onException, block)
|
*/
|
||||||
}
|
suspend inline fun <T> safely(
|
||||||
|
block: suspend () -> T
|
||||||
suspend fun <T, R> T.runCatchingSafely(
|
): T = safely(defaultSafelyExceptionHandler, block)
|
||||||
onException: ExceptionHandler<R> = defaultSafelyExceptionHandler,
|
suspend inline fun <T, R> R.safely(
|
||||||
block: suspend T.() -> R
|
block: suspend R.() -> T
|
||||||
): Result<R> = runCatching {
|
): T = safely<T> { block() }
|
||||||
safely(onException) { block() }
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Deprecated("Renamed", ReplaceWith("runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely"))
|
||||||
suspend fun <T> safelyWithResult(
|
suspend fun <T> safelyWithResult(
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend () -> T
|
||||||
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
|
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
|
||||||
|
@Deprecated("Renamed", ReplaceWith("this.runCatchingSafely(block)", "dev.inmo.micro_utils.coroutines.runCatchingSafely"))
|
||||||
suspend fun <T, R> T.safelyWithResult(
|
suspend fun <T, R> R.safelyWithResult(
|
||||||
block: suspend T.() -> R
|
block: suspend R.() -> T
|
||||||
): Result<R> = runCatchingSafely(defaultSafelyExceptionHandler, block)
|
): Result<T> = safelyWithResult<T> { block() }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
|
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
|
||||||
@@ -148,18 +138,20 @@ val defaultSafelyWithoutExceptionHandlerWithNull: ExceptionHandler<Nothing?> = {
|
|||||||
* result from exception (instead of throwing it, by default always returns null)
|
* result from exception (instead of throwing it, by default always returns null)
|
||||||
*/
|
*/
|
||||||
suspend fun <T> safelyWithoutExceptions(
|
suspend fun <T> safelyWithoutExceptions(
|
||||||
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend () -> T
|
||||||
): T? = safely(onException, block)
|
): T? = runCatchingSafely(onException, block).getOrNull()
|
||||||
|
|
||||||
suspend fun <T> runCatchingSafelyWithoutExceptions(
|
suspend fun <T> runCatchingSafelyWithoutExceptions(
|
||||||
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
onException: ExceptionHandler<T?> = defaultSafelyExceptionHandler,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend () -> T
|
||||||
): Result<T?> = runCatching {
|
): Result<T?> = runCatchingSafely(onException, block).let {
|
||||||
safelyWithoutExceptions(onException, block)
|
if (it.isFailure) return Result.success<T?>(null)
|
||||||
|
|
||||||
|
it
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CoroutineScope(
|
fun CoroutineScopeWithDefaultFallback(
|
||||||
context: CoroutineContext,
|
context: CoroutineContext,
|
||||||
defaultExceptionsHandler: ExceptionHandler<Unit>
|
defaultExceptionsHandler: ExceptionHandler<Unit>
|
||||||
) = CoroutineScope(
|
) = CoroutineScope(
|
||||||
|
@@ -10,7 +10,9 @@ fun CoroutineScope.launchSafely(
|
|||||||
onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
|
onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
|
||||||
block: suspend CoroutineScope.() -> Unit
|
block: suspend CoroutineScope.() -> Unit
|
||||||
) = launch(context, start) {
|
) = launch(context, start) {
|
||||||
safely(onException, block)
|
runCatchingSafely(onException) {
|
||||||
|
block()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun CoroutineScope.launchSafelyWithoutExceptions(
|
fun CoroutineScope.launchSafelyWithoutExceptions(
|
||||||
@@ -19,7 +21,9 @@ fun CoroutineScope.launchSafelyWithoutExceptions(
|
|||||||
onException: ExceptionHandler<Unit?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
onException: ExceptionHandler<Unit?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||||
block: suspend CoroutineScope.() -> Unit
|
block: suspend CoroutineScope.() -> Unit
|
||||||
) = launch(context, start) {
|
) = launch(context, start) {
|
||||||
safelyWithoutExceptions(onException, block)
|
runCatchingSafelyWithoutExceptions(onException) {
|
||||||
|
block()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> CoroutineScope.asyncSafely(
|
fun <T> CoroutineScope.asyncSafely(
|
||||||
@@ -28,7 +32,9 @@ fun <T> CoroutineScope.asyncSafely(
|
|||||||
onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend CoroutineScope.() -> T
|
||||||
) = async(context, start) {
|
) = async(context, start) {
|
||||||
safely(onException, block)
|
runCatchingSafely(onException) {
|
||||||
|
block()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> CoroutineScope.asyncSafelyWithoutExceptions(
|
fun <T> CoroutineScope.asyncSafelyWithoutExceptions(
|
||||||
@@ -37,5 +43,7 @@ fun <T> CoroutineScope.asyncSafelyWithoutExceptions(
|
|||||||
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
|
||||||
block: suspend CoroutineScope.() -> T
|
block: suspend CoroutineScope.() -> T
|
||||||
) = async(context, start) {
|
) = async(context, start) {
|
||||||
safelyWithoutExceptions(onException, block)
|
runCatchingSafelyWithoutExceptions(onException) {
|
||||||
|
block()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,12 @@ fun CoroutineScope.LinkedSupervisorJob(
|
|||||||
additionalContext: CoroutineContext? = null
|
additionalContext: CoroutineContext? = null
|
||||||
) = coroutineContext.LinkedSupervisorJob(additionalContext)
|
) = coroutineContext.LinkedSupervisorJob(additionalContext)
|
||||||
|
|
||||||
fun CoroutineScope.LinkedSupervisorScope(
|
|
||||||
|
fun CoroutineContext.LinkedSupervisorScope(
|
||||||
additionalContext: CoroutineContext? = null
|
additionalContext: CoroutineContext? = null
|
||||||
) = CoroutineScope(
|
) = CoroutineScope(
|
||||||
coroutineContext + LinkedSupervisorJob(additionalContext)
|
this + LinkedSupervisorJob(additionalContext)
|
||||||
)
|
)
|
||||||
|
fun CoroutineScope.LinkedSupervisorScope(
|
||||||
|
additionalContext: CoroutineContext? = null
|
||||||
|
) = coroutineContext.LinkedSupervisorScope(additionalContext)
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.fsm.common
|
package dev.inmo.micro_utils.fsm.common
|
||||||
|
|
||||||
import dev.inmo.micro_utils.common.Optional
|
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.coroutines.*
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
|
||||||
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
|
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
|
||||||
@@ -118,23 +117,28 @@ open class DefaultStatesMachine <T: State>(
|
|||||||
* [launchStateHandling] will returns some [State] then [statesManager] [StatesManager.update] will be used, otherwise
|
* [launchStateHandling] will returns some [State] then [statesManager] [StatesManager.update] will be used, otherwise
|
||||||
* [StatesManager.endChain].
|
* [StatesManager.endChain].
|
||||||
*/
|
*/
|
||||||
override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions {
|
override fun start(scope: CoroutineScope): Job {
|
||||||
(statesManager.getActiveStates().asFlow() + statesManager.onStartChain).subscribeSafelyWithoutExceptions(this) {
|
val supervisorScope = scope.LinkedSupervisorScope()
|
||||||
launch { performStateUpdate(Optional.absent(), it, scope.LinkedSupervisorScope()) }
|
supervisorScope.launchSafelyWithoutExceptions {
|
||||||
}
|
(statesManager.getActiveStates().asFlow() + statesManager.onStartChain).subscribeSafelyWithoutExceptions(supervisorScope) {
|
||||||
statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(this) {
|
supervisorScope.launch { performStateUpdate(Optional.absent(), it, supervisorScope) }
|
||||||
launch { performStateUpdate(Optional.presented(it.first), it.second, scope.LinkedSupervisorScope()) }
|
}
|
||||||
}
|
statesManager.onChainStateUpdated.subscribeSafelyWithoutExceptions(supervisorScope) {
|
||||||
statesManager.onEndChain.subscribeSafelyWithoutExceptions(this) { removedState ->
|
supervisorScope.launch { performStateUpdate(Optional.presented(it.first), it.second, supervisorScope) }
|
||||||
launch {
|
}
|
||||||
statesJobsMutex.withLock {
|
statesManager.onEndChain.subscribeSafelyWithoutExceptions(supervisorScope) { removedState ->
|
||||||
val stateInMap = statesJobs.keys.firstOrNull { stateInMap -> stateInMap == removedState }
|
supervisorScope.launch {
|
||||||
if (stateInMap === removedState) {
|
statesJobsMutex.withLock {
|
||||||
statesJobs[stateInMap] ?.cancel()
|
val stateInMap = statesJobs.keys.firstOrNull { stateInMap -> stateInMap == removedState }
|
||||||
|
if (stateInMap === removedState) {
|
||||||
|
statesJobs[stateInMap] ?.cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return supervisorScope.coroutineContext.job
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -15,5 +15,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.20.52
|
version=0.21.1
|
||||||
android_code_version=258
|
android_code_version=260
|
||||||
|
20
ksp/generator/build.gradle
Normal file
20
ksp/generator/build.gradle
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.jvm"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$publishJvmOnlyPath"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api project(":micro_utils.common")
|
||||||
|
api libs.kotlin.poet
|
||||||
|
api libs.ksp
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
}
|
49
ksp/generator/src/main/kotlin/FilesWorkaround.kt
Normal file
49
ksp/generator/src/main/kotlin/FilesWorkaround.kt
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package dev.inmo.micro_ksp.generator
|
||||||
|
|
||||||
|
import com.google.devtools.ksp.symbol.KSClassDeclaration
|
||||||
|
import com.google.devtools.ksp.symbol.KSFile
|
||||||
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
fun KSClassDeclaration.writeFile(
|
||||||
|
prefix: String = "",
|
||||||
|
suffix: String = "",
|
||||||
|
relatedPath: String = "",
|
||||||
|
force: Boolean = false,
|
||||||
|
fileSpecBuilder: () -> FileSpec
|
||||||
|
) {
|
||||||
|
val containingFile = containingFile!!
|
||||||
|
File(
|
||||||
|
File(
|
||||||
|
File(containingFile.filePath).parent,
|
||||||
|
relatedPath
|
||||||
|
),
|
||||||
|
"$prefix${simpleName.asString()}$suffix.kt"
|
||||||
|
).takeIf { force || !it.exists() } ?.apply {
|
||||||
|
parentFile.mkdirs()
|
||||||
|
writer().use { writer ->
|
||||||
|
fileSpecBuilder().writeTo(writer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun KSFile.writeFile(
|
||||||
|
prefix: String = "",
|
||||||
|
suffix: String = "",
|
||||||
|
relatedPath: String = "",
|
||||||
|
force: Boolean = false,
|
||||||
|
fileSpecBuilder: () -> FileSpec
|
||||||
|
) {
|
||||||
|
File(
|
||||||
|
File(
|
||||||
|
File(filePath).parent,
|
||||||
|
relatedPath
|
||||||
|
),
|
||||||
|
"$prefix${fileName.dropLastWhile { it != '.' }.removeSuffix(".")}$suffix.kt"
|
||||||
|
).takeIf { force || !it.exists() } ?.apply {
|
||||||
|
parentFile.mkdirs()
|
||||||
|
writer().use { writer ->
|
||||||
|
fileSpecBuilder().writeTo(writer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
ksp/sealed/build.gradle
Normal file
7
ksp/sealed/build.gradle
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppJvmJsAndroidLinuxMingwLinuxArm64ProjectPresetPath"
|
21
ksp/sealed/generator/build.gradle
Normal file
21
ksp/sealed/generator/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.jvm"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$publishJvmOnlyPath"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api project(":micro_utils.ksp.generator")
|
||||||
|
api project(":micro_utils.ksp.sealed")
|
||||||
|
api libs.kotlin.poet
|
||||||
|
api libs.ksp
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
}
|
110
ksp/sealed/generator/src/main/kotlin/Processor.kt
Normal file
110
ksp/sealed/generator/src/main/kotlin/Processor.kt
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
package dev.inmo.micro_utils.ksp.sealed.generator
|
||||||
|
|
||||||
|
import com.google.devtools.ksp.KspExperimental
|
||||||
|
import com.google.devtools.ksp.getAnnotationsByType
|
||||||
|
import com.google.devtools.ksp.processing.CodeGenerator
|
||||||
|
import com.google.devtools.ksp.processing.Resolver
|
||||||
|
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||||
|
import com.google.devtools.ksp.symbol.*
|
||||||
|
import com.squareup.kotlinpoet.AnnotationSpec
|
||||||
|
import com.squareup.kotlinpoet.ClassName
|
||||||
|
import com.squareup.kotlinpoet.CodeBlock
|
||||||
|
import com.squareup.kotlinpoet.FileSpec
|
||||||
|
import com.squareup.kotlinpoet.FunSpec
|
||||||
|
import com.squareup.kotlinpoet.KModifier
|
||||||
|
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
||||||
|
import com.squareup.kotlinpoet.PropertySpec
|
||||||
|
import com.squareup.kotlinpoet.asTypeName
|
||||||
|
import com.squareup.kotlinpoet.ksp.toClassName
|
||||||
|
import dev.inmo.micro_ksp.generator.writeFile
|
||||||
|
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround
|
||||||
|
|
||||||
|
class Processor(
|
||||||
|
private val codeGenerator: CodeGenerator
|
||||||
|
) : SymbolProcessor {
|
||||||
|
private fun KSClassDeclaration.resolveSubclasses(): List<KSClassDeclaration> {
|
||||||
|
return (getSealedSubclasses().flatMap {
|
||||||
|
it.resolveSubclasses()
|
||||||
|
}.ifEmpty {
|
||||||
|
sequenceOf(this)
|
||||||
|
}).toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(KspExperimental::class)
|
||||||
|
private fun FileSpec.Builder.generateSealedWorkaround(
|
||||||
|
ksClassDeclaration: KSClassDeclaration,
|
||||||
|
resolver: Resolver
|
||||||
|
) {
|
||||||
|
val subClasses = ksClassDeclaration.resolveSubclasses().distinct()
|
||||||
|
val subClassesNames = subClasses.filter {
|
||||||
|
when (it.classKind) {
|
||||||
|
ClassKind.ENUM_ENTRY,
|
||||||
|
ClassKind.OBJECT -> true
|
||||||
|
ClassKind.INTERFACE,
|
||||||
|
ClassKind.CLASS,
|
||||||
|
ClassKind.ENUM_CLASS,
|
||||||
|
ClassKind.ANNOTATION_CLASS -> false
|
||||||
|
}
|
||||||
|
}.filter {
|
||||||
|
it.getAnnotationsByType(GenerateSealedWorkaround.Exclude::class).count() == 0
|
||||||
|
}.sortedBy {
|
||||||
|
(it.getAnnotationsByType(GenerateSealedWorkaround.Order::class).firstOrNull()) ?.order ?: 0
|
||||||
|
}.map {
|
||||||
|
it.toClassName()
|
||||||
|
}
|
||||||
|
val className = ksClassDeclaration.toClassName()
|
||||||
|
val setType = Set::class.asTypeName().parameterizedBy(
|
||||||
|
ksClassDeclaration.toClassName()
|
||||||
|
)
|
||||||
|
addProperty(
|
||||||
|
PropertySpec.builder(
|
||||||
|
"values",
|
||||||
|
setType
|
||||||
|
).apply {
|
||||||
|
modifiers.add(
|
||||||
|
KModifier.PRIVATE
|
||||||
|
)
|
||||||
|
initializer(
|
||||||
|
CodeBlock.of(
|
||||||
|
"""setOf(${subClassesNames.joinToString(",\n") {it.simpleNames.joinToString(".")}})"""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}.build()
|
||||||
|
)
|
||||||
|
addFunction(
|
||||||
|
FunSpec.builder("values").apply {
|
||||||
|
receiver(ClassName(className.packageName, *className.simpleNames.toTypedArray(), "Companion"))
|
||||||
|
returns(setType)
|
||||||
|
addCode(
|
||||||
|
CodeBlock.of(
|
||||||
|
"""return values"""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}.build()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(KspExperimental::class)
|
||||||
|
override fun process(resolver: Resolver): List<KSAnnotated> {
|
||||||
|
(resolver.getSymbolsWithAnnotation(GenerateSealedWorkaround::class.qualifiedName!!)).filterIsInstance<KSClassDeclaration>().forEach {
|
||||||
|
val prefix = it.getAnnotationsByType(GenerateSealedWorkaround::class).first().prefix
|
||||||
|
it.writeFile(prefix = prefix, suffix = "SealedWorkaround") {
|
||||||
|
FileSpec.builder(
|
||||||
|
it.packageName.asString(),
|
||||||
|
"${it.simpleName.getShortName()}SealedWorkaround"
|
||||||
|
).apply {
|
||||||
|
addFileComment(
|
||||||
|
"""
|
||||||
|
THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
|
||||||
|
TO REGENERATE IT JUST DELETE FILE
|
||||||
|
ORIGINAL FILE: ${it.containingFile ?.fileName}
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
generateSealedWorkaround(it, resolver)
|
||||||
|
}.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return emptyList()
|
||||||
|
}
|
||||||
|
}
|
11
ksp/sealed/generator/src/main/kotlin/Provider.kt
Normal file
11
ksp/sealed/generator/src/main/kotlin/Provider.kt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package dev.inmo.micro_utils.ksp.sealed.generator
|
||||||
|
|
||||||
|
import com.google.devtools.ksp.processing.SymbolProcessor
|
||||||
|
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
|
||||||
|
import com.google.devtools.ksp.processing.SymbolProcessorProvider
|
||||||
|
|
||||||
|
class Provider : SymbolProcessorProvider {
|
||||||
|
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = Processor(
|
||||||
|
environment.codeGenerator
|
||||||
|
)
|
||||||
|
}
|
@@ -0,0 +1 @@
|
|||||||
|
dev.inmo.micro_utils.ksp.sealed.generator.Provider
|
27
ksp/sealed/generator/test/build.gradle
Normal file
27
ksp/sealed/generator/test/build.gradle
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
id "com.android.library"
|
||||||
|
id "com.google.devtools.ksp"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply from: "$mppProjectWithSerializationPresetPath"
|
||||||
|
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
api project(":micro_utils.ksp.sealed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
add("kspCommonMainMetadata", project(":micro_utils.ksp.sealed.generator"))
|
||||||
|
}
|
||||||
|
|
||||||
|
ksp {
|
||||||
|
}
|
16
ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt
Normal file
16
ksp/sealed/generator/test/src/commonMain/kotlin/Test.kt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package dev.inmo.micro_utils.ksp.sealed.generator.test
|
||||||
|
|
||||||
|
import dev.inmo.microutils.kps.sealed.GenerateSealedWorkaround
|
||||||
|
|
||||||
|
@GenerateSealedWorkaround
|
||||||
|
sealed interface Test {
|
||||||
|
@GenerateSealedWorkaround.Order(2)
|
||||||
|
object A : Test
|
||||||
|
@GenerateSealedWorkaround.Exclude
|
||||||
|
object B : Test
|
||||||
|
@GenerateSealedWorkaround.Order(0)
|
||||||
|
object C : Test
|
||||||
|
|
||||||
|
// Required for successful sealed workaround generation
|
||||||
|
companion object
|
||||||
|
}
|
@@ -0,0 +1,11 @@
|
|||||||
|
// THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
|
||||||
|
// TO REGENERATE IT JUST DELETE FILE
|
||||||
|
// ORIGINAL FILE: Test.kt
|
||||||
|
package dev.inmo.micro_utils.ksp.`sealed`.generator.test
|
||||||
|
|
||||||
|
import kotlin.collections.Set
|
||||||
|
|
||||||
|
private val values: Set<Test> = setOf(Test.C,
|
||||||
|
Test.A)
|
||||||
|
|
||||||
|
public fun Test.Companion.values(): Set<Test> = values
|
14
ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt
Normal file
14
ksp/sealed/src/commonMain/kotlin/GenerateSealedWorkaround.kt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package dev.inmo.microutils.kps.sealed
|
||||||
|
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
annotation class GenerateSealedWorkaround(
|
||||||
|
val prefix: String = ""
|
||||||
|
) {
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
annotation class Order(val order: Int)
|
||||||
|
@Retention(AnnotationRetention.BINARY)
|
||||||
|
@Target(AnnotationTarget.CLASS)
|
||||||
|
annotation class Exclude
|
||||||
|
}
|
@@ -41,7 +41,7 @@ class TemporalFilesRoutingConfigurator(
|
|||||||
filesMutex: Mutex,
|
filesMutex: Mutex,
|
||||||
onNewFileFlow: Flow<TemporalFileId>
|
onNewFileFlow: Flow<TemporalFileId>
|
||||||
): Job = scope.launchSafelyWithoutExceptions {
|
): Job = scope.launchSafelyWithoutExceptions {
|
||||||
while (isActive) {
|
while (currentCoroutineContext().isActive) {
|
||||||
val filesWithCreationInfo = filesMap.mapNotNull { (fileId, file) ->
|
val filesWithCreationInfo = filesMap.mapNotNull { (fileId, file) ->
|
||||||
fileId to ((Files.getAttribute(file.toPath(), "creationTime") as? FileTime) ?.toMillis() ?: return@mapNotNull null)
|
fileId to ((Files.getAttribute(file.toPath(), "creationTime") as? FileTime) ?.toMillis() ?: return@mapNotNull null)
|
||||||
}
|
}
|
||||||
|
@@ -49,6 +49,12 @@ String[] includes = [
|
|||||||
":fsm:common",
|
":fsm:common",
|
||||||
":fsm:repos:common",
|
":fsm:repos:common",
|
||||||
|
|
||||||
|
":ksp:generator",
|
||||||
|
|
||||||
|
":ksp:sealed",
|
||||||
|
":ksp:sealed:generator",
|
||||||
|
":ksp:sealed:generator:test",
|
||||||
|
|
||||||
":dokka"
|
":dokka"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user