DelayableCoroutineScope

interface DelayableCoroutineScope : CoroutineScope

提供异步可延迟API的 CoroutineScopeDelayableCoroutineScope 继承并扩展 CoroutineScope, 对外提供部分服务于 Java 开发者的异步延迟函数。

对外提供的这些延时函数本质上是通过当前作用域所构建的异步函数,并通过 delay 实现非阻塞的延时效果。 Java 开发者可以通过这些延时函数来更高效的通过异步回调手段实现类似于 Thread.sleep 的效果。

需要注意的是,本质上这些延迟任务都是 异步 的,所以需要依靠回调函数进行进一步的逻辑。 它们继承 CompletableFuture , 额外的延迟api也具有与 CompletableFuture 相仿的使用方式.

Java 开发者可以通过链式风格使用这些延时函数:

public void foo(Bot bot) { // Bot 间接实现了 DelayableCoroutineScope
DelayableCompletableFuture<LocalTime> whole = bot
// (1). 延时5秒,打印当前时间
.delay(Duration.ofSeconds(5), () -> {
System.out.println(LocalTime.now());
})
// (2). 流程(1)结束后,再延时5秒,打印当前时间
.delay(Duration.ofSeconds(5), () -> {
System.out.println(LocalTime.now());
})
// (3). 流程(2)结束后,再延时5秒,返回当前时间
.delayAndCompute(Duration.ofSeconds(5), (v) -> LocalTime.now());
}

上述示例中,函数内通过 bot 总共创建了3个延时函数,他们将在 异步 中按照顺序分别延时5秒。 内部异步的延时任务会在创建时立刻执行。示例中的 foo 函数会立刻返回,而延时任务不会收到影响。

当然,延时任务的最后会返回 DelayableCompletableFuture, 它实现 Future, 你可以通过此 future 获取整体延时任务的最终结果, 或者控制延时任务的流程。

如果流程中某个节点出现了异常,则后续延时任务会受到上游任务的影响而无法抵达。

Author

ForteScarlet

See also

Functions

Link copied to clipboard
open fun delay(duration: JavaDuration, runnable: Runnable): DelayableCompletableFuture<Void?>

延时 duration 时间后执行回调函数 runnable,得到一个 DelayableCompletableFuture

open fun delay(millis: Long, runnable: Runnable): DelayableCompletableFuture<Void?>

延迟 millis 毫秒后执行 runnable,得到一个 DelayableCompletableFuture

open fun delay(time: Long, timeUnit: TimeUnit, runnable: Runnable): DelayableCompletableFuture<Void?>

延迟时间单位为 timeUnittime 时长后执行 runnable,得到一个 DelayableCompletableFuture

Link copied to clipboard

延迟 duration 时间后,执行 supplier, 并将 supplier 得到结果通过得到的 DelayableCompletableFuture 向下传递。

open fun <V> delayAndCompute(millis: Long, supplier: Supplier<V>): DelayableCompletableFuture<V>

延迟 millis 毫秒的时长后执行 supplier, 并将 supplier 得到结果通过得到的 DelayableCompletableFuture 向下传递。

open fun <V> delayAndCompute(time: Long, timeUnit: TimeUnit, supplier: Supplier<V>): DelayableCompletableFuture<V>

延迟时间单位为 timeUnittime 时长后执行 supplier, 并将 supplier 得到结果通过得到的 DelayableCompletableFuture 向下传递。

Properties

Link copied to clipboard

Inheritors

Link copied to clipboard

Extensions

Link copied to clipboard
fun <A : Application> CoroutineScope.applicationLauncher(create: suspend () -> A): ApplicationLauncher<A>

通过一个协程作用域并提供构建函数来得到一个 ApplicationLauncher 实例。

Link copied to clipboard
inline fun <T> CoroutineScope.effectedFlowItems(crossinline block: suspend FlowCollector<T>.() -> Unit): Items<T>

以构建 Flow 的方式构建 Items.

Link copied to clipboard
inline fun <T> CoroutineScope.effectedItemsByFlow(crossinline flowFactory: () -> Flow<T>): Items<T>

提供 CoroutineScope 和构建 Flow 的函数 flowFactory 来构建 Items.

Link copied to clipboard
inline fun <T> CoroutineScope.flowItems(crossinline block: suspend FlowCollector<T>.(Items.PreprocessingProperties) -> Unit): Items<T>

以构建 Flow 的方式构建 Items.

Link copied to clipboard
inline fun <T> CoroutineScope.itemsByFlow(crossinline flowFactory: (Items.PreprocessingProperties) -> Flow<T>): Items<T>

提供 CoroutineScope 和构建 Flow 的函数 flowFactory 来构建 Items.

Link copied to clipboard
fun <T> CoroutineScope.lazyValue(init: Boolean = false, initializer: suspend () -> T): LazyValue<T>
Link copied to clipboard
inline fun <T> CoroutineScope.produceItems(context: CoroutineContext = EmptyCoroutineContext, capacity: Int = 0, crossinline block: suspend ProducerScope<T>.(Items.PreprocessingProperties) -> Unit): Items<T>

通过 produce 构建 ChannelIterator 来得到一个 Items 实例。