11import Combine
22
33extension Store {
4- /// Subscribes to updates when a store containing optional state goes from `nil` to non-`nil` or
5- /// non-`nil` to `nil` .
4+ /// Calls one of two closures depending on whether a store's optional state is `nil` or not, and
5+ /// whenever this condition changes for as long as the cancellable lives .
66 ///
7- /// **NOTE:** one of the `unwrap` or `else` closures is always called based on the *initial* state of the
8- /// optional state (`nil` or non-`nil`), in addition to subsequent changes of `nil` / non-`nil`.
7+ /// If the store's state is non-`nil`, it will safely unwrap the value and bundle it into a new
8+ /// store of non-optional state that is passed to the first closure. If the store's state is
9+ /// `nil`, the second closure is called instead.
910 ///
10- /// This is useful for handling navigation in UIKit. The state for a screen that you want to
11- /// navigate to can be held as an optional value in the parent, and when that value switches
12- /// from `nil` to non-`nil` you want to trigger a navigation and hand the detail view a `Store`
13- /// whose domain has been scoped to just that feature:
11+ /// This method is useful for handling navigation in UIKit. The state for a screen the user wants
12+ /// to navigate to can be held as an optional value in the parent, and when that value goes from
13+ /// `nil` to non-`nil`, or non-`nil` to `nil`, you can update the navigation stack accordingly:
1414 ///
15- /// class MasterViewController : UIViewController {
16- /// let store: Store<MasterState, MasterAction >
15+ /// class ParentViewController : UIViewController {
16+ /// let store: Store<ParentState, ParentAction >
1717 /// var cancellables: Set<AnyCancellable> = []
1818 /// ...
1919 /// func viewDidLoad() {
2020 /// ...
2121 /// self.store
22- /// .scope(state: \.optionalDetail , action: MasterAction.detail )
22+ /// .scope(state: \.optionalChild , action: ParentAction.child )
2323 /// .ifLet(
24- /// then: { [weak self] detailStore in
24+ /// then: { [weak self] childStore in
2525 /// self?.navigationController?.pushViewController(
26- /// DetailViewController (store: detailStore ),
26+ /// ChildViewController (store: childStore ),
2727 /// animated: true
2828 /// )
2929 /// },
@@ -37,15 +37,15 @@ extension Store {
3737 /// }
3838 ///
3939 /// - Parameters:
40- /// - unwrap: A function that is called with a store of non-optional state whenever the store's
41- /// optional state is initially non-`nil` or goes from `nil` to non-`nil`.
42- /// - else: A function that is called whenever the store's optional state is initially `nil` or
40+ /// - unwrap: A function that is called with a store of non-optional state when the store's
41+ /// state is non-`nil`, or whenever it goes from `nil` to non-`nil`.
42+ /// - else: A function that is called when the store's optional state is `nil`, or whenever it
4343 /// goes from non-`nil` to `nil`.
44-
45- /// - Returns: A cancellable associated with the underlying subscription .
44+ /// - Returns: A cancellable that maintains a subscription to updates whenever the store's state
45+ /// goes from `nil` to non-`nil` and vice versa, so that the caller can react to these changes .
4646 public func ifLet< Wrapped> (
4747 then unwrap: @escaping ( Store < Wrapped , Action > ) -> Void ,
48- else: @escaping ( ) -> Void
48+ else: @escaping ( ) -> Void = { }
4949 ) -> Cancellable where State == Wrapped ? {
5050 let elseCancellable =
5151 self
@@ -75,16 +75,4 @@ extension Store {
7575 unwrapCancellable. cancel ( )
7676 }
7777 }
78-
79- /// An overload of `ifLet(then:else:)` for the times that you do not want to handle the `else`
80- /// case.
81- ///
82- /// - Parameter unwrap: A function that is called with a store of non-optional state whenever the
83- /// store's optional state is initially non-`nil` or goes from `nil` to non-`nil`.
84- /// - Returns: A cancellable associated with the underlying subscription.
85- public func ifLet< Wrapped> (
86- then unwrap: @escaping ( Store < Wrapped , Action > ) -> Void
87- ) -> Cancellable where State == Wrapped ? {
88- self . ifLet ( then: unwrap, else: { } )
89- }
9078}
0 commit comments