Skip to content

Commit

Permalink
Add a mcSyncLaunch extension function
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobkmar committed May 20, 2021
1 parent 5fbbc6c commit e80b16d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ package net.axay.fabrik.core.task

import kotlinx.coroutines.*

/**
* A [CoroutineDispatcher] which executes code synchronously to the
* MinecraftServer main thread.
*/
lateinit var mcCoroutineDispatcher: CoroutineDispatcher
internal set

/**
* A [CoroutineScope] using the current MinecraftServer
* as the Dispatcher.
*/
lateinit var mcCoroutineScope: CoroutineScope
internal set

/**
* A CoroutineScope using the IO Dispatcher
* of kotlinx.coroutines.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.axay.fabrik.core.task

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
* A [CoroutineDispatcher] which executes code synchronously to the
* MinecraftServer main thread.
*/
lateinit var mcCoroutineDispatcher: CoroutineDispatcher
internal set

/**
* A [CoroutineScope] using the current MinecraftServer
* as the Dispatcher.
*/
lateinit var mcCoroutineScope: CoroutineScope
internal set

/**
* Does the same as [launch], but the dispatcher defaults to [mcCoroutineDispatcher].
*
* This way, you can execute code synchronously (to the MinecraftServer main thread)
* very easily.
*
* ```kotlin
* coroutineScope {
* mcSyncLaunch {
* // suspending and sync now
* }
* }
* ```
*/
fun CoroutineScope.mcSyncLaunch(block: suspend CoroutineScope.() -> Unit) =
launch(mcCoroutineDispatcher, block = block)

0 comments on commit e80b16d

Please sign in to comment.