Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alvin0319/scheduler-coroutine
https://github.com/alvin0319/scheduler-coroutine
Last synced: 16 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/alvin0319/scheduler-coroutine
- Owner: alvin0319
- License: gpl-3.0
- Created: 2023-07-23T03:51:54.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-07-23T03:52:11.000Z (over 1 year ago)
- Last Synced: 2024-11-01T13:42:00.672Z (2 months ago)
- Language: Kotlin
- Size: 74.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scheduler-coroutine
Coroutine for Nukkit ported from [heartbeat-coroutines](https://github.com/monun/heartbeat-coroutines/tree/master)
---
* Features
* Coroutine which dispatch()ed from Nukkit's main heartbeat
* CoroutineScope in PluginBase lifecycle
* Flexible delay process---
Here's some example which process this code without asynchronous library:1. Broadcast cooldown message every 1 second for 3 seconds
2. Damage all entities every 1 second for every 5 seconds
3. Broadcast `Surprise~` message and shutdown the server#### Thread
```
Runnable {
repeat(3) {
Server.getInstance().scheduler.scheduleTask(this) {
broadcast(3 - it)
Thread.sleep(1000L)
}
}
repeat(5) {
Server.getInstance().scheduler.scheduleTask(this) {
damageAll()
}
Thread.sleep(1000L)
}
Server.getInstance().scheduler.scheduleTask(this) {
broadcast("surprise~")
}
}.let {
Thread(it).start()
}
```#### Callback
```kotlin
// somewhat method which process these code in async
async({
repeat(3) {
Server.getInstance().scheduler.scheduleTask(this) {
broadcast(3 - it)
Thread.sleep(1000L)
}
}
}) {
async({
repeat(5) {
Server.getInstance().scheduler.scheduleTask(this) {
damageAll()
}
Thread.sleep(1000L)
}
}) {
async {
Server.getInstance().scheduler.scheduleTask(this) {
broadcast("surprise~")
}
}
}
}
```As routines become more complex, asynchrony issues and complexity reduce flexibility and increase exponentially in
maintenance difficulty.By using coroutines, you can drastically reduce the complexity of chaining code in scheduler.
The example below is coroutine code that runs synchronously inside a scheduler.
#### Coroutine
```kotlin
CoroutineScope(Dispatchers.Scheduler).launch {
repeat(3) {
broadcast(3 - it)
delay(1000L)
}
repeat(5) {
damageAll()
delay(1000L)
}
broadcast("surprise~")
}
```It's simple, isn't it?
Please refer to [this document](https://kotlinlang.org/docs/coroutines-overview.html) to learn more about coroutine.
# Download & Installation
```kotlin
repositories {
maven {
name = "minjae-repo-snapshot"
url = uri("https://repo.minjae.dev/snapshots")
}
}dependencies {
implementation("dev.minjae.pnx.coroutine:scheduler-coroutine:1.0-SNAPSHOT")
}
```