Skip to content

Commit

Permalink
Migrate creator versions download to coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
RedNesto committed Jul 16, 2024
1 parent da8086f commit 8537ff0
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 123 deletions.
28 changes: 27 additions & 1 deletion src/main/kotlin/creator/custom/CreatorContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,37 @@
package com.demonwav.mcdev.creator.custom

import com.demonwav.mcdev.creator.custom.types.CreatorProperty
import com.demonwav.mcdev.creator.modalityState
import com.intellij.ide.util.projectWizard.WizardContext
import com.intellij.openapi.application.EDT
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.asContextElement
import com.intellij.openapi.observable.properties.PropertyGraph
import com.intellij.util.namedChildScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext

data class CreatorContext(
val graph: PropertyGraph,
val properties: Map<String, CreatorProperty<*>>,
val wizardContext: WizardContext,
)
val scope: CoroutineScope
) {
val modalityState: ModalityState
get() = wizardContext.modalityState

val coroutineContext: CoroutineContext
get() = modalityState.asContextElement()

/**
* The CoroutineContext to use when a change has to be made to the creator UI
*/
val uiContext: CoroutineContext
get() = Dispatchers.EDT + coroutineContext

/**
* A general purpose scope dependent of the main creator scope, cancelled when the creator is closed.
*/
fun childScope(name: String): CoroutineScope = scope.namedChildScope(name)
}
4 changes: 3 additions & 1 deletion src/main/kotlin/creator/custom/CustomPlatformStep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class CustomPlatformStep(
parent: NewProjectWizardStep,
) : AbstractNewProjectWizardStep(parent) {

val creatorScope = TemplateService.instance.scope("MinecraftDev Creator")
val creatorUiScope = TemplateService.instance.scope("MinecraftDev Creator UI")
val templateRepos = MinecraftSettings.instance.creatorTemplateRepos

Expand Down Expand Up @@ -126,10 +127,11 @@ class CustomPlatformStep(
private var hasTemplateErrors: Boolean = true

private var properties = mutableMapOf<String, CreatorProperty<*>>()
private var creatorContext = CreatorContext(propertyGraph, properties, context)
private var creatorContext = CreatorContext(propertyGraph, properties, context, creatorScope)

init {
Disposer.register(context.disposable) {
creatorScope.cancel("The creator got disposed")
creatorUiScope.cancel("The creator got disposed")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ import com.intellij.util.ui.AsyncProcessIcon
import javax.swing.DefaultComboBoxModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.swing.Swing
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ArchitecturyVersionsCreatorProperty(
Expand Down Expand Up @@ -280,7 +279,7 @@ class ArchitecturyVersionsCreatorProperty(
updateArchitecturyApiVersions()
}

downloadVersions {
downloadVersions(context) {
val fabricVersions = fabricVersions
if (fabricVersions != null) {
loaderVersionModel.removeAllElements()
Expand Down Expand Up @@ -433,36 +432,35 @@ class ArchitecturyVersionsCreatorProperty(
private var fabricApiVersions: FabricApiVersions? = null
private var architecturyVersions: ArchitecturyVersion? = null

private fun downloadVersions(completeCallback: () -> Unit) {
private fun downloadVersions(context: CreatorContext, completeCallback: () -> Unit) {
if (hasDownloadedVersions) {
completeCallback()
return
}

application.executeOnPooledThread {
runBlocking {
awaitAll(
asyncIO { ForgeVersion.downloadData().also { forgeVersions = it } },
asyncIO { NeoForgeVersion.downloadData().also { neoForgeVersions = it } },
asyncIO { FabricVersions.downloadData().also { fabricVersions = it } },
asyncIO {
collectMavenVersions(
"https://maven.architectury.dev/dev/architectury/architectury-loom/maven-metadata.xml"
).also {
loomVersions = it
.mapNotNull(SemanticVersion::tryParse)
.sortedDescending()
}
},
asyncIO { FabricApiVersions.downloadData().also { fabricApiVersions = it } },
asyncIO { ArchitecturyVersion.downloadData().also { architecturyVersions = it } },
)

hasDownloadedVersions = true

withContext(Dispatchers.Swing) {
completeCallback()
}
val scope = context.childScope("ArchitecturyVersionsCreatorProperty")
scope.launch(Dispatchers.Default) {
awaitAll(
asyncIO { ForgeVersion.downloadData().also { forgeVersions = it } },
asyncIO { NeoForgeVersion.downloadData().also { neoForgeVersions = it } },
asyncIO { FabricVersions.downloadData().also { fabricVersions = it } },
asyncIO {
collectMavenVersions(
"https://maven.architectury.dev/dev/architectury/architectury-loom/maven-metadata.xml"
).also {
loomVersions = it
.mapNotNull(SemanticVersion::tryParse)
.sortedDescending()
}
},
asyncIO { FabricApiVersions.downloadData().also { fabricApiVersions = it } },
asyncIO { ArchitecturyVersion.downloadData().also { architecturyVersions = it } },
)

hasDownloadedVersions = true

withContext(context.uiContext) {
completeCallback()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ import com.intellij.ui.JBColor
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.util.application
import com.intellij.util.ui.AsyncProcessIcon
import javax.swing.DefaultComboBoxModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.swing.Swing
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class FabricVersionsCreatorProperty(
Expand Down Expand Up @@ -220,7 +218,7 @@ class FabricVersionsCreatorProperty(
updateFabricApiVersions()
}

downloadVersion {
downloadVersion(context) {
val fabricVersions = fabricVersions
if (fabricVersions != null) {
loaderVersionModel.removeAllElements()
Expand Down Expand Up @@ -307,31 +305,30 @@ class FabricVersionsCreatorProperty(
private var loomVersions: List<SemanticVersion>? = null
private var fabricApiVersions: FabricApiVersions? = null

private fun downloadVersion(uiCallback: () -> Unit) {
private fun downloadVersion(context: CreatorContext, uiCallback: () -> Unit) {
if (hasDownloadedVersions) {
uiCallback()
return
}

application.executeOnPooledThread {
runBlocking {
awaitAll(
asyncIO { FabricVersions.downloadData().also { fabricVersions = it } },
asyncIO {
collectMavenVersions(
"https://maven.fabricmc.net/net/fabricmc/fabric-loom/maven-metadata.xml"
).mapNotNull(SemanticVersion::tryParse)
.sortedDescending()
.also { loomVersions = it }
},
asyncIO { FabricApiVersions.downloadData().also { fabricApiVersions = it } },
)

hasDownloadedVersions = true

withContext(Dispatchers.Swing) {
uiCallback()
}
val scope = context.childScope("FabricVersionsCreatorProperty")
scope.launch(Dispatchers.Default) {
awaitAll(
asyncIO { FabricVersions.downloadData().also { fabricVersions = it } },
asyncIO {
collectMavenVersions(
"https://maven.fabricmc.net/net/fabricmc/fabric-loom/maven-metadata.xml"
).mapNotNull(SemanticVersion::tryParse)
.sortedDescending()
.also { loomVersions = it }
},
asyncIO { FabricApiVersions.downloadData().also { fabricApiVersions = it } },
)

hasDownloadedVersions = true

withContext(context.uiContext) {
uiCallback()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ import com.intellij.ui.ComboboxSpeedSearch
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.RightGap
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.util.application
import com.intellij.util.ui.AsyncProcessIcon
import javax.swing.DefaultComboBoxModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.swing.Swing
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ForgeVersionsCreatorProperty(
Expand Down Expand Up @@ -148,7 +146,7 @@ class ForgeVersionsCreatorProperty(
}
}

downloadVersions {
downloadVersions(context) {
reloadMinecraftVersions()

loadingVersionsProperty.set(false)
Expand Down Expand Up @@ -186,21 +184,20 @@ class ForgeVersionsCreatorProperty(

private var forgeVersion: ForgeVersion? = null

private fun downloadVersions(uiCallback: () -> Unit) {
private fun downloadVersions(context: CreatorContext, uiCallback: () -> Unit) {
if (hasDownloadedVersions) {
uiCallback()
return
}

application.executeOnPooledThread {
runBlocking {
forgeVersion = ForgeVersion.downloadData()
val scope = context.childScope("ForgeVersionsCreatorProperty")
scope.launch(Dispatchers.IO) {
forgeVersion = ForgeVersion.downloadData()

hasDownloadedVersions = true
hasDownloadedVersions = true

withContext(Dispatchers.Swing) {
uiCallback()
}
withContext(context.uiContext) {
uiCallback()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ import com.intellij.openapi.observable.properties.GraphProperty
import com.intellij.ui.ComboboxSpeedSearch
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.util.application
import com.intellij.util.ui.AsyncProcessIcon
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.swing.Swing
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MavenArtifactVersionCreatorProperty(
Expand Down Expand Up @@ -110,6 +108,7 @@ class MavenArtifactVersionCreatorProperty(
}

downloadVersions(
context,
// The key might be a bit too unique, but that'll do the job
descriptor.name + "@" + descriptor.hashCode(),
sourceUrl,
Expand All @@ -127,6 +126,7 @@ class MavenArtifactVersionCreatorProperty(
private var versionsCache = ConcurrentHashMap<String, List<SemanticVersion>>()

private fun downloadVersions(
context: CreatorContext,
key: String,
url: String,
rawVersionFilter: (String) -> Boolean,
Expand All @@ -143,22 +143,21 @@ class MavenArtifactVersionCreatorProperty(
return
}

application.executeOnPooledThread {
runBlocking {
val versions = collectMavenVersions(url)
.asSequence()
.filter(rawVersionFilter)
.mapNotNull(SemanticVersion::tryParse)
.filter(versionFilter)
.sortedDescending()
.take(limit)
.toList()

versionsCache[cacheKey] = versions

withContext(Dispatchers.Swing) {
uiCallback(versions)
}
val scope = context.childScope("MavenArtifactVersionCreatorProperty")
scope.launch(Dispatchers.Default) {
val versions = withContext(Dispatchers.IO) { collectMavenVersions(url) }
.asSequence()
.filter(rawVersionFilter)
.mapNotNull(SemanticVersion::tryParse)
.filter(versionFilter)
.sortedDescending()
.take(limit)
.toList()

versionsCache[cacheKey] = versions

withContext(context.uiContext) {
uiCallback(versions)
}
}
}
Expand Down
Loading

0 comments on commit 8537ff0

Please sign in to comment.