Skip to content

Commit

Permalink
switching a some commands used by the client app to the processor style
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Jul 14, 2023
1 parent 94e21b0 commit f1d5f7c
Show file tree
Hide file tree
Showing 29 changed files with 201 additions and 68 deletions.
19 changes: 19 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.zegreatrob.coupling.client.components

import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.action.async.SuspendAction

interface DispatchFunc<D> {
operator fun <C : SuspendAction<D, R>, R> invoke(
commandFunc: () -> C,
response: (R) -> Unit,
): () -> Unit

operator fun <C, R> invoke(
commandFunc: () -> C,
fireCommand: suspend ActionCannon<D>.(C) -> R,
response: (R) -> Unit,
): () -> Unit
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zegreatrob.coupling.client.components.pairassignments

import com.zegreatrob.coupling.action.pairassignmentdocument.DeletePairAssignmentsCommand
import com.zegreatrob.coupling.action.pairassignmentdocument.fire
import com.zegreatrob.coupling.client.components.CouplingButton
import com.zegreatrob.coupling.client.components.DispatchFunc
import com.zegreatrob.coupling.client.components.Paths.currentPairsPage
Expand Down Expand Up @@ -40,8 +41,9 @@ val CurrentPairAssignmentsPanel by nfc<CurrentPairAssignmentsPanelProps> { props
val (redirectUrl, setRedirectUrl) = useState<String?>(null)
val redirectToCurrentFunc = { setRedirectUrl(party.id.currentPairsPage()) }
val onCancel = dispatchFunc(
{ DeletePairAssignmentsCommand(party.id, pairAssignments.id) },
{ redirectToCurrentFunc() },
commandFunc = { DeletePairAssignmentsCommand(party.id, pairAssignments.id) },
fireCommand = ::fire,
response = { redirectToCurrentFunc() },
)
if (redirectUrl != null) {
Navigate { to = redirectUrl }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zegreatrob.coupling.client.components.pairassignments

import com.zegreatrob.coupling.action.pairassignmentdocument.DeletePairAssignmentsCommand
import com.zegreatrob.coupling.action.pairassignmentdocument.fire
import com.zegreatrob.coupling.client.components.Controls
import com.zegreatrob.coupling.client.components.CouplingButton
import com.zegreatrob.coupling.client.components.external.w3c.WindowFunctions
Expand Down Expand Up @@ -55,7 +56,11 @@ val PairAssignmentRow by nfc<PairAssignmentRowProps> { props ->
val windowFuncs = props.windowFunctions ?: WindowFunctions
val (dispatchFunc, reload) = controls
val onDeleteClick: () -> Unit = useCallback {
val deleteFunc = dispatchFunc({ DeletePairAssignmentsCommand(party.id, document.id) }, { reload() })
val deleteFunc = dispatchFunc(
commandFunc = { DeletePairAssignmentsCommand(party.id, document.id) },
fireCommand = ::fire,
response = { reload() },
)
if (windowFuncs.window.confirm("Are you sure you want to delete these pair assignments?")) {
deleteFunc.invoke()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.zegreatrob.coupling.client.components.slack
import com.benasher44.uuid.uuid4
import com.zegreatrob.coupling.action.VoidResult
import com.zegreatrob.coupling.action.party.SaveSlackIntegrationCommand
import com.zegreatrob.coupling.action.party.fire
import com.zegreatrob.coupling.client.components.ConfigForm
import com.zegreatrob.coupling.client.components.CouplingButton
import com.zegreatrob.coupling.client.components.DispatchFunc
Expand Down Expand Up @@ -49,7 +50,11 @@ val SlackConnectPageContent by nfc<SlackConnectPageContentProps> { props ->
)
}
var result by useState<VoidResult?>(null)
val onSave = props.dispatchFunc({ command }, { result = it })
val onSave = props.dispatchFunc(
commandFunc = { command },
fireCommand = ::fire,
response = { result = it },
)

if (result == null) {
ConfigForm {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zegreatrob.coupling.client.components.spin

import com.zegreatrob.coupling.action.pairassignmentdocument.SpinCommand
import com.zegreatrob.coupling.action.pairassignmentdocument.fire
import com.zegreatrob.coupling.client.components.DispatchFunc
import com.zegreatrob.coupling.client.components.Paths.newPairAssignmentsPath
import com.zegreatrob.coupling.model.pairassignmentdocument.PairAssignmentDocument
Expand Down Expand Up @@ -67,6 +68,7 @@ private fun onSpin(
pinSelections: List<String?>,
setRedirectUrl: (String) -> Unit,
) = dispatchFunc(
{ SpinCommand(party.id, playerSelections.playerIds(), pinSelections.filterNotNull()) },
{ setRedirectUrl(party.newPairAssignmentsPath()) },
commandFunc = { SpinCommand(party.id, playerSelections.playerIds(), pinSelections.filterNotNull()) },
fireCommand = ::fire,
response = { setRedirectUrl(party.newPairAssignmentsPath()) },
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zegreatrob.coupling.client.components

import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.action.ActionWrapper
import com.zegreatrob.testmints.action.async.SuspendAction

class StubDispatcher {
Expand All @@ -13,6 +15,12 @@ class StubDispatcher {
?.responseFunc
?.invoke(result)
}
fun <C, R> wrappedSendResult(result: R) {
dispatchList.filterIsInstance<DispatchedFunc<ActionWrapper<C>, R>>()
.firstOrNull()
?.responseFunc
?.invoke(result)
}

fun <C, D, R> commandFunctionsDispatched() where C : SuspendAction<D, R> =
dispatchList.filterIsInstance<DispatchedFunc<C, R>>()
Expand All @@ -34,4 +42,12 @@ class StubDispatchFunc<D>(private val stubber: StubDispatcher = StubDispatcher()
): () -> Unit = {
stubber.dispatchList.add(DispatchedFunc(commandFunc(), response))
}

override fun <C, R> invoke(
commandFunc: () -> C,
fireCommand: suspend ActionCannon<D>.(C) -> R,
response: (R) -> Unit,
): () -> Unit = {
stubber.dispatchList.add(DispatchedFunc(commandFunc(), response))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class CurrentPairAssignmentsPanelTest {
)
} exercise {
actor.click(screen.getByText("Save!"))
stubDispatcher.sendResult<SavePairAssignmentsCommand, _>(VoidResult.Accepted)
stubDispatcher.wrappedSendResult<SavePairAssignmentsCommand, _>(VoidResult.Accepted)
} verify {
waitFor {
stubDispatcher.commandsDispatched<SavePairAssignmentsCommand>().size
Expand Down Expand Up @@ -104,7 +104,7 @@ class CurrentPairAssignmentsPanelTest {
)
} exercise {
actor.click(screen.findByText("Cancel"))
act { stubDispatcher.sendResult<DeletePairAssignmentsCommand, _>(VoidResult.Accepted) }
act { stubDispatcher.wrappedSendResult<DeletePairAssignmentsCommand, _>(VoidResult.Accepted) }
} verify {
waitFor {
stubDispatcher.commandsDispatched<DeletePairAssignmentsCommand>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PairAssignmentRowTest {
)
} exercise {
actor.click(screen.getByText("DELETE"))
stubDispatcher.sendResult<DeletePairAssignmentsCommand, _>(VoidResult.Accepted)
stubDispatcher.wrappedSendResult<DeletePairAssignmentsCommand, _>(VoidResult.Accepted)
} verify {
stubDispatcher.commandsDispatched<DeletePairAssignmentsCommand>()
.assertIsEqualTo(listOf(DeletePairAssignmentsCommand(party.id, document.id)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SlackConnectPageContentTest {
.assertIsEqualTo(null, "Return button showed up unexpectedly early")
} exercise {
actor.click(saveButton)
act { stubber.sendResult<SaveSlackIntegrationCommand, _>(VoidResult.Accepted) }
act { stubber.wrappedSendResult<SaveSlackIntegrationCommand, _>(VoidResult.Accepted) }
} verify {
stubber.commandsDispatched<SaveSlackIntegrationCommand>()
.assertIsEqualTo(
Expand Down Expand Up @@ -113,7 +113,7 @@ class SlackConnectPageContentTest {
},
)
actor.click(saveButton)
act { stubDispatcher.sendResult<SaveSlackIntegrationCommand, _>(VoidResult.Accepted) }
act { stubDispatcher.wrappedSendResult<SaveSlackIntegrationCommand, _>(VoidResult.Accepted) }
} exercise {
actor.click(returnButton)
} verify {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zegreatrob.coupling.client

import com.zegreatrob.coupling.client.components.DispatchFunc
import com.zegreatrob.react.dataloader.DataLoaderTools
import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.action.async.SuspendAction
import com.zegreatrob.testmints.action.async.SuspendActionExecuteSyntax

Expand All @@ -11,6 +12,7 @@ class DecoratedDispatchFunc<D : SuspendActionExecuteSyntax>(
) : DispatchFunc<D> {

private val dispatcher get() = dispatcherFunc()
private val cannon by lazy { ActionCannon(dispatcher) }

override fun <C : SuspendAction<D, R>, R> invoke(commandFunc: () -> C, response: (R) -> Unit) = fun() {
val command = commandFunc()
Expand All @@ -20,4 +22,17 @@ class DecoratedDispatchFunc<D : SuspendActionExecuteSyntax>(
response,
)
}

override fun <C, R> invoke(
commandFunc: () -> C,
fireCommand: suspend ActionCannon<D>.(C) -> R,
response: (R) -> Unit,
): () -> Unit = fun() {
val command = commandFunc()
tools.performAsyncWork(
{ fireCommand(cannon, command) },
{ handler: Throwable -> throw handler },
response,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.zegreatrob.coupling.model.CouplingSocketMessage
import com.zegreatrob.coupling.model.pin.Pin
import com.zegreatrob.minreact.ReactFunc
import com.zegreatrob.minreact.nfc
import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.action.async.SuspendAction
import emotion.react.css
import js.core.jso
Expand Down Expand Up @@ -135,6 +136,12 @@ class NoOpDispatcherDispatchFunc : DispatchFunc<NoOpDispatcher> {
commandFunc: () -> C,
response: (R) -> Unit,
): () -> Unit = {}

override fun <C, R> invoke(
commandFunc: () -> C,
fireCommand: suspend ActionCannon<NoOpDispatcher>.(C) -> R,
response: (R) -> Unit,
) = {}
}

interface NoOpDispatcher :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zegreatrob.coupling.client.pairassignments

import com.zegreatrob.coupling.action.pairassignmentdocument.DeletePairAssignmentsCommand
import com.zegreatrob.coupling.action.pairassignmentdocument.SavePairAssignmentsCommand
import com.zegreatrob.coupling.action.pairassignmentdocument.fire
import com.zegreatrob.coupling.client.components.Controls
import com.zegreatrob.coupling.client.components.CouplingWebsocket
import com.zegreatrob.coupling.client.components.disconnectedMessage
Expand Down Expand Up @@ -41,7 +42,11 @@ val SocketedPairAssignments by nfc<SocketedPairAssignmentsProps<*>> { (party, pl
val onMessageFunc: (Message) -> Unit = useCallback { handleMessage(it, setMessage, setPairAssignments) }
val updatePairAssignments = useCallback(party.id, controls.dispatchFunc) { new: PairAssignmentDocument ->
setPairAssignments(new)
controls.dispatchFunc({ SavePairAssignmentsCommand(party.id, new) }, {}).invoke()
controls.dispatchFunc(
commandFunc = { SavePairAssignmentsCommand(party.id, new) },
fireCommand = ::fire,
response = {},
).invoke()
}
val auth0Data = useAuth0Data()
var token by useState("")
Expand Down
2 changes: 1 addition & 1 deletion coupling-plugins/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ com-fasterxml-jackson-core-jackson-databind = "com.fasterxml.jackson.core:jackso
com-github-ben-manes-gradle-versions-plugin = "com.github.ben-manes:gradle-versions-plugin:0.47.0"
com-soywiz-korlibs-klock = "com.soywiz.korlibs.klock:klock:4.0.8"
com-zegreatrob-jsmints-jsmints-bom = "com.zegreatrob.jsmints:jsmints-bom:4.3.13"
com-zegreatrob-testmints-testmints-bom = "com.zegreatrob.testmints:testmints-bom:10.0.19"
com-zegreatrob-testmints-testmints-bom = "com.zegreatrob.testmints:testmints-bom:10.0.20"
com-zegreatrob-tools-tools-bom = "com.zegreatrob.tools:tools-bom:0.5.4"
org-ajoberstar-grgit-gradle-plugin = "org.ajoberstar.grgit:org.ajoberstar.grgit.gradle.plugin:5.0.0"
org-apache-logging-log4j-log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "org-apache-logging-log4j" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ com-fasterxml-jackson-core-jackson-databind = "com.fasterxml.jackson.core:jackso
com-github-ajalt-clikt-clikt = "com.github.ajalt.clikt:clikt:4.0.0"
com-soywiz-korlibs-klock = "com.soywiz.korlibs.klock:klock:4.0.8"
com-zegreatrob-jsmints-jsmints-bom = "com.zegreatrob.jsmints:jsmints-bom:4.3.13"
com-zegreatrob-testmints-testmints-bom = "com.zegreatrob.testmints:testmints-bom:10.0.19"
com-zegreatrob-testmints-testmints-bom = "com.zegreatrob.testmints:testmints-bom:10.0.20"
io-github-microutils-kotlin-logging = "io.github.microutils:kotlin-logging:3.0.5"
io-ktor-ktor-bom = "io.ktor:ktor-bom:2.3.2"
org-jetbrains-kotlin-wrappers-kotlin-wrappers-bom = "org.jetbrains.kotlin-wrappers:kotlin-wrappers-bom:1.0.0-pre.596"
Expand Down
Loading

0 comments on commit f1d5f7c

Please sign in to comment.