mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Public API stabilization: rename packages and introduce internal implementations
This commit is contained in:
parent
ea7751238c
commit
808be4dce1
@ -25,7 +25,7 @@ import net.mamoe.mirai.console.command.ConsoleCommandSender
|
||||
import net.mamoe.mirai.console.data.PluginDataStorage
|
||||
import net.mamoe.mirai.console.internal.plugin.CuiPluginCenter
|
||||
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.PluginManager
|
||||
import net.mamoe.mirai.console.plugin.center.PluginCenter
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
@file:Suppress("MemberVisibilityCanBePrivate")
|
||||
|
||||
package net.mamoe.mirai.console.internal.utils
|
||||
package net.mamoe.mirai.console.internal.util
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,9 @@
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("ConsoleUtils")
|
||||
|
||||
package net.mamoe.mirai.console.util
|
||||
|
||||
import kotlin.annotation.AnnotationTarget.*
|
||||
|
@ -8,11 +8,13 @@
|
||||
*/
|
||||
|
||||
@file:Suppress("NOTHING_TO_INLINE")
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("ConsoleUtils")
|
||||
|
||||
package net.mamoe.mirai.console.util
|
||||
|
||||
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
|
||||
|
||||
public interface BotManager {
|
||||
|
@ -8,6 +8,8 @@
|
||||
*/
|
||||
|
||||
@file:Suppress("INAPPLICABLE_JVM_NAME", "unused")
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("ConsoleUtils")
|
||||
|
||||
package net.mamoe.mirai.console.util
|
||||
|
||||
|
@ -7,10 +7,14 @@
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("ConsoleUtils")
|
||||
|
||||
package net.mamoe.mirai.console.util
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.future.future
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Runnable
|
||||
import net.mamoe.mirai.console.internal.util.JavaPluginSchedulerImpl
|
||||
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin
|
||||
import java.util.concurrent.Callable
|
||||
import java.util.concurrent.CompletableFuture
|
||||
@ -19,16 +23,13 @@ import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
||||
/**
|
||||
* 拥有生命周期管理的 Java 线程池.
|
||||
* 拥有生命周期管理的简单 Java 线程池.
|
||||
*
|
||||
* 在插件被 [卸载][JavaPlugin.onDisable] 时将会自动停止.
|
||||
*
|
||||
* @see JavaPlugin.scheduler 获取实例
|
||||
*/
|
||||
public class JavaPluginScheduler internal constructor(parentCoroutineContext: CoroutineContext) : CoroutineScope {
|
||||
public override val coroutineContext: CoroutineContext =
|
||||
parentCoroutineContext + SupervisorJob(parentCoroutineContext[Job])
|
||||
|
||||
public interface JavaPluginScheduler : CoroutineScope {
|
||||
/**
|
||||
* 新增一个 Repeating Task (定时任务)
|
||||
*
|
||||
@ -36,60 +37,39 @@ public class JavaPluginScheduler internal constructor(parentCoroutineContext: Co
|
||||
*
|
||||
* @see Future.cancel 取消这个任务
|
||||
*/
|
||||
public fun repeating(intervalMs: Long, runnable: Runnable): Future<Void?> {
|
||||
return this.future {
|
||||
while (isActive) {
|
||||
withContext(Dispatchers.IO) { runnable.run() }
|
||||
delay(intervalMs)
|
||||
}
|
||||
null
|
||||
}
|
||||
}
|
||||
public fun repeating(intervalMs: Long, runnable: Runnable): Future<Void?>
|
||||
|
||||
/**
|
||||
* 新增一个 Delayed Task (延迟任务)
|
||||
*
|
||||
* 在延迟 [delayMillis] 后执行 [runnable]
|
||||
*/
|
||||
public fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture<Void?> {
|
||||
return future {
|
||||
delay(delayMillis)
|
||||
withContext(Dispatchers.IO) {
|
||||
runnable.run()
|
||||
}
|
||||
null
|
||||
}
|
||||
}
|
||||
public fun delayed(delayMillis: Long, runnable: Runnable): CompletableFuture<Void?>
|
||||
|
||||
/**
|
||||
* 新增一个 Delayed Task (延迟任务)
|
||||
*
|
||||
* 在延迟 [delayMillis] 后执行 [runnable]
|
||||
*/
|
||||
public fun <R> delayed(delayMillis: Long, runnable: Callable<R>): CompletableFuture<Void?> {
|
||||
return future {
|
||||
delay(delayMillis)
|
||||
withContext(Dispatchers.IO) { runnable.call() }
|
||||
null
|
||||
}
|
||||
}
|
||||
public fun <R> delayed(delayMillis: Long, runnable: Callable<R>): CompletableFuture<Void?>
|
||||
|
||||
/**
|
||||
* 异步执行一个任务, 最终返回 [Future], 与 Java 使用方法无异, 但效率更高且可以在插件关闭时停止
|
||||
*/
|
||||
public fun <R> async(supplier: Callable<R>): Future<R> {
|
||||
return future {
|
||||
withContext(Dispatchers.IO) { supplier.call() }
|
||||
}
|
||||
}
|
||||
public fun <R> async(supplier: Callable<R>): Future<R>
|
||||
|
||||
/**
|
||||
* 异步执行一个任务, 没有返回
|
||||
*/
|
||||
public fun async(runnable: Runnable): Future<Void?> {
|
||||
return future {
|
||||
withContext(Dispatchers.IO) { runnable.run() }
|
||||
null
|
||||
}
|
||||
public fun async(runnable: Runnable): Future<Void?>
|
||||
|
||||
public companion object {
|
||||
/**
|
||||
* 创建一个 [JavaPluginScheduler]
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmName("create")
|
||||
public operator fun invoke(parentCoroutineContext: CoroutineContext): JavaPluginScheduler =
|
||||
JavaPluginSchedulerImpl(parentCoroutineContext)
|
||||
}
|
||||
}
|
@ -8,6 +8,8 @@
|
||||
*/
|
||||
|
||||
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
|
||||
@file:JvmMultifileClass
|
||||
@file:JvmName("ConsoleUtils")
|
||||
|
||||
package net.mamoe.mirai.console.util
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user