Skip to content

Coroutines

Miu edited this page Aug 15, 2023 · 2 revisions

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.

To get started with Nexus, we recommend reading the following in chronological:

  1. Installation & Preparing Nexus
  2. Designing Commands
  3. Command Interceptors
  4. Additional Features (Subcommand Router, Option Validation)
  5. Context Menus
  6. Command Synchronization

You may want to read a specific part of handling command and middleware responses:

You can also read about additional features of Nexus:

You can read about synchronizing commands to Discord:

For more additional performance:

Additional configurations:

Clone this wiki locally