mirror of
https://github.com/processing/processing4.git
synced 2026-01-26 01:41:06 +01:00
Improved Schema threading
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user