forked from TabooLib/taboolib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# expansion-submit-chain | ||
|
||
对 Kotlin `Coroutine API` 的封装。 | ||
|
||
## 范例代码 | ||
|
||
```kotlin | ||
submitChain { | ||
// 等待 10 ticks | ||
wait(10) | ||
// 同步执行代码 | ||
sync { | ||
sender.sendMessage("Hello from Sync!") | ||
} | ||
wait(20) | ||
// 异步执行代码 | ||
async { | ||
sender.sendMessage("Hello from Async!") | ||
} | ||
wait(5) | ||
// 允许求值 | ||
val value = async { | ||
1 + 2 + 3 | ||
} | ||
sync { | ||
sender.sendMessage("Value: $value") | ||
} | ||
wait(100) | ||
var index = 0 | ||
// 重复执行的同步代码,每 20 ticks 执行一次 | ||
val context = sync(period = 20L, delay = 0L) { | ||
index += 1 | ||
sender.sendMessage("Sync: $index") | ||
if (index == 10) { | ||
sender.sendMessage("&cSync task cancelled.".colored()) | ||
cancel() | ||
"END" | ||
} else { | ||
// 由于任务没有被 cancel,所以这里的返回值不会应用在 context 上 | ||
"_" | ||
} | ||
} | ||
sync { | ||
sender.sendMessage("Sync: $context") | ||
// "Sync: END" | ||
} | ||
} | ||
``` |