Skip to content

Commit 2222331

Browse files
authored
Merge pull request #1021 from Stefterv/schema-threading
Improved Schema threading
2 parents 5504c8c + 1ab2359 commit 2222331

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

app/src/processing/app/Schema.kt

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package processing.app
22

3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.Job
6+
import kotlinx.coroutines.launch
37
import processing.app.ui.Editor
48
import java.io.File
59
import java.io.FileOutputStream
@@ -13,6 +17,8 @@ import java.util.*
1317
class Schema {
1418
companion object{
1519
private var base: Base? = null
20+
val jobs = mutableListOf<Job>()
21+
1622
@JvmStatic
1723
fun handleSchema(input: String, base: Base): Editor?{
1824
this.base = base
@@ -92,8 +98,9 @@ class Schema {
9298
}
9399

94100
}
101+
102+
private val scope = CoroutineScope(Dispatchers.Default)
95103
private fun downloadFiles(uri: URI, urlList: String, targetFolder: File, extension: String = ""){
96-
Thread{
97104
targetFolder.mkdirs()
98105

99106
val base = uri.path.split("/")
@@ -128,15 +135,20 @@ class Schema {
128135
URL("https://$content").path.isNotBlank() -> "https://$content"
129136
else -> "https://$base/$content"
130137
})
131-
url.openStream().use { input ->
132-
target.outputStream().use { output ->
133-
input.copyTo(output)
138+
val download = scope.launch{
139+
url.openStream().use { input ->
140+
target.outputStream().use { output ->
141+
input.copyTo(output)
142+
}
134143
}
135144
}
145+
jobs.add(download)
146+
download.invokeOnCompletion {
147+
jobs.remove(download)
148+
}
136149
}
137150

138151
}
139-
}.start()
140152
}
141153

142154

app/test/processing/app/SchemaTest.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package processing.app
22

3+
import kotlinx.coroutines.joinAll
4+
import kotlinx.coroutines.runBlocking
35
import org.junit.jupiter.params.ParameterizedTest
46
import org.junit.jupiter.params.provider.ValueSource
57
import org.mockito.ArgumentCaptor
@@ -65,8 +67,8 @@ class SchemaTest {
6567

6668
val base64 = Base64.encode(sketch.toByteArray())
6769
Schema.handleSchema("pde://sketch/base64/$base64?pde=AnotherFile:$base64", base)
68-
val captor = ArgumentCaptor.forClass(String::class.java)
6970

71+
val captor = ArgumentCaptor.forClass(String::class.java)
7072
verify(base).handleOpenUntitled(captor.capture())
7173

7274
val file = File(captor.value)
@@ -82,6 +84,7 @@ class SchemaTest {
8284
@Test
8385
fun testURLSketch() {
8486
Schema.handleSchema("pde://sketch/url/github.com/processing/processing-examples/raw/refs/heads/main/Basics/Arrays/Array/Array.pde", base)
87+
waitForSchemeJobsToComplete()
8588

8689
val captor = ArgumentCaptor.forClass(String::class.java)
8790
verify(base).handleOpenUntitled(captor.capture())
@@ -104,6 +107,7 @@ class SchemaTest {
104107
])
105108
fun testURLSketchWithFile(file: String){
106109
Schema.handleSchema("pde://sketch/url/github.com/processing/processing-examples/raw/refs/heads/main/Basics/Arrays/ArrayObjects/ArrayObjects.pde?pde=$file", base)
110+
waitForSchemeJobsToComplete()
107111

108112
val captor = ArgumentCaptor.forClass(String::class.java)
109113
verify(base).handleOpenUntitled(captor.capture())
@@ -126,4 +130,10 @@ class SchemaTest {
126130
Preferences.save()
127131
}
128132
}
133+
134+
fun waitForSchemeJobsToComplete(){
135+
runBlocking {
136+
joinAll(*Schema.jobs.toTypedArray())
137+
}
138+
}
129139
}

0 commit comments

Comments
 (0)