Add FrontendLoggingInitContext.invokeAfterInitialization

This commit is contained in:
Karlatemp 2022-08-31 22:39:02 +08:00
parent 637fe1a5b9
commit a94c98d990
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59
2 changed files with 11 additions and 0 deletions

View File

@ -409,6 +409,11 @@ public interface MiraiConsoleImplementation : CoroutineScope {
* 调用此函数会立即初始化平台日志实现. 在未完成准备工作前切勿使用此方法
*/
public fun acquirePlatformImplementation(): MiraiLogger.Factory
/**
* 在完成 [MiraiLogger.Factory] 接管后马上执行 [action]
*/
public fun invokeAfterInitialization(action: () -> Unit)
}
///////////////////////////////////////////////////////////////////////////

View File

@ -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() }
}