Skip to content

Commit 84b50e9

Browse files
committed
Disable output actions when bypassing the inner middleware
1 parent d9575b3 commit 84b50e9

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Sources/GatedMiddleware/GatedMiddleware.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ public final class GatedMiddleware<M: Middleware>: Middleware {
9595
/// unwrapped versions of `getState` and `output` as properties of your concrete middleware, and set them from the arguments of this function.
9696
///
9797
/// This will be always forwarded to the inner middleware regardless of its gate state, another reason for you to never start side-effects on this
98-
/// event
98+
/// event. However, this is proxied by the gated middleware, and output will only be forwarded to the store in case the gate state is active.
9999
///
100100
/// - Parameters:
101101
/// - getState: a closure that allows the middleware to read the current state at any point in time
102102
/// - output: an action handler that allows the middleware to dispatch new actions at any point in time
103103
public func receiveContext(getState: @escaping GetState<M.StateType>, output: AnyActionHandler<M.OutputActionType>) {
104-
middleware.receiveContext(getState: getState, output: output)
104+
middleware.receiveContext(getState: getState, output: .init { [weak self] outputAction, source in
105+
guard let self = self, self.gate == .active else { return }
106+
output.dispatch(outputAction, from: source)
107+
})
105108
}
106109

107110
/// Handles the incoming actions and may or not start async tasks, check the latest state at any point or dispatch additional actions.

0 commit comments

Comments
 (0)