@@ -16,8 +16,8 @@ public enum GateState: String, Codable, Equatable, Hashable {
1616///
1717/// Every gated middleware starts with an initial gate state, called "default gate state". From that point, it will evaluate all incoming actions to
1818/// detect a "control action", which is an action for switching on or off the gate state. This control action is detected thanks to a control action
19- /// map or a control map KeyPath configured in the GatedMiddleware's init, which from a given input action allows the user to inform either or not
20- /// this is a control action returning an Optional instance of that ControlAction (or nil in case it's a regular action).
19+ /// map closure or a control action map KeyPath configured in the GatedMiddleware's init, which from a given input action allows the user to inform
20+ /// or not either this is a control action returning an Optional instance of that ControlAction (or nil in case it's a regular action).
2121///
2222/// The init also requires some comparison values, for turnOn or turnOff the gate. If it's a control action, and it's equals to turn on, it will set
2323/// the inner middleware to active. If it's a control action, and it's equals to turn off, it will set the inner middleware to bypass. If it's not a
@@ -197,4 +197,33 @@ extension Middleware {
197197 ) -> GatedMiddleware < Self > {
198198 GatedMiddleware ( middleware: self , controlActionMap: controlActionMap, turnOn: turnOn, turnOff: turnOff, default: gateState)
199199 }
200+
201+ /// Gated middleware is a middleware that holds an inner middleware that could be either active or not. The gated middleware has an internal state,
202+ /// called `gate state`, that determines whether or not the inner middleware should be in `active` or `bypass` mode. This can be changed dynamically.
203+ ///
204+ /// Every gated middleware starts with an initial gate state, called "default gate state". From that point, it will evaluate all incoming actions to
205+ /// detect a "control action", which is an action for switching on or off the gate state. This control action is detected thanks to a control action
206+ /// map or a control map KeyPath configured in the GatedMiddleware's init, which from a given input action allows the user to inform either or not
207+ /// this is a control action returning an Optional instance of that ControlAction (or nil in case it's a regular action).
208+ ///
209+ /// The init also requires some comparison values, for turnOn or turnOff the gate. If it's a control action, and it's equals to turn on, it will set
210+ /// the inner middleware to active. If it's a control action, and it's equals to turn off, it will set the inner middleware to bypass. If it's not a
211+ /// control action, or it's not equals to any of the comparison values, the gate will remain untouched.
212+ ///
213+ /// There one last important topic. The gated middleware will ALWAYS forward control actions to inner middlewares, regardless of their gate state
214+ /// (active or bypass) and regardless of the turn on/turn off comparison result. This will allow important actions like disabling or enabling the
215+ /// inner middleware for control actions, so for example, even for when we close the gate we still want to tell the inner middleware that it's gonna
216+ /// be bypassed and it should kill all of its timers or async side-effects.
217+ /// - Parameters:
218+ /// - controlAction: a key-path that goes from incoming action to an optional Bool. It result is nil, it means this is not a control action.
219+ /// In case it has a non-nil Bool, this will enable (for `true`) or bypass (for `false`) the inner middleware. The inner
220+ /// middleware will also receive that control action regardless of its gate state.
221+ /// - gateState: initial `gateState`, either `active` or `bypass`
222+ /// - Returns: a `GatedMiddleware` containing internally this current middleware, allowing it to be bypassed or not.
223+ public func gated(
224+ controlActionMap: @escaping ( InputActionType ) -> Bool ? ,
225+ default gateState: GateState
226+ ) -> GatedMiddleware < Self > {
227+ GatedMiddleware ( middleware: self , controlActionMap: controlActionMap, turnOn: true , turnOff: false , default: gateState)
228+ }
200229}
0 commit comments