Skip to content

Commit

Permalink
stop a script when running another script with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
sproctor committed Dec 19, 2024
1 parent 34d7b6f commit 37eac0b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package warlockfe.warlock3.app.di

import androidx.room.RoomDatabase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import okio.Path.Companion.toPath
import warlockfe.warlock3.compose.AppContainer
import warlockfe.warlock3.core.client.WarlockClient
Expand All @@ -26,11 +28,14 @@ class JvmAppContainer(
ioDispatcher = Dispatchers.IO,
warlockDirs = warlockDirs,
) {
private val externalScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

override val scriptManager: ScriptManager =
WarlockScriptEngineRegistry(
highlightRepository = highlightRepository,
variableRepository = variableRepository,
scriptDirRepository = scriptDirRepository,
externalScope = externalScope,
)
override val sgeClientFactory: SgeClientFactory =
object : SgeClientFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ScriptInstance {
val name: String
val status: ScriptStatus

suspend fun start(client: WarlockClient, argumentString: String, onStop: suspend () -> Unit)
suspend fun start(client: WarlockClient, argumentString: String, onStop: () -> Unit)

fun stop()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package warlockfe.warlock3.scripting

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import warlockfe.warlock3.core.client.WarlockClient
import warlockfe.warlock3.core.prefs.HighlightRepository
import warlockfe.warlock3.core.prefs.ScriptDirRepository
Expand All @@ -22,6 +26,7 @@ class WarlockScriptEngineRegistry(
highlightRepository: HighlightRepository,
variableRepository: VariableRepository,
private val scriptDirRepository: ScriptDirRepository,
private val externalScope: CoroutineScope,
) : ScriptManager {

private val _scriptInfo = MutableStateFlow<Map<Long, ScriptInfo>>(emptyMap())
Expand Down Expand Up @@ -64,13 +69,20 @@ class WarlockScriptEngineRegistry(
}

private suspend fun startInstance(client: WarlockClient, instance: ScriptInstance, argString: String?) {
runningScripts.forEach { runningInstance ->
if (instance.name == runningInstance.name) {
runningInstance.stop()
}
}
client.print(StyledString("Starting script: ${instance.name}", style = WarlockStyle.Echo))
runningScripts += instance
scriptStateChanged(instance)
instance.start(client = client, argumentString = argString ?: "") {
runningScripts -= instance
_scriptInfo.update { it.minus(instance.id) }
client.print(StyledString("Script has finished: ${instance.name}", style = WarlockStyle.Echo))
externalScope.launch {
runningScripts -= instance
_scriptInfo.update { it.minus(instance.id) }
client.print(StyledString("Script has finished: ${instance.name}", style = WarlockStyle.Echo))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class JsInstance(
private set

@OptIn(ExperimentalCoroutinesApi::class)
override suspend fun start(client: WarlockClient, argumentString: String, onStop: suspend () -> Unit) {
override suspend fun start(client: WarlockClient, argumentString: String, onStop: () -> Unit) {
status = ScriptStatus.Running
this.client = client
thread = thread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class WslScriptInstance(
private val suspendedChannel = Channel<Unit>(0)

@OptIn(ExperimentalCoroutinesApi::class)
override suspend fun start(client: WarlockClient, argumentString: String, onStop: suspend () -> Unit) {
override suspend fun start(client: WarlockClient, argumentString: String, onStop: () -> Unit) {
val arguments = parseArguments(argumentString)
status = ScriptStatus.Running

Expand Down

0 comments on commit 37eac0b

Please sign in to comment.