Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional label that shows in the object description #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand All @@ -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:**
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ let thunk = Thunk<MyState> { dispatch, getState in

// A thunk can also be a function if you want to pass on parameters
func thunkWithParams(_ identifier: Int) -> Thunk<MyState> {
return Thunk<MyState> { dispatch, getState in
let label = "Getting something for ID \(identifier)"
return Thunk<MyState>(label: label) { dispatch, getState in
guard let state = getState() else { return }

if state.loading {
Expand Down
16 changes: 12 additions & 4 deletions ReSwift-Thunk/Thunk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
import Foundation
import ReSwift

public struct Thunk<State>: Action {
public struct Thunk<State>: 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))"
}
}

Expand Down
9 changes: 9 additions & 0 deletions ReSwift-ThunkTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ private func fakeReducer(action: Action, state: FakeState?) -> FakeState {

class Tests: XCTestCase {

func testDescription() {
XCTAssertEqual("Thunk<FakeState>(label: \"With Label\")",
Thunk<FakeState>(label: "With Label", body: { _, _ in }).description)
XCTAssertEqual("Thunk<FakeState>()",
Thunk<FakeState>(label: nil, body: { _, _ in }).description)
XCTAssertEqual("Thunk<FakeState>()",
Thunk<FakeState>(body: { _, _ in }).description)
}

func testAction() {
let middleware: Middleware<FakeState> = createThunkMiddleware()
let dispatch: DispatchFunction = { _ in }
Expand Down