Public API stabilization: rename packages and introduce internal implementations

This commit is contained in:
Him188 2020-08-22 17:45:39 +08:00
parent ea7751238c
commit 808be4dce1
8 changed files with 99 additions and 45 deletions

View File

@ -25,7 +25,7 @@ import net.mamoe.mirai.console.command.ConsoleCommandSender
import net.mamoe.mirai.console.data.PluginDataStorage import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.internal.plugin.CuiPluginCenter import net.mamoe.mirai.console.internal.plugin.CuiPluginCenter
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
import net.mamoe.mirai.console.internal.utils.ConsoleBuiltInPluginDataStorage import net.mamoe.mirai.console.internal.util.ConsoleBuiltInPluginDataStorage
import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.PluginLoader
import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.center.PluginCenter import net.mamoe.mirai.console.plugin.center.PluginCenter

View File

@ -9,7 +9,7 @@
@file:Suppress("MemberVisibilityCanBePrivate") @file:Suppress("MemberVisibilityCanBePrivate")
package net.mamoe.mirai.console.internal.utils package net.mamoe.mirai.console.internal.util
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job

View File

@ -0,0 +1,65 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package net.mamoe.mirai.console.internal.util
import kotlinx.coroutines.*
import kotlinx.coroutines.future.future
import net.mamoe.mirai.console.util.JavaPluginScheduler
import java.util.concurrent.Callable
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Future
import kotlin.coroutines.CoroutineContext
internal class JavaPluginSchedulerImpl internal constructor(parentCoroutineContext: CoroutineContext) : CoroutineScope,
JavaPluginScheduler {
override val coroutineContext: CoroutineContext =
parentCoroutineContext + SupervisorJob(parentCoroutineContext[Job])
override fun repeating(intervalMs: Long, runnable: Runnable): Future<Void?> {
return this.future {
while (isActive) {
withContext(Dispatchers.IO) { runnable.run() }
delay(intervalMs)
}
null
}
}
override fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture<Void?> {
return future {
delay(delayMillis)
withContext(Dispatchers.IO) {
runnable.run()
}
null
}
}
override fun <R> delayed(delayMillis: Long, runnable: Callable<R>): CompletableFuture<Void?> {
return future {
delay(delayMillis)
withContext(Dispatchers.IO) { runnable.call() }
null
}
}
override fun <R> async(supplier: Callable<R>): Future<R> {
return future {
withContext(Dispatchers.IO) { supplier.call() }
}
}
override fun async(runnable: Runnable): Future<Void?> {
return future {
withContext(Dispatchers.IO) { runnable.run() }
null
}
}
}

View File

@ -7,6 +7,9 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("ConsoleUtils")
package net.mamoe.mirai.console.util package net.mamoe.mirai.console.util
import kotlin.annotation.AnnotationTarget.* import kotlin.annotation.AnnotationTarget.*

View File

@ -8,11 +8,13 @@
*/ */
@file:Suppress("NOTHING_TO_INLINE") @file:Suppress("NOTHING_TO_INLINE")
@file:JvmMultifileClass
@file:JvmName("ConsoleUtils")
package net.mamoe.mirai.console.util package net.mamoe.mirai.console.util
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.internal.utils.BotManagerImpl import net.mamoe.mirai.console.internal.util.BotManagerImpl
import net.mamoe.mirai.contact.User import net.mamoe.mirai.contact.User
public interface BotManager { public interface BotManager {

View File

@ -8,6 +8,8 @@
*/ */
@file:Suppress("INAPPLICABLE_JVM_NAME", "unused") @file:Suppress("INAPPLICABLE_JVM_NAME", "unused")
@file:JvmMultifileClass
@file:JvmName("ConsoleUtils")
package net.mamoe.mirai.console.util package net.mamoe.mirai.console.util

View File

@ -7,10 +7,14 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("ConsoleUtils")
package net.mamoe.mirai.console.util package net.mamoe.mirai.console.util
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.future.future import kotlinx.coroutines.Runnable
import net.mamoe.mirai.console.internal.util.JavaPluginSchedulerImpl
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin import net.mamoe.mirai.console.plugin.jvm.JavaPlugin
import java.util.concurrent.Callable import java.util.concurrent.Callable
import java.util.concurrent.CompletableFuture import java.util.concurrent.CompletableFuture
@ -19,16 +23,13 @@ import kotlin.coroutines.CoroutineContext
/** /**
* 拥有生命周期管理的 Java 线程池. * 拥有生命周期管理的简单 Java 线程池.
* *
* 在插件被 [卸载][JavaPlugin.onDisable] 时将会自动停止. * 在插件被 [卸载][JavaPlugin.onDisable] 时将会自动停止.
* *
* @see JavaPlugin.scheduler 获取实例 * @see JavaPlugin.scheduler 获取实例
*/ */
public class JavaPluginScheduler internal constructor(parentCoroutineContext: CoroutineContext) : CoroutineScope { public interface JavaPluginScheduler : CoroutineScope {
public override val coroutineContext: CoroutineContext =
parentCoroutineContext + SupervisorJob(parentCoroutineContext[Job])
/** /**
* 新增一个 Repeating Task (定时任务) * 新增一个 Repeating Task (定时任务)
* *
@ -36,60 +37,39 @@ public class JavaPluginScheduler internal constructor(parentCoroutineContext: Co
* *
* @see Future.cancel 取消这个任务 * @see Future.cancel 取消这个任务
*/ */
public fun repeating(intervalMs: Long, runnable: Runnable): Future<Void?> { public fun repeating(intervalMs: Long, runnable: Runnable): Future<Void?>
return this.future {
while (isActive) {
withContext(Dispatchers.IO) { runnable.run() }
delay(intervalMs)
}
null
}
}
/** /**
* 新增一个 Delayed Task (延迟任务) * 新增一个 Delayed Task (延迟任务)
* *
* 在延迟 [delayMillis] 后执行 [runnable] * 在延迟 [delayMillis] 后执行 [runnable]
*/ */
public fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture<Void?> { public fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture<Void?>
return future {
delay(delayMillis)
withContext(Dispatchers.IO) {
runnable.run()
}
null
}
}
/** /**
* 新增一个 Delayed Task (延迟任务) * 新增一个 Delayed Task (延迟任务)
* *
* 在延迟 [delayMillis] 后执行 [runnable] * 在延迟 [delayMillis] 后执行 [runnable]
*/ */
public fun <R> delayed(delayMillis: Long, runnable: Callable<R>): CompletableFuture<Void?> { public fun <R> delayed(delayMillis: Long, runnable: Callable<R>): CompletableFuture<Void?>
return future {
delay(delayMillis)
withContext(Dispatchers.IO) { runnable.call() }
null
}
}
/** /**
* 异步执行一个任务, 最终返回 [Future], Java 使用方法无异, 但效率更高且可以在插件关闭时停止 * 异步执行一个任务, 最终返回 [Future], Java 使用方法无异, 但效率更高且可以在插件关闭时停止
*/ */
public fun <R> async(supplier: Callable<R>): Future<R> { public fun <R> async(supplier: Callable<R>): Future<R>
return future {
withContext(Dispatchers.IO) { supplier.call() }
}
}
/** /**
* 异步执行一个任务, 没有返回 * 异步执行一个任务, 没有返回
*/ */
public fun async(runnable: Runnable): Future<Void?> { public fun async(runnable: Runnable): Future<Void?>
return future {
withContext(Dispatchers.IO) { runnable.run() } public companion object {
null /**
} * 创建一个 [JavaPluginScheduler]
*/
@JvmStatic
@JvmName("create")
public operator fun invoke(parentCoroutineContext: CoroutineContext): JavaPluginScheduler =
JavaPluginSchedulerImpl(parentCoroutineContext)
} }
} }

View File

@ -8,6 +8,8 @@
*/ */
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@file:JvmMultifileClass
@file:JvmName("ConsoleUtils")
package net.mamoe.mirai.console.util package net.mamoe.mirai.console.util