Created
April 13, 2023 15:12
-
-
Save simme/5c4345e4f230a4ba96b0d873e23a7d5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
Store( | |
initialState: .init(), | |
reducer: UserRecipesListReducer().relay { [unowned self] featureAction in | |
// Handle action here | |
} | |
) | |
*/ | |
extension Reducer { | |
@inlinable | |
public func relay(_ callback: @escaping (Action) -> Void) -> _RelayReducer<Self> { | |
_RelayReducer<Self>(base: self, callback: callback) | |
} | |
} | |
public struct _RelayReducer<Base: Reducer>: Reducer { | |
@usableFromInline | |
let base: Base | |
@usableFromInline | |
let callback: (Base.Action) -> Void | |
@usableFromInline | |
init(base: Base, callback: @escaping (Base.Action) -> Void) { | |
self.base = base | |
self.callback = callback | |
} | |
@inlinable | |
public func reduce(into state: inout Base.State, action: Base.Action) -> Effect<Base.Action> { | |
callback(action) | |
return self.base.reduce(into: &state, action: action) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment