diff --git a/modules/conflate-api/src/main/kotlin/com/encodeering/conflate/api/Middleware.kt b/modules/conflate-api/src/main/kotlin/com/encodeering/conflate/api/Middleware.kt index 611b7ce..0baab4d 100644 --- a/modules/conflate-api/src/main/kotlin/com/encodeering/conflate/api/Middleware.kt +++ b/modules/conflate-api/src/main/kotlin/com/encodeering/conflate/api/Middleware.kt @@ -2,7 +2,7 @@ package com.encodeering.conflate.api interface Middleware { - suspend fun dispatch (action : Action, connection : Connection) + fun interceptor (connection : Connection) : Interceptor interface Connection { @@ -14,4 +14,10 @@ interface Middleware { } + interface Interceptor { + + suspend fun dispatch (action : Action) + + } + } \ No newline at end of file diff --git a/modules/conflate-logging/src/main/kotlin/com/encodeering/conflate/logging/Logging.kt b/modules/conflate-logging/src/main/kotlin/com/encodeering/conflate/logging/Logging.kt index 53520a6..9ea6a57 100644 --- a/modules/conflate-logging/src/main/kotlin/com/encodeering/conflate/logging/Logging.kt +++ b/modules/conflate-logging/src/main/kotlin/com/encodeering/conflate/logging/Logging.kt @@ -15,7 +15,10 @@ class Logging ( val exception : () -> Boolean = { true } ) : Middleware { - suspend override fun dispatch (action : Action, connection : Middleware.Connection) { + override fun interceptor (connection : Middleware.Connection) : Middleware.Interceptor { + return object : Middleware.Interceptor { + + suspend override fun dispatch (action : Action) { if (before ()) debug (">>", action) try { @@ -27,10 +30,13 @@ class Logging ( throw e } - } + } private fun debug (prefix : CharSequence, action : Action) = log ("$prefix {}", action) + } + } + companion object { private val logger : Logger by lazy { LoggerFactory.getLogger (Logging::class.java) } diff --git a/modules/conflate-logging/src/test/kotlin/com/encodeering/conflate/logging/LoggingTest.kt b/modules/conflate-logging/src/test/kotlin/com/encodeering/conflate/logging/LoggingTest.kt index 0ea74f9..9ee042c 100644 --- a/modules/conflate-logging/src/test/kotlin/com/encodeering/conflate/logging/LoggingTest.kt +++ b/modules/conflate-logging/src/test/kotlin/com/encodeering/conflate/logging/LoggingTest.kt @@ -40,7 +40,7 @@ class LoggingTest : Spek({ val logging = logging (before = true, log = log) co { - logging.dispatch (action, connection ()) + logging.interceptor (connection ()).dispatch (action) verify (log).invoke (">> {}", action) verifyNoMoreInteractions (log) @@ -54,7 +54,7 @@ class LoggingTest : Spek({ val logging = logging (after = true, log = log) co { - logging.dispatch (action, connection ()) + logging.interceptor (connection ()).dispatch (action) verify (log).invoke ("-- {}", action) verifyNoMoreInteractions (log) @@ -70,7 +70,7 @@ class LoggingTest : Spek({ throws { co { try { - logging.dispatch (action, connection (next = { throw IllegalStateException () })) + logging.interceptor (connection (next = { throw IllegalStateException () })).dispatch (action) } finally { verify (log).invoke ("!! {}", action) verifyNoMoreInteractions (log) @@ -86,7 +86,7 @@ class LoggingTest : Spek({ val logging = logging (exception = true, log = log ()) co { - logging.dispatch (action, connection (next = next)) + logging.interceptor (connection (next = next)).dispatch (action) verify (next).invoke (action) } @@ -100,7 +100,7 @@ class LoggingTest : Spek({ val logging = logging (before = true, after = true, log = log) co { - logging.dispatch (action, connection (next = next)) + logging.interceptor (connection (next = next)).dispatch (action) val ordered = Mockito.inOrder (log, next) ordered.verify (log).invoke (eq (">> {}"), eq (action)) diff --git a/modules/conflate-test/src/main/kotlin/com/encodeering/conflate/test/fixture/Middlewares.kt b/modules/conflate-test/src/main/kotlin/com/encodeering/conflate/test/fixture/Middlewares.kt index d3efd0c..06cd22f 100644 --- a/modules/conflate-test/src/main/kotlin/com/encodeering/conflate/test/fixture/Middlewares.kt +++ b/modules/conflate-test/src/main/kotlin/com/encodeering/conflate/test/fixture/Middlewares.kt @@ -27,10 +27,16 @@ object Middlewares { after : (Action, Middleware.Connection) -> Unit = { _, _ -> Unit }) = object : Middleware { - suspend override fun dispatch (action : Action, connection : Middleware.Connection) { + override fun interceptor(connection : Middleware.Connection) : Middleware.Interceptor { + return object : Middleware.Interceptor { + + suspend override fun dispatch (action : Action) { before (action, connection) connection.next (action) after (action, connection) + } + + } } } diff --git a/modules/conflate/src/main/kotlin/com/encodeering/conflate/Conflate.kt b/modules/conflate/src/main/kotlin/com/encodeering/conflate/Conflate.kt index 3729025..05c6004 100644 --- a/modules/conflate/src/main/kotlin/com/encodeering/conflate/Conflate.kt +++ b/modules/conflate/src/main/kotlin/com/encodeering/conflate/Conflate.kt @@ -72,6 +72,8 @@ class Conflate ( private class Next (private var resolve : () -> State, private val dispatch : suspend (Action) -> Unit, private val middleware : Middleware, private val next : Middleware.Connection) : Middleware.Connection { + val interceptor = middleware.interceptor (next) + override val state : State get () = resolve () @@ -80,7 +82,7 @@ class Conflate ( } suspend override fun next (action : Action) { - middleware.dispatch (action, next) + interceptor.dispatch (action) } } diff --git a/modules/conflate/src/main/kotlin/com/encodeering/conflate/middleware/Codeblock.kt b/modules/conflate/src/main/kotlin/com/encodeering/conflate/middleware/Codeblock.kt index db05a75..b656843 100644 --- a/modules/conflate/src/main/kotlin/com/encodeering/conflate/middleware/Codeblock.kt +++ b/modules/conflate/src/main/kotlin/com/encodeering/conflate/middleware/Codeblock.kt @@ -5,10 +5,16 @@ import com.encodeering.conflate.api.Middleware internal class Codeblock (private val block : (Action, State) -> Unit) : Middleware { - suspend override fun dispatch (action : Action, connection : Middleware.Connection) { + override fun interceptor(connection : Middleware.Connection) : Middleware.Interceptor { + return object : Middleware.Interceptor { + + suspend override fun dispatch (action : Action) { connection.apply { block (action, connection.state) next (action) + } + } + } }