Skip to content

Instantly share code, notes, and snippets.

@simme
Created April 13, 2023 15:12
Show Gist options
  • Save simme/5c4345e4f230a4ba96b0d873e23a7d5e to your computer and use it in GitHub Desktop.
Save simme/5c4345e4f230a4ba96b0d873e23a7d5e to your computer and use it in GitHub Desktop.
/*
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