1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-30 14:08:44 +00:00

fixes and improvements

This commit is contained in:
InsanusMokrassar 2022-12-28 22:48:49 +06:00
parent 6db6209c88
commit ee055873e6

View File

@ -169,11 +169,22 @@ class DefaultBehaviourContextWithFSM<T : State>(
statesManager.endChain(state) statesManager.endChain(state)
} }
} }
fun Job.enableRemoveOnCompletion(state: T) {
invokeOnCompletion {
launchSafelyWithoutExceptions {
if (this@enableRemoveOnCompletion === statesJobs[state]) {
statesJobs.remove(state)
}
}
}
}
statesManager.onStartChain.subscribeSafelyWithoutExceptions(this) { statesManager.onStartChain.subscribeSafelyWithoutExceptions(this) {
statesJobsMutex.withLock { statesJobsMutex.withLock {
runCatchingSafely { statesJobs.remove(it) ?.cancel() } runCatchingSafely { statesJobs.remove(it) ?.cancel() }
statesJobs[it] = launch { statePerformer(it) } statesJobs[it] = launch { statePerformer(it) }.apply { enableRemoveOnCompletion(it) }
} }
} }
statesManager.onEndChain.subscribeSafelyWithoutExceptions(this) { statesManager.onEndChain.subscribeSafelyWithoutExceptions(this) {
@ -186,7 +197,7 @@ class DefaultBehaviourContextWithFSM<T : State>(
statesJobsMutex.withLock { statesJobsMutex.withLock {
runCatchingSafely { statesJobs.remove(old) ?.cancel() } runCatchingSafely { statesJobs.remove(old) ?.cancel() }
runCatchingSafely { statesJobs.remove(new) ?.cancel() } runCatchingSafely { statesJobs.remove(new) ?.cancel() }
statesJobs[new] = launch { statePerformer(new) } statesJobs[new] = launch { statePerformer(new) }.apply { enableRemoveOnCompletion(it) }
} }
if (old.context != new.context) { if (old.context != new.context) {
updatesFlows.remove(old.context) updatesFlows.remove(old.context)
@ -194,7 +205,11 @@ class DefaultBehaviourContextWithFSM<T : State>(
} }
statesManager.getActiveStates().forEach { statesManager.getActiveStates().forEach {
launch { statePerformer(it) } statesJobsMutex.withLock {
runCatchingSafely { statesJobs.remove(it) ?.cancel() }
statesJobs[it] = launch { statePerformer(it) }.apply { enableRemoveOnCompletion(it) }
}
} }
} }
/** /**