Skip to content

Improved Schema threading #1021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions app/src/processing/app/Schema.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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("/")
Expand Down Expand Up @@ -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()
}


Expand Down
12 changes: 11 additions & 1 deletion app/test/processing/app/SchemaTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -126,4 +130,10 @@ class SchemaTest {
Preferences.save()
}
}

fun waitForSchemeJobsToComplete(){
runBlocking {
joinAll(*Schema.jobs.toTypedArray())
}
}
}