@@ -2,32 +2,31 @@ import Foundation
2
2
import os. log
3
3
import SwiftRex
4
4
5
- extension Middleware where StateType: Equatable {
5
+ extension MiddlewareProtocol where StateType: Equatable {
6
6
public func logger(
7
- actionTransform: LoggerMiddleware < Self > . ActionTransform = . default( ) ,
8
- actionPrinter: LoggerMiddleware < Self > . ActionLogger = . osLog,
7
+ actionTransform: LoggerMiddleware < InputActionType , StateType > . ActionTransform = . default( ) ,
8
+ actionPrinter: LoggerMiddleware < InputActionType , StateType > . ActionLogger = . osLog,
9
9
actionFilter: @escaping ( InputActionType ) -> Bool = { _ in true } ,
10
- stateDiffTransform: LoggerMiddleware < Self > . StateDiffTransform = . diff( ) ,
11
- stateDiffPrinter: LoggerMiddleware < Self > . StateLogger = . osLog,
10
+ stateDiffTransform: LoggerMiddleware < InputActionType , StateType > . StateDiffTransform = . diff( ) ,
11
+ stateDiffPrinter: LoggerMiddleware < InputActionType , StateType > . StateLogger = . osLog,
12
12
queue: DispatchQueue = . main
13
- ) -> LoggerMiddleware < Self > {
13
+ ) -> ComposedMiddleware < InputActionType , OutputActionType , StateType > {
14
14
LoggerMiddleware (
15
- self ,
16
15
actionTransform: actionTransform,
17
16
actionPrinter: actionPrinter,
18
17
actionFilter: actionFilter,
19
18
stateDiffTransform: stateDiffTransform,
20
19
stateDiffPrinter: stateDiffPrinter,
21
20
queue: queue
22
- )
21
+ ) . lift ( outputAction : { ( _ : Never ) -> OutputActionType in } ) <> self
23
22
}
24
23
}
25
24
26
- public final class LoggerMiddleware < M : Middleware > : Middleware where M . StateType : Equatable {
27
- public typealias InputActionType = M . InputActionType
28
- public typealias OutputActionType = M . OutputActionType
29
- public typealias StateType = M . StateType
30
- private let middleware : M
25
+ public final class LoggerMiddleware < Action , State : Equatable > : MiddlewareProtocol {
26
+ public typealias InputActionType = Action
27
+ public typealias OutputActionType = Never
28
+ public typealias StateType = State
29
+
31
30
private let queue : DispatchQueue
32
31
private var getState : GetState < StateType > ?
33
32
private let actionTransform : ActionTransform
@@ -37,15 +36,13 @@ public final class LoggerMiddleware<M: Middleware>: Middleware where M.StateType
37
36
private let stateDiffPrinter : StateLogger
38
37
39
38
init (
40
- _ middleware: M ,
41
39
actionTransform: ActionTransform ,
42
40
actionPrinter: ActionLogger ,
43
41
actionFilter: @escaping ( InputActionType ) -> Bool = { _ in true } ,
44
42
stateDiffTransform: StateDiffTransform ,
45
43
stateDiffPrinter: StateLogger ,
46
44
queue: DispatchQueue
47
45
) {
48
- self . middleware = middleware
49
46
self . actionTransform = actionTransform
50
47
self . actionPrinter = actionPrinter
51
48
self . actionFilter = actionFilter
@@ -54,19 +51,11 @@ public final class LoggerMiddleware<M: Middleware>: Middleware where M.StateType
54
51
self . queue = queue
55
52
}
56
53
57
- public func receiveContext( getState: @escaping GetState < StateType > , output: AnyActionHandler < OutputActionType > ) {
58
- self . getState = getState
59
- middleware. receiveContext ( getState: getState, output: output)
60
- }
61
-
62
- public func handle( action: InputActionType , from dispatcher: ActionSource , afterReducer: inout AfterReducer ) {
63
- guard actionFilter ( action) else { return }
54
+ public func handle( action: Action , from dispatcher: ActionSource , state: @escaping GetState < State > ) -> IO < Never > {
55
+ guard actionFilter ( action) else { return . pure( ) }
64
56
let stateBefore = getState ? ( )
65
- var innerAfterReducer = AfterReducer . doNothing ( )
66
-
67
- middleware. handle ( action: action, from: dispatcher, afterReducer: & innerAfterReducer)
68
57
69
- afterReducer = innerAfterReducer <> . do { [ weak self] in
58
+ return IO { [ weak self] _ in
70
59
guard let self = self ,
71
60
let stateAfter = self . getState ? ( ) else { return }
72
61
@@ -83,15 +72,14 @@ public final class LoggerMiddleware<M: Middleware>: Middleware where M.StateType
83
72
84
73
extension LoggerMiddleware {
85
74
public static func `default`(
86
- actionTransform: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . ActionTransform = . default( ) ,
87
- actionPrinter: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . ActionLogger = . osLog,
75
+ actionTransform: LoggerMiddleware < InputActionType , StateType > . ActionTransform = . default( ) ,
76
+ actionPrinter: LoggerMiddleware < InputActionType , StateType > . ActionLogger = . osLog,
88
77
actionFilter: @escaping ( InputActionType ) -> Bool = { _ in true } ,
89
- stateDiffTransform: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . StateDiffTransform = . diff( ) ,
90
- stateDiffPrinter: LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > . StateLogger = . osLog,
78
+ stateDiffTransform: LoggerMiddleware < InputActionType , StateType > . StateDiffTransform = . diff( ) ,
79
+ stateDiffPrinter: LoggerMiddleware < InputActionType , StateType > . StateLogger = . osLog,
91
80
queue: DispatchQueue = . main
92
- ) -> LoggerMiddleware < IdentityMiddleware < InputActionType , OutputActionType , StateType > > {
81
+ ) -> LoggerMiddleware < InputActionType , StateType > {
93
82
. init(
94
- IdentityMiddleware ( ) ,
95
83
actionTransform: actionTransform,
96
84
actionPrinter: actionPrinter,
97
85
actionFilter: actionFilter,
0 commit comments