Improved Schema threading

This commit is contained in:
Stef Tervelde
2025-03-28 07:56:18 +01:00
parent 499d200ef3
commit 1ab2359cac
2 changed files with 28 additions and 6 deletions

View File

@@ -1,5 +1,9 @@
package processing.app
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import processing.app.ui.Editor
import java.io.File
import java.io.FileOutputStream
@@ -13,6 +17,8 @@ import java.util.*
class Schema {
companion object{
private var base: Base? = null
val jobs = mutableListOf<Job>()
@JvmStatic
fun handleSchema(input: String, base: Base): Editor?{
this.base = base
@@ -92,8 +98,9 @@ class Schema {
}
}
private val scope = CoroutineScope(Dispatchers.Default)
private fun downloadFiles(uri: URI, urlList: String, targetFolder: File, extension: String = ""){
Thread{
targetFolder.mkdirs()
val base = uri.path.split("/")
@@ -128,15 +135,20 @@ class Schema {
URL("https://$content").path.isNotBlank() -> "https://$content"
else -> "https://$base/$content"
})
url.openStream().use { input ->
target.outputStream().use { output ->
input.copyTo(output)
val download = scope.launch{
url.openStream().use { input ->
target.outputStream().use { output ->
input.copyTo(output)
}
}
}
jobs.add(download)
download.invokeOnCompletion {
jobs.remove(download)
}
}
}
}.start()
}

View File

@@ -1,5 +1,7 @@
package processing.app
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.mockito.ArgumentCaptor
@@ -65,8 +67,8 @@ class SchemaTest {
val base64 = Base64.encode(sketch.toByteArray())
Schema.handleSchema("pde://sketch/base64/$base64?pde=AnotherFile:$base64", base)
val captor = ArgumentCaptor.forClass(String::class.java)
val captor = ArgumentCaptor.forClass(String::class.java)
verify(base).handleOpenUntitled(captor.capture())
val file = File(captor.value)
@@ -82,6 +84,7 @@ class SchemaTest {
@Test
fun testURLSketch() {
Schema.handleSchema("pde://sketch/url/github.com/processing/processing-examples/raw/refs/heads/main/Basics/Arrays/Array/Array.pde", base)
waitForSchemeJobsToComplete()
val captor = ArgumentCaptor.forClass(String::class.java)
verify(base).handleOpenUntitled(captor.capture())
@@ -104,6 +107,7 @@ class SchemaTest {
])
fun testURLSketchWithFile(file: String){
Schema.handleSchema("pde://sketch/url/github.com/processing/processing-examples/raw/refs/heads/main/Basics/Arrays/ArrayObjects/ArrayObjects.pde?pde=$file", base)
waitForSchemeJobsToComplete()
val captor = ArgumentCaptor.forClass(String::class.java)
verify(base).handleOpenUntitled(captor.capture())
@@ -126,4 +130,10 @@ class SchemaTest {
Preferences.save()
}
}
fun waitForSchemeJobsToComplete(){
runBlocking {
joinAll(*Schema.jobs.toTypedArray())
}
}
}