From 6fdad87c18e7feec06272f53cfb856c1b44f04bb Mon Sep 17 00:00:00 2001 From: Luiz Rodrigo Martins Barbosa Date: Thu, 2 Jul 2020 20:48:21 +0200 Subject: [PATCH] Fix protocol conformance and improve ergonomics --- README.md | 4 +-- .../LoggerMiddleware/LoggerMiddleware.swift | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7360ed8..1aa2582 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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() ``` diff --git a/Sources/LoggerMiddleware/LoggerMiddleware.swift b/Sources/LoggerMiddleware/LoggerMiddleware.swift index 28c8b1c..2e8e843 100644 --- a/Sources/LoggerMiddleware/LoggerMiddleware.swift +++ b/Sources/LoggerMiddleware/LoggerMiddleware.swift @@ -21,7 +21,7 @@ extension Middleware where StateType: Equatable { } }, queue: DispatchQueue = .main - ) -> LoggerMiddleware { + ) -> LoggerMiddleware { LoggerMiddleware( self, actionTransform: actionTransform, @@ -33,8 +33,10 @@ extension Middleware where StateType: Equatable { } } -public final class LoggerMiddleware -where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputActionType == OutputActionType { +public final class LoggerMiddleware: 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? @@ -83,8 +85,8 @@ where M.StateType == StateType, M.InputActionType == InputActionType, M.OutputAc } } -extension LoggerMiddleware where M == IdentityMiddleware { - 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)" }, @@ -102,12 +104,14 @@ extension LoggerMiddleware where M == IdentityMiddleware LoggerMiddleware> { + .init( + IdentityMiddleware(), + actionTransform: actionTransform, + actionPrinter: actionPrinter, + stateDiffTransform: stateDiffTransform, + stateDiffPrinter: stateDiffPrinter, + queue: queue + ) } }