Skip to content

Commit

Permalink
Send cancellable actions from ActionDispatcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLambion committed Feb 7, 2020
1 parent 9b4848f commit 46d933d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 9 additions & 1 deletion Sources/SwiftDux/Action/ActionDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ public protocol ActionDispatcher {

extension ActionDispatcher {

/// Sends an action to a reducer to mutate the state of the application.
/// - Parameter action: An action to dispatch to the store.
public func callAsFunction(_ action: Action) {
send(action)
}


/// Send an action that returns a cancellable object.
/// - Parameter action: The action
/// - Returns: A cancellable to cancel the action.
public func sendAsCancellable(_ action: CancellableAction) -> AnyCancellable {
action.sendAsCancellable(self)
}
}
8 changes: 2 additions & 6 deletions Sources/SwiftDux/Action/ActionPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public struct ActionPlan<State>: CancellableAction where State: StateType {
///
/// - Parameter send: The send function that dispatches an action.
/// - Returns: AnyCancellable to cancel the action plan.
public func sendAsCancellable(_ send: ActionDispatcher) -> Cancellable {
public func sendAsCancellable(_ send: ActionDispatcher) -> AnyCancellable {
var publisherCancellable: AnyCancellable? = nil
send(
ActionPlan<State> { store, completed in
Expand All @@ -132,11 +132,7 @@ public struct ActionPlan<State>: CancellableAction where State: StateType {
return publisherCancellable
}
)

return AnyCancellable {
publisherCancellable?.cancel()
publisherCancellable = nil
}
return AnyCancellable { [publisherCancellable] in publisherCancellable?.cancel() }
}

/// Dispatches another action plan after this one has completed. This allows
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftDux/Action/CancellableAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ public protocol CancellableAction: Action {
///
/// - Parameter send: The send function that dispatches an action.
/// - Returns: AnyCancellable to cancel the action plan.
func sendAsCancellable(_ send: ActionDispatcher) -> Cancellable

func sendAsCancellable(_ send: ActionDispatcher) -> AnyCancellable
}

0 comments on commit 46d933d

Please sign in to comment.