Coroutines - ShindouMihou/Nexus GitHub Wiki

Nexus employs native Java threads (system threads) internally to manage various tasks using its dedicated thread pool. While this approach is effective, an alternative with potential benefits exists. Kotlin introduces its own coroutine system, accessible exclusively to Kotlin users. However, to ensure compatibility with Java developers, we do not employ this system by default. Nevertheless, we recognize the advantages of Kotlin's coroutine technology and have introduced our custom async wrapper.

Nexus Launch

The Nexus Launch serves as our proprietary async wrapper, granting developers the ability to encompass Nexus' internal async processes (such as event handling) with alternative, more cost-effective solutions. By adjusting Nexus' configuration, you can employ a distinct async mechanism (please note that in this example, we use GlobalScope, but we recommend using your personal CoroutineScope):

Nexus.configure {
    launch.launcher = NexusLaunchWrapper { task ->
        GlobalScope.launch { task.run() }
    }
    
    launch.scheduler = NexusScheduledLaunchWrapper { timeInMillis, task ->
        return@NexusScheduledLaunchWrapper object : Cancellable {
            val job = GlobalScope.launch {
                delay(timeInMillis)
                task.run()
            }
    
            override fun cancel(mayInterruptIfRunning: Boolean): Boolean {
                job.cancel()
                return true
            }
        }
    }
}

From a performance perspective, this adjustment typically yields no substantial or easily discernible impact. However, if you perceive a difference or are curious, consider implementing and evaluating a similar solution. While this alteration may not deliver significant gains, it similarly poses no drawbacks. Feel free to experiment with this approach as needed.