Skip to content

Commit 6fdad87

Browse files
committed
Fix protocol conformance and improve ergonomics
1 parent 120ff5b commit 6fdad87

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Simple usage, logging all actions and state changes for whole app
66
```swift
7-
LoggerMiddleware() <> MyOtherMiddleware().lift(...)
7+
LoggerMiddleware.default() <> MyOtherMiddleware().lift(...)
88
```
99

1010
### Log a single middleware, only actions and state within that middleware field
@@ -13,7 +13,7 @@ MyOtherMiddleware().logger().lift(...)
1313
```
1414

1515
### Log a single middleware, but including actions and state changes for the whole app
16-
(same as adding LoggerMiddleware in the chain as seen in the first option)
16+
(same as adding LoggerMiddleware.default() in the chain as seen in the first option)
1717
```swift
1818
MyOtherMiddleware().lift(...).logger()
1919
```

Sources/LoggerMiddleware/LoggerMiddleware.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension Middleware where StateType: Equatable {
2121
}
2222
},
2323
queue: DispatchQueue = .main
24-
) -> LoggerMiddleware<Self, InputActionType, OutputActionType, StateType> {
24+
) -> LoggerMiddleware<Self> {
2525
LoggerMiddleware(
2626
self,
2727
actionTransform: actionTransform,
@@ -33,8 +33,10 @@ extension Middleware where StateType: Equatable {
3333
}
3434
}
3535

36-
public final class LoggerMiddleware<M: Middleware, InputActionType, OutputActionType, StateType: Equatable>
37-
where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputActionType == OutputActionType {
36+
public final class LoggerMiddleware<M: Middleware>: Middleware where M.StateType: Equatable {
37+
public typealias InputActionType = M.InputActionType
38+
public typealias OutputActionType = M.OutputActionType
39+
public typealias StateType = M.StateType
3840
private let middleware: M
3941
private let queue: DispatchQueue
4042
private var getState: GetState<StateType>?
@@ -83,8 +85,8 @@ where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputAc
8385
}
8486
}
8587

86-
extension LoggerMiddleware where M == IdentityMiddleware<InputActionType, OutputActionType, StateType> {
87-
public convenience init(
88+
extension LoggerMiddleware {
89+
public static func `default`(
8890
actionTransform: @escaping (InputActionType, ActionSource) -> String = {
8991
"\n🕹 \($0)\n🎪 \($1.file.split(separator: "/").last ?? ""):\($1.line) \($1.function)"
9092
},
@@ -102,12 +104,14 @@ extension LoggerMiddleware where M == IdentityMiddleware<InputActionType, Output
102104
}
103105
},
104106
queue: DispatchQueue = .main
105-
) {
106-
self.init(IdentityMiddleware(),
107-
actionTransform: actionTransform,
108-
actionPrinter: actionPrinter,
109-
stateDiffTransform: stateDiffTransform,
110-
stateDiffPrinter: stateDiffPrinter,
111-
queue: queue)
107+
) -> LoggerMiddleware<IdentityMiddleware<InputActionType, OutputActionType, StateType>> {
108+
.init(
109+
IdentityMiddleware(),
110+
actionTransform: actionTransform,
111+
actionPrinter: actionPrinter,
112+
stateDiffTransform: stateDiffTransform,
113+
stateDiffPrinter: stateDiffPrinter,
114+
queue: queue
115+
)
112116
}
113117
}

0 commit comments

Comments
 (0)