From a94c98d990e5fb669996aa0aa20077807329fbbd Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Wed, 31 Aug 2022 22:39:02 +0800 Subject: [PATCH] Add `FrontendLoggingInitContext.invokeAfterInitialization` --- .../backend/mirai-console/src/MiraiConsoleImplementation.kt | 5 +++++ .../src/internal/MiraiConsoleImplementationBridge.kt | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt b/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt index 1fc0d090f..f228658e2 100644 --- a/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt +++ b/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt @@ -409,6 +409,11 @@ public interface MiraiConsoleImplementation : CoroutineScope { * 调用此函数会立即初始化平台日志实现. 在未完成准备工作前切勿使用此方法 */ public fun acquirePlatformImplementation(): MiraiLogger.Factory + + /** + * 在完成 [MiraiLogger.Factory] 接管后马上执行 [action] + */ + public fun invokeAfterInitialization(action: () -> Unit) } /////////////////////////////////////////////////////////////////////////// diff --git a/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 6f042957a..eefc68b99 100644 --- a/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -129,10 +129,15 @@ internal class MiraiConsoleImplementationBridge( // 3. [externalImplementation] decides how to log the message // 4. [externalImplementation] outputs by using [platform] val context = object : MiraiConsoleImplementation.FrontendLoggingInitContext { + val pendingActions = mutableListOf<() -> Unit>() override fun acquirePlatformImplementation(): MiraiLogger.Factory { @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") return MiraiLoggerFactoryImplementationBridge.instance } + + override fun invokeAfterInitialization(action: () -> Unit) { + pendingActions.add(action) + } } val response = externalImplementation.createLoggerFactory(context) @@ -142,6 +147,7 @@ internal class MiraiConsoleImplementationBridge( @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") MiraiLoggerFactoryImplementationBridge.freeze() // forbid any further overrides + context.pendingActions.forEach { it.invoke() } }