Skip to content

Commit 288e112

Browse files
committed
changes middleware api to include an initialization phase
* middlware that wants to support long running processes - across multiple actions - can be written easier this way NOTE * introduces an issue that has been already discussed here: reduxjs/redux#1240
1 parent e9fed56 commit 288e112

File tree

6 files changed

+37
-11
lines changed
  • modules

6 files changed

+37
-11
lines changed

modules/conflate-api/src/main/kotlin/com/encodeering/conflate/api/Middleware.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.encodeering.conflate.api
22

33
interface Middleware<in State> {
44

5-
suspend fun dispatch (action : Action, connection : Connection<State>)
5+
fun interceptor (connection : Connection<State>) : Interceptor
66

77
interface Connection<out State> {
88

@@ -14,4 +14,10 @@ interface Middleware<in State> {
1414

1515
}
1616

17+
interface Interceptor {
18+
19+
suspend fun dispatch (action : Action)
20+
21+
}
22+
1723
}

modules/conflate-logging/src/main/kotlin/com/encodeering/conflate/logging/Logging.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ class Logging<in State> (
1515
val exception : () -> Boolean = { true }
1616
) : Middleware<State> {
1717

18-
suspend override fun dispatch (action : Action, connection : Middleware.Connection<State>) {
18+
override fun interceptor (connection : Middleware.Connection<State>) : Middleware.Interceptor {
19+
return object : Middleware.Interceptor {
20+
21+
suspend override fun dispatch (action : Action) {
1922
if (before ()) debug (">>", action)
2023

2124
try {
@@ -27,10 +30,13 @@ class Logging<in State> (
2730

2831
throw e
2932
}
30-
}
33+
}
3134

3235
private fun debug (prefix : CharSequence, action : Action) = log ("$prefix {}", action)
3336

37+
}
38+
}
39+
3440
companion object {
3541

3642
private val logger : Logger by lazy { LoggerFactory.getLogger (Logging::class.java) }

modules/conflate-logging/src/test/kotlin/com/encodeering/conflate/logging/LoggingTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class LoggingTest : Spek({
4040
val logging = logging (before = true, log = log)
4141

4242
co {
43-
logging.dispatch (action, connection ())
43+
logging.interceptor (connection ()).dispatch (action)
4444

4545
verify (log).invoke (">> {}", action)
4646
verifyNoMoreInteractions (log)
@@ -54,7 +54,7 @@ class LoggingTest : Spek({
5454
val logging = logging (after = true, log = log)
5555

5656
co {
57-
logging.dispatch (action, connection ())
57+
logging.interceptor (connection ()).dispatch (action)
5858

5959
verify (log).invoke ("-- {}", action)
6060
verifyNoMoreInteractions (log)
@@ -70,7 +70,7 @@ class LoggingTest : Spek({
7070
throws<IllegalStateException> {
7171
co {
7272
try {
73-
logging.dispatch (action, connection (next = { throw IllegalStateException () }))
73+
logging.interceptor (connection (next = { throw IllegalStateException () })).dispatch (action)
7474
} finally {
7575
verify (log).invoke ("!! {}", action)
7676
verifyNoMoreInteractions (log)
@@ -86,7 +86,7 @@ class LoggingTest : Spek({
8686
val logging = logging (exception = true, log = log ())
8787

8888
co {
89-
logging.dispatch (action, connection (next = next))
89+
logging.interceptor (connection (next = next)).dispatch (action)
9090

9191
verify (next).invoke (action)
9292
}
@@ -100,7 +100,7 @@ class LoggingTest : Spek({
100100
val logging = logging (before = true, after = true, log = log)
101101

102102
co {
103-
logging.dispatch (action, connection (next = next))
103+
logging.interceptor (connection (next = next)).dispatch (action)
104104

105105
val ordered = Mockito.inOrder (log, next)
106106
ordered.verify (log).invoke (eq (">> {}"), eq (action))

modules/conflate-test/src/main/kotlin/com/encodeering/conflate/test/fixture/Middlewares.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ object Middlewares {
2727
after : (Action, Middleware.Connection<T>) -> Unit = { _, _ -> Unit }) =
2828
object : Middleware<T> {
2929

30-
suspend override fun dispatch (action : Action, connection : Middleware.Connection<T>) {
30+
override fun interceptor(connection : Middleware.Connection<T>) : Middleware.Interceptor {
31+
return object : Middleware.Interceptor {
32+
33+
suspend override fun dispatch (action : Action) {
3134
before (action, connection)
3235
connection.next (action)
3336
after (action, connection)
37+
}
38+
39+
}
3440
}
3541

3642
}

modules/conflate/src/main/kotlin/com/encodeering/conflate/Conflate.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class Conflate<out State> (
7272

7373
private class Next<out State> (private var resolve : () -> State, private val dispatch : suspend (Action) -> Unit, private val middleware : Middleware<State>, private val next : Middleware.Connection<State>) : Middleware.Connection<State> {
7474

75+
val interceptor = middleware.interceptor (next)
76+
7577
override val state : State
7678
get () = resolve ()
7779

@@ -80,7 +82,7 @@ class Conflate<out State> (
8082
}
8183

8284
suspend override fun next (action : Action) {
83-
middleware.dispatch (action, next)
85+
interceptor.dispatch (action)
8486
}
8587

8688
}

modules/conflate/src/main/kotlin/com/encodeering/conflate/middleware/Codeblock.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import com.encodeering.conflate.api.Middleware
55

66
internal class Codeblock<in State> (private val block : (Action, State) -> Unit) : Middleware<State> {
77

8-
suspend override fun dispatch (action : Action, connection : Middleware.Connection<State>) {
8+
override fun interceptor(connection : Middleware.Connection<State>) : Middleware.Interceptor {
9+
return object : Middleware.Interceptor {
10+
11+
suspend override fun dispatch (action : Action) {
912
connection.apply {
1013
block (action, connection.state)
1114
next (action)
15+
}
16+
}
17+
1218
}
1319
}
1420

0 commit comments

Comments
 (0)