Skip to content

Commit

Permalink
Fix protocol conformance and improve ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
luizmb committed Jul 2, 2020
1 parent 120ff5b commit 6fdad87
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Simple usage, logging all actions and state changes for whole app
```swift
LoggerMiddleware() <> MyOtherMiddleware().lift(...)
LoggerMiddleware.default() <> MyOtherMiddleware().lift(...)
```

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

### Log a single middleware, but including actions and state changes for the whole app
(same as adding LoggerMiddleware in the chain as seen in the first option)
(same as adding LoggerMiddleware.default() in the chain as seen in the first option)
```swift
MyOtherMiddleware().lift(...).logger()
```
Expand Down
28 changes: 16 additions & 12 deletions Sources/LoggerMiddleware/LoggerMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension Middleware where StateType: Equatable {
}
},
queue: DispatchQueue = .main
) -> LoggerMiddleware<Self, InputActionType, OutputActionType, StateType> {
) -> LoggerMiddleware<Self> {
LoggerMiddleware(
self,
actionTransform: actionTransform,
Expand All @@ -33,8 +33,10 @@ extension Middleware where StateType: Equatable {
}
}

public final class LoggerMiddleware<M: Middleware, InputActionType, OutputActionType, StateType: Equatable>
where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputActionType == OutputActionType {
public final class LoggerMiddleware<M: Middleware>: Middleware where M.StateType: Equatable {
public typealias InputActionType = M.InputActionType
public typealias OutputActionType = M.OutputActionType
public typealias StateType = M.StateType
private let middleware: M
private let queue: DispatchQueue
private var getState: GetState<StateType>?
Expand Down Expand Up @@ -83,8 +85,8 @@ where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputAc
}
}

extension LoggerMiddleware where M == IdentityMiddleware<InputActionType, OutputActionType, StateType> {
public convenience init(
extension LoggerMiddleware {
public static func `default`(
actionTransform: @escaping (InputActionType, ActionSource) -> String = {
"\n🕹 \($0)\n🎪 \($1.file.split(separator: "/").last ?? ""):\($1.line) \($1.function)"
},
Expand All @@ -102,12 +104,14 @@ extension LoggerMiddleware where M == IdentityMiddleware<InputActionType, Output
}
},
queue: DispatchQueue = .main
) {
self.init(IdentityMiddleware(),
actionTransform: actionTransform,
actionPrinter: actionPrinter,
stateDiffTransform: stateDiffTransform,
stateDiffPrinter: stateDiffPrinter,
queue: queue)
) -> LoggerMiddleware<IdentityMiddleware<InputActionType, OutputActionType, StateType>> {
.init(
IdentityMiddleware(),
actionTransform: actionTransform,
actionPrinter: actionPrinter,
stateDiffTransform: stateDiffTransform,
stateDiffPrinter: stateDiffPrinter,
queue: queue
)
}
}

0 comments on commit 6fdad87

Please sign in to comment.