Skip to content

Commit

Permalink
Merge pull request #2 from okkero/v-kotlin11m04
Browse files Browse the repository at this point in the history
version 1.1.0
  • Loading branch information
okkero authored Jan 14, 2017
2 parents e636ed1 + 5644c7c commit cf1313f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ More information here:
https://blog.jetbrains.com/kotlin/2016/07/first-glimpse-of-kotlin-1-1-coroutines-type-aliases-and-more/
https://blog.jetbrains.com/kotlin/2016/10/kotlin-1-1-m02-is-here/
https://blog.jetbrains.com/kotlin/2016/11/kotlin-1-1-m03-is-here/

https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-1-m04-is-here/
# Skedule
A small coroutine library for the BukkitScheduler for Bukkit/Spigot plugin developers using Kotlin

Expand Down Expand Up @@ -125,7 +125,7 @@ scheduler.schedule(plugin, SynchronizationContext.ASYNC) { //ASYNC here specifie
<repositories>
<repository>
<id>okkero</id>
<url>http://nexus.elyc.in/repository/maven-releases/</url>
<url>http://nexus.okkero.com/repository/maven-releases/</url>
</repository>
</repositories>
```
Expand All @@ -134,7 +134,7 @@ scheduler.schedule(plugin, SynchronizationContext.ASYNC) { //ASYNC here specifie
<dependency>
<groupId>com.okkero.skedule</groupId>
<artifactId>skedule</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

<groupId>com.okkero.skedule</groupId>
<artifactId>skedule</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>Skedule</name>

<distributionManagement>
<repository>
<id>deployment-elycin</id>
<url>http://d.elyc.in:8081/repository/maven-releases/</url>
<url>http://nexus.okkero.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>deployment-elycin</id>
<url>http://d.elyc.in:8081/repository/maven-snapshots/</url>
<url>http://nexus.okkero.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.1-M01</kotlin.version>
<kotlin.version>1.1-M04</kotlin.version>
<junit.version>4.12</junit.version>
<dokka.version>0.9.9</dokka.version>
</properties>
Expand Down
28 changes: 17 additions & 11 deletions src/main/kotlin/com/okkero/skedule/SchedulerCoroutine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import org.bukkit.Bukkit
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitScheduler
import org.bukkit.scheduler.BukkitTask
import kotlin.coroutines.Continuation
import kotlin.coroutines.RestrictsSuspension
import kotlin.coroutines.createCoroutine
import kotlin.coroutines.suspendCoroutine

/**
* Schedule a coroutine with the Bukkit Scheduler.
Expand All @@ -17,9 +21,10 @@ import org.bukkit.scheduler.BukkitTask
* @see SynchronizationContext
*/
fun BukkitScheduler.schedule(plugin: Plugin, initialContext: SynchronizationContext = SYNC,
coroutine co: BukkitSchedulerController.() -> Continuation<Unit>): CoroutineTask {
co: suspend BukkitSchedulerController.() -> Unit): CoroutineTask {
val controller = BukkitSchedulerController(plugin, this)
controller.start(initialContext, controller.co())
val coroutine = co.createCoroutine(controller, completion = controller)
controller.start(initialContext, coroutine)

return CoroutineTask(controller)
}
Expand All @@ -33,7 +38,8 @@ fun BukkitScheduler.schedule(plugin: Plugin, initialContext: SynchronizationCont
* @property isRepeating whether this coroutine is currently backed by a repeating task
*/
//TODO Verify if thread safe
class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitScheduler) {
@RestrictsSuspension
class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitScheduler) : Continuation<Unit> {

private var schedulerDelegate: TaskScheduler = NonRepeatingTaskScheduler(plugin, scheduler)

Expand All @@ -58,7 +64,7 @@ class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitSchedul
*
* @return the actual amount of ticks waited
*/
suspend fun waitFor(ticks: Long, cont: Continuation<Long>) {
suspend fun waitFor(ticks: Long): Long = suspendCoroutine { cont ->
schedulerDelegate.doWait(ticks, cont::resume)
}

Expand All @@ -70,7 +76,7 @@ class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitSchedul
*
* @return the actual amount of ticks waited
*/
suspend fun yield(cont: Continuation<Long>) {
suspend fun yield(): Long = suspendCoroutine { cont ->
schedulerDelegate.doYield(cont::resume)
}

Expand All @@ -81,7 +87,7 @@ class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitSchedul
* @param context the context to switch to
* @return `true` if a context switch was made, `false` otherwise
*/
suspend fun switchContext(context: SynchronizationContext, cont: Continuation<Boolean>) {
suspend fun switchContext(context: SynchronizationContext): Boolean = suspendCoroutine { cont ->
schedulerDelegate.doContextSwitch(context, cont::resume)
}

Expand All @@ -91,7 +97,7 @@ class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitSchedul
*
* @param context the synchronization context of the new task
*/
suspend fun newContext(context: SynchronizationContext, cont: Continuation<Unit>) {
suspend fun newContext(context: SynchronizationContext): Unit = suspendCoroutine { cont ->
schedulerDelegate.forceNewContext(context, { cont.resume(Unit) })
}

Expand All @@ -102,18 +108,18 @@ class BukkitSchedulerController(val plugin: Plugin, val scheduler: BukkitSchedul
* things like countdowns and delays at fixed intervals, since [waitFor] will not result in a new task being
* spawned.
*/
suspend fun repeating(resolution: Long, cont: Continuation<Long>) {
suspend fun repeating(resolution: Long): Long = suspendCoroutine { cont ->
schedulerDelegate = RepeatingTaskScheduler(resolution, plugin, scheduler)
schedulerDelegate.forceNewContext(currentContext()) { cont.resume(0) }
}

operator fun handleResult(result: Unit, cont: Continuation<Nothing>) {
override fun resume(result: Unit) {
currentTask?.cancel()
}

operator fun handleException(e: Throwable, cont: Continuation<Nothing>) {
override fun resumeWithException(exception: Throwable) {
currentTask?.cancel()
throw e
throw exception
}

}
Expand Down

0 comments on commit cf1313f

Please sign in to comment.