Skip to content

Commit

Permalink
carefully working through problems related to generic dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Jul 18, 2023
1 parent 615472d commit 22d8e71
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import kotlinx.coroutines.coroutineScope
import kotools.types.collection.NotEmptyList
import kotools.types.collection.toNotEmptyList

interface ServerSpinCommandDispatcher<D> :
interface ServerSpinCommandDispatcher<out D> :
SpinCommand.Dispatcher,
PartyIdPairAssignmentDocumentSaveSyntax,
PartyIdLoadSyntax,
PartyIdLoadIntegrationSyntax,
PartyIdLoadPlayersSyntax,
PartyIdHistorySyntax,
PartyIdPinRecordsSyntax,
CannonProvider<D> where D : ShufflePairsAction.Dispatcher {
CannonProvider<D> where D : ShufflePairsAction.Dispatcher<D> {

val slackRepository: SlackRepository
val slackAccessRepository: SlackAccessGet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.zegreatrob.coupling.model.pairassignmentdocument.PinnedCouplingPair
import com.zegreatrob.coupling.model.party.PartyDetails
import com.zegreatrob.coupling.model.pin.Pin
import com.zegreatrob.coupling.model.player.Player
import com.zegreatrob.coupling.server.action.CannonProvider
import com.zegreatrob.testmints.action.ExecutableActionExecuteSyntax
import com.zegreatrob.testmints.action.async.SimpleSuspendAction
import kotools.types.collection.NotEmptyList
Expand All @@ -19,11 +20,12 @@ data class ShufflePairsAction(
val players: NotEmptyList<Player>,
val pins: List<Pin>,
val history: List<PairAssignmentDocument>,
) : SimpleSuspendAction<ShufflePairsAction.Dispatcher, PairAssignmentDocument> {
override val performFunc = link(Dispatcher::perform)
) : SimpleSuspendAction<ShufflePairsAction.Dispatcher<*>, PairAssignmentDocument> {
override val performFunc = link(Dispatcher<*>::perform)

interface Dispatcher :
interface Dispatcher<out D> :
Clock,
CannonProvider<D>,
ExecutableActionExecuteSyntax,
FindNewPairsAction.Dispatcher,
AssignPinsActionDispatcher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.zegreatrob.coupling.model.party.PartyDetails
import com.zegreatrob.coupling.model.party.PartyId
import com.zegreatrob.coupling.model.player.Player
import com.zegreatrob.minassert.assertIsEqualTo
import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.setup
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
Expand All @@ -28,7 +29,7 @@ import kotlin.test.Test
class GameExamplesTest {

companion object :
ShufflePairsAction.Dispatcher,
ShufflePairsAction.Dispatcher<Any>,
FindNewPairsAction.Dispatcher,
NextPlayerAction.Dispatcher,
CreatePairCandidateReportAction.Dispatcher,
Expand All @@ -37,6 +38,7 @@ class GameExamplesTest {
Wheel {
override val wheel = this
override val actionDispatcher = this
override val cannon = ActionCannon.invoke(this)
override val execute = this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.zegreatrob.coupling.stubmodel.stubPlayer
import com.zegreatrob.minassert.assertIsEqualTo
import com.zegreatrob.minspy.SpyData
import com.zegreatrob.minspy.spyFunction
import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.setup
import kotlinx.datetime.Clock
import kotools.types.collection.NotEmptyList
Expand All @@ -26,7 +27,8 @@ class ShufflePairsActionTest {

@Test
fun willBuildAGameRunWithAllAvailablePlayersAndThenReturnTheResults() = setup(object :
ShufflePairsAction.Dispatcher {
ShufflePairsAction.Dispatcher<Any> {
override val cannon: ActionCannon<Any> get() = TODO("Not yet implemented")
override val execute = stubActionExecutor(NextPlayerAction::class)
override val wheel: Wheel get() = throw NotImplementedError("Stubbed")

Expand Down
10 changes: 5 additions & 5 deletions server/src/jsMain/kotlin/Handler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fun serverlessSocketConnect(event: dynamic, context: dynamic) = js("require('ser
private suspend fun handleConnect(request: Request, connectionId: String, event: Any?): Int {
val commandDispatcher = with(request) { commandDispatcher(user, scope, traceId) }
val partyId = request.query["partyId"].toString().let(::PartyId)
val result = commandDispatcher.execute(ConnectPartyUserCommand(partyId, connectionId))
val result = commandDispatcher.perform(ConnectPartyUserCommand(partyId, connectionId))
return if (result == null) {
403
} else {
Expand Down Expand Up @@ -148,7 +148,7 @@ fun serverlessSocketMessage(event: Json): dynamic {
return MainScope().promise {
val socketDispatcher = socketDispatcher()
if (message is PairAssignmentAdjustmentMessage) {
socketDispatcher.execute(
socketDispatcher.perform(
ReportDocCommand(connectionId, message.currentPairAssignments),
)?.broadcast(socketDispatcher)
}
Expand All @@ -162,7 +162,7 @@ fun notifyConnect(event: Json) = MainScope().promise {
val connectionId = event.at<String>("/requestContext/connectionId") ?: ""
println("notifyConnect $connectionId")
val socketDispatcher = socketDispatcher()
socketDispatcher.execute(ConnectionsQuery(connectionId))
socketDispatcher.perform(ConnectionsQuery(connectionId))
?.let { results ->
socketDispatcher.managementApiClient.send(
PostToConnectionCommand(
Expand All @@ -185,7 +185,7 @@ fun serverlessSocketDisconnect(event: dynamic) = MainScope().promise {
val socketDispatcher = socketDispatcher()

socketDispatcher
.execute(DisconnectPartyUserCommand(connectionId))
.perform(DisconnectPartyUserCommand(connectionId))
?.broadcast(socketDispatcher)
.let { json("statusCode" to 200) }
}
Expand All @@ -197,7 +197,7 @@ private suspend fun CoroutineScope.socketDispatcher() = commandDispatcher(
)

private suspend fun Pair<List<CouplingConnection>, CouplingSocketMessage>.broadcast(socketDispatcher: CommandDispatcher) =
socketDispatcher.execute(BroadcastAction(first, second))
socketDispatcher.perform(BroadcastAction(first, second))

private fun delete(connectionId: String, managementApi: ApiGatewayManagementApiClient) = managementApi.send(
DeleteConnectionCommand(json("ConnectionId" to connectionId)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.zegreatrob.coupling.server.action.pairassignmentdocument.PairAssignme
import com.zegreatrob.coupling.server.action.pairassignmentdocument.ServerDeletePairAssignmentsCommandDispatcher
import com.zegreatrob.coupling.server.action.pairassignmentdocument.ServerSavePairAssignmentDocumentCommandDispatcher
import com.zegreatrob.coupling.server.action.pairassignmentdocument.ServerSpinCommandDispatcher
import com.zegreatrob.coupling.server.action.pairassignmentdocument.ShufflePairsAction
import com.zegreatrob.coupling.server.action.party.PartyIntegrationQuery
import com.zegreatrob.coupling.server.action.party.ServerDeletePartyCommandDispatcher
import com.zegreatrob.coupling.server.action.pin.PinsQuery
Expand All @@ -50,6 +51,7 @@ import com.zegreatrob.coupling.server.secret.JwtSecretGenerator
import com.zegreatrob.coupling.server.secret.ServerDeleteSecretCommandDispatcher
import com.zegreatrob.coupling.server.slack.FetchSlackRepository
import com.zegreatrob.testmints.action.ActionCannon
import com.zegreatrob.testmints.action.ExecutableActionExecutor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
Expand All @@ -64,9 +66,7 @@ interface ICommandDispatcher :
ConnectionsQuery.Dispatcher,
CurrentPairAssignmentDocumentQuery.Dispatcher,
DisconnectPartyUserCommand.Dispatcher,
DispatchingActionExecutor<CommandDispatcher>,
GlobalStatsQuery.Dispatcher,
PairAssignmentDispatcher,
PairAssignmentDocumentListQuery.Dispatcher,
PartyDispatcher,
PinsQuery.Dispatcher,
Expand All @@ -88,9 +88,6 @@ class CommandDispatcher(
override val traceId: Uuid,
override val managementApiClient: ApiGatewayManagementApiClient = apiGatewayManagementApiClient(),
) : ICommandDispatcher, RepositoryCatalog by repositoryCatalog, TraceIdProvider {
override val execute = this
override val actionDispatcher = this

override val slackRepository: SlackRepository by lazy { FetchSlackRepository() }

private var authorizedPartyIdDispatcherJob: Deferred<CurrentPartyDispatcher>? = null
Expand Down Expand Up @@ -121,6 +118,9 @@ class CurrentPartyDispatcher(
private val commandDispatcher: CommandDispatcher,
) :
ICommandDispatcher by commandDispatcher,
DispatchingActionExecutor<CurrentPartyDispatcher>,
ShufflePairsAction.Dispatcher<CurrentPartyDispatcher>,
PairAssignmentDispatcher<CurrentPartyDispatcher>,
ServerSpinCommandDispatcher<CurrentPartyDispatcher>,
ServerSaveSlackIntegrationCommandDispatcher,
ServerCreateSecretCommandDispatcher,
Expand All @@ -135,11 +135,10 @@ class CurrentPartyDispatcher(
ServerSavePinCommandDispatcher,
CannonProvider<CurrentPartyDispatcher> {
override val userId: String get() = commandDispatcher.userId

override val execute: ExecutableActionExecutor<PairAssignmentDispatcher<CurrentPartyDispatcher>> = this
override val cannon: ActionCannon<CurrentPartyDispatcher> = ActionCannon(this, LoggingActionPipe(traceId))

suspend fun isAuthorized() = currentPartyId.validateAuthorized() != null

override val actionDispatcher = this
private suspend fun PartyId.validateAuthorized() = if (userIsAuthorized(this)) this else null

private suspend fun userIsAuthorized(partyId: PartyId) = currentUser.authorizedPartyIds.contains(partyId) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import com.zegreatrob.coupling.server.action.pairassignmentdocument.ShufflePairs
import com.zegreatrob.coupling.server.action.pairassignmentdocument.Wheel
import com.zegreatrob.testmints.action.ExecutableActionExecutor

interface PairAssignmentDispatcher :
ShufflePairsAction.Dispatcher,
interface PairAssignmentDispatcher<D> :
ShufflePairsAction.Dispatcher<D>,
FindNewPairsAction.Dispatcher,
NextPlayerAction.Dispatcher,
CreatePairCandidateReportListAction.Dispatcher,
CreatePairCandidateReportAction.Dispatcher,
Wheel {
override val execute: ExecutableActionExecutor<PairAssignmentDispatcher>
override val execute: ExecutableActionExecutor<PairAssignmentDispatcher<D>>
override val wheel: Wheel get() = this
}

0 comments on commit 22d8e71

Please sign in to comment.