From 30cf190461ac228d7d1c6f65c795a1360be25545 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 1 Oct 2023 15:28:00 +0600 Subject: [PATCH] conflicts resolvers customization --- CHANGELOG.md | 3 +++ bot/build.gradle | 4 ++++ .../OnStartContextsConflictResolver.kt | 16 +++++++++++++++ .../OnUpdateContextsConflictResolver.kt | 20 +++++++++++++++++++ .../main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 6 +++++- build.gradle | 1 + gradle/libs.versions.toml | 5 +++++ 7 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 bot/src/main/kotlin/dev/inmo/plagubot/OnStartContextsConflictResolver.kt create mode 100644 bot/src/main/kotlin/dev/inmo/plagubot/OnUpdateContextsConflictResolver.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 793cc27..ebf761a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 7.2.2 +* `Bot`: + * Now you may customize both `onStart` and `onUpdate` conflicts resolvers + ## 7.2.1 * `Versions`: diff --git a/bot/build.gradle b/bot/build.gradle index 473692a..16b40fb 100644 --- a/bot/build.gradle +++ b/bot/build.gradle @@ -2,6 +2,7 @@ plugins { id 'org.jetbrains.kotlin.jvm' id "org.jetbrains.kotlin.plugin.serialization" id 'application' + id "com.google.devtools.ksp" } project.group="$group" @@ -36,3 +37,6 @@ java { targetCompatibility = JavaVersion.VERSION_1_8 } +ksp { // this generator do not require any arguments and we should left `ksp` empty +} + diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/OnStartContextsConflictResolver.kt b/bot/src/main/kotlin/dev/inmo/plagubot/OnStartContextsConflictResolver.kt new file mode 100644 index 0000000..153120a --- /dev/null +++ b/bot/src/main/kotlin/dev/inmo/plagubot/OnStartContextsConflictResolver.kt @@ -0,0 +1,16 @@ +package dev.inmo.plagubot + +import dev.inmo.micro_utils.fsm.common.State + +fun interface OnStartContextsConflictResolver { + /** + * @param old Old state which is currently placed on the [State.context] + * @param new New state pretend to replace [old] one + * @return Should return: + * + * * Null in case when current realization unable to resolve conflict + * * False when current realization knows that [new] [State] must **not** replace [old] one + * * True when current realization knows that [new] [State] must replace [old] one + */ + suspend operator fun invoke(old: State, new: State): Boolean? +} \ No newline at end of file diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/OnUpdateContextsConflictResolver.kt b/bot/src/main/kotlin/dev/inmo/plagubot/OnUpdateContextsConflictResolver.kt new file mode 100644 index 0000000..a408c89 --- /dev/null +++ b/bot/src/main/kotlin/dev/inmo/plagubot/OnUpdateContextsConflictResolver.kt @@ -0,0 +1,20 @@ +package dev.inmo.plagubot + +import dev.inmo.micro_utils.fsm.common.State + +fun interface OnUpdateContextsConflictResolver { + /** + * This method will be called when [sourceStateWithOldContext] [State.context] and [newStateWithNewContext] are not equal and currently there is + * launched [currentStateOnNewContext] state on the chain with [State.context] from [currentStateOnNewContext] + * + * @param sourceStateWithOldContext Old state where from [newStateWithNewContext] came + * @param newStateWithNewContext New state with changing [State.context] (it is different with [sourceStateWithOldContext] [State.context]) + * @param currentStateOnNewContext State which is currently running on [newStateWithNewContext] [State.context] + * @return Should return: + * + * * Null in case when current realization unable to resolve conflict + * * False when [currentStateOnNewContext] **should not** be stopped in favor to [newStateWithNewContext] + * * True when [currentStateOnNewContext] **should** be stopped in favor to [newStateWithNewContext] + */ + suspend operator fun invoke(sourceStateWithOldContext: State, newStateWithNewContext: State, currentStateOnNewContext: State): Boolean? +} \ No newline at end of file diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt index 4b0cf7c..c8ed0b6 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -6,6 +6,7 @@ import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.micro_utils.fsm.common.State import dev.inmo.micro_utils.fsm.common.StatesManager import dev.inmo.micro_utils.fsm.common.managers.* +import dev.inmo.micro_utils.koin.getAllDistinct import dev.inmo.plagubot.config.* import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.extensions.api.webhook.deleteWebhook @@ -94,6 +95,8 @@ data class PlaguBot( GlobalContext.startKoin(koinApp) logger.i("Koin started") lateinit var behaviourContext: BehaviourContext + val onStartContextsConflictResolver = koinApp.koin.getAllDistinct() + val onUpdateContextsConflictResolver = koinApp.koin.getAllDistinct() bot.buildBehaviourWithFSM( scope = scope, defaultExceptionsHandler = { @@ -101,7 +104,8 @@ data class PlaguBot( }, statesManager = koinApp.koin.getOrNull>() ?: DefaultStatesManager( koinApp.koin.getOrNull>() ?: InMemoryDefaultStatesManagerRepo(), - onStartContextsConflictResolver = { _, _ -> false } + onStartContextsConflictResolver = { old, new -> onStartContextsConflictResolver.firstNotNullOfOrNull { it(old, new) } ?: false }, + onUpdateContextsConflictResolver = { old, new, currentNew -> onUpdateContextsConflictResolver.firstNotNullOfOrNull { it(old, new, currentNew) } ?: false } ), onStateHandlingErrorHandler = koinApp.koin.getOrNull>() ?: { state, e -> logger.eS(e) { "Unable to handle state $state" } diff --git a/build.gradle b/build.gradle index 8639909..c5dd810 100644 --- a/build.gradle +++ b/build.gradle @@ -8,6 +8,7 @@ buildscript { classpath libs.buildscript.kt.gradle classpath libs.buildscript.kt.serialization classpath libs.buildscript.gh.release + classpath libs.buildscript.ksp } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0c08c2c..8fb356d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,6 +8,8 @@ microutils = "0.19.9" tgbotapi = "9.2.1" kslog = "1.1.2" +ksp = "1.8.22-1.0.11" + jb-exposed = "0.41.1" jb-dokka = "1.8.20" @@ -32,6 +34,8 @@ kt-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json" tgbotapi = { module = "dev.inmo:tgbotapi", version.ref = "tgbotapi" } tgbotapi-behaviourBuilder-fsm = { module = "dev.inmo:tgbotapi.behaviour_builder.fsm", version.ref = "tgbotapi" } microutils-repos-exposed = { module = "dev.inmo:micro_utils.repos.exposed", version.ref = "microutils" } +microutils-koin = { module = "dev.inmo:micro_utils.koin", version.ref = "microutils" } +microutils-koin-generator = { module = "dev.inmo:micro_utils.koin.generator", version.ref = "microutils" } kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } koin = { module = "io.insert-koin:koin-core", version.ref = "koin" } @@ -48,3 +52,4 @@ buildscript-kt-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", buildscript-kt-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kt" } buildscript-jb-dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "jb-dokka" } buildscript-gh-release = { module = "com.github.breadmoirai:github-release", version.ref = "gh-release" } +buildscript-ksp = { module = "com.google.devtools.ksp:symbol-processing-gradle-plugin", version.ref = "ksp" }