diff --git a/Sources/SwiftDux/Action/ActionDispatcher.swift b/Sources/SwiftDux/Action/ActionDispatcher.swift index 3c330ac..300ce05 100644 --- a/Sources/SwiftDux/Action/ActionDispatcher.swift +++ b/Sources/SwiftDux/Action/ActionDispatcher.swift @@ -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) + } } diff --git a/Sources/SwiftDux/Action/ActionPlan.swift b/Sources/SwiftDux/Action/ActionPlan.swift index 5b1003c..94b6a4e 100644 --- a/Sources/SwiftDux/Action/ActionPlan.swift +++ b/Sources/SwiftDux/Action/ActionPlan.swift @@ -121,7 +121,7 @@ public struct ActionPlan: 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 { store, completed in @@ -132,11 +132,7 @@ public struct ActionPlan: 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 diff --git a/Sources/SwiftDux/Action/CancellableAction.swift b/Sources/SwiftDux/Action/CancellableAction.swift index 109087a..8451846 100644 --- a/Sources/SwiftDux/Action/CancellableAction.swift +++ b/Sources/SwiftDux/Action/CancellableAction.swift @@ -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 }