diff --git a/CHANGELOG.md b/CHANGELOG.md index 42ddb55..c8c87dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ **Breaking API Changes:** **API Changes:** +- Add optional `label` property to `Thunk` that is used for the object `description`, e.g for logging/printing the thunk object (#50) - @DivineDominion **Fixes:** @@ -21,7 +22,7 @@ **Other:** - Rename SwiftPM library to `ReSwiftThunk`, this makes naming consistent across all package manager (Cocoapods, Carthage and SwiftPM) (#42) - @jookes - `ExpectThunk`'s methods `dispatches` and `getsState` no longer have `@discardableResult` return values (#40) - @xavierLowmiller -- + # 1.2.0 **API Changes:** diff --git a/README.md b/README.md index bc159cc..5a5aef2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,8 @@ let thunk = Thunk { dispatch, getState in // A thunk can also be a function if you want to pass on parameters func thunkWithParams(_ identifier: Int) -> Thunk { - return Thunk { dispatch, getState in + let label = "Getting something for ID \(identifier)" + return Thunk(label: label) { dispatch, getState in guard let state = getState() else { return } if state.loading { diff --git a/ReSwift-Thunk/Thunk.swift b/ReSwift-Thunk/Thunk.swift index f337860..cd01a0d 100644 --- a/ReSwift-Thunk/Thunk.swift +++ b/ReSwift-Thunk/Thunk.swift @@ -9,12 +9,20 @@ import Foundation import ReSwift -public struct Thunk: Action { +public struct Thunk: Action, CustomStringConvertible { let body: (_ dispatch: @escaping DispatchFunction, _ getState: @escaping () -> State?) -> Void - public init(body: @escaping ( - _ dispatch: @escaping DispatchFunction, - _ getState: @escaping () -> State?) -> Void) { + public let label: String? + + public init(label: String? = nil, + body: @escaping (_ dispatch: @escaping DispatchFunction, + _ getState: @escaping () -> State?) -> Void) { self.body = body + self.label = label + } + + public var description: String { + let label = self.label.map { "label: \"\($0)\"" } ?? "" + return "\(type(of: self))(\(label))" } } diff --git a/ReSwift-ThunkTests/Tests.swift b/ReSwift-ThunkTests/Tests.swift index c73a31e..bbab87e 100644 --- a/ReSwift-ThunkTests/Tests.swift +++ b/ReSwift-ThunkTests/Tests.swift @@ -20,6 +20,15 @@ private func fakeReducer(action: Action, state: FakeState?) -> FakeState { class Tests: XCTestCase { + func testDescription() { + XCTAssertEqual("Thunk(label: \"With Label\")", + Thunk(label: "With Label", body: { _, _ in }).description) + XCTAssertEqual("Thunk()", + Thunk(label: nil, body: { _, _ in }).description) + XCTAssertEqual("Thunk()", + Thunk(body: { _, _ in }).description) + } + func testAction() { let middleware: Middleware = createThunkMiddleware() let dispatch: DispatchFunction = { _ in }