diff --git a/app/src/processing/app/Schema.kt b/app/src/processing/app/Schema.kt index a0f6f7b99..a02bf1da7 100644 --- a/app/src/processing/app/Schema.kt +++ b/app/src/processing/app/Schema.kt @@ -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() + @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() } diff --git a/app/test/processing/app/SchemaTest.kt b/app/test/processing/app/SchemaTest.kt index 955172d17..73f7f9c9c 100644 --- a/app/test/processing/app/SchemaTest.kt +++ b/app/test/processing/app/SchemaTest.kt @@ -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()) + } + } } \ No newline at end of file