Skip to content

Commit 24d101e

Browse files
committed
Add concurrency task lifecycle binding to interactor
1 parent e795e9d commit 24d101e

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Sources/IMVVM/AnyCancellable+Interactor.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// SOFTWARE.
2222

2323
import Combine
24+
import Foundation
2425

2526
extension AnyCancellable {
2627
/// When the interactor deinits, the subscription is cancelled.

Sources/IMVVM/Task+Interactor.swift

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Yi Wang
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import Combine
24+
import Foundation
25+
26+
extension Task: Cancellable {}
27+
28+
extension Task {
29+
/// When the interactor deinits, the task is cancelled.
30+
///
31+
/// This function provides the utility to manage Task lifecycle inside a `Interactor` implementation. For
32+
/// example:
33+
///
34+
/// class MyInteractor: Interactor {
35+
/// func buttonDidTap() {
36+
/// Task {
37+
/// ...
38+
/// }
39+
/// .cancelOnDeinit(of: self)
40+
/// }
41+
/// }
42+
///
43+
/// - Note: Because this function causes the given interactor to stongly retain the task, this means the task
44+
/// itself should not strongly retain the interactor. Otherwise a retain cycle would occur causing memory leaks.
45+
///
46+
/// This function is thread-safe. Invocations of this function to the same interactor instance can be performed on
47+
/// the different threads.
48+
///
49+
/// This function can only be invoked after the given interactor has loaded. This is done via the interactor's
50+
/// `viewDidAppear` function. Generally speaking, this interactor should be bound to the lifecycle of a `View`.
51+
/// See `ViewLifecycleObserver` for more details.
52+
///
53+
/// - Parameters:
54+
/// - interactor: The interactor to bind the task's lifecycle to.
55+
public func cancelOnDeinit<InteractorType: Interactor>(of interactor: InteractorType) {
56+
if !interactor.isLoaded {
57+
fatalError("\(interactor) has not been loaded")
58+
}
59+
interactor.deinitCancelBag.store(AnyCancellable(self))
60+
}
61+
62+
/// When the interactor's view disappears, the task is cancelled.
63+
///
64+
/// This function provides the utility to manage Task lifecycle inside a `Interactor` implementation. For
65+
/// example:
66+
///
67+
/// class MyInteractor: Interactor {
68+
/// func buttonDidTap() {
69+
/// Task {
70+
/// ...
71+
/// }
72+
/// .cancelOnViewDidDisappear(of: self)
73+
/// }
74+
/// }
75+
///
76+
/// - Note: Because this function causes the given interactor to stongly retain the task, this means the task
77+
/// itself should not strongly retain the interactor. Otherwise a retain cycle would occur causing memory leaks.
78+
///
79+
/// This function is thread-safe. Invocations of this function to the same interactor instance can be performed on
80+
/// the different threads.
81+
///
82+
/// This function can only be invoked after the given interactor has received notification that its view has
83+
/// appeared. This is done via the interactor's `viewDidAppear` function. Generally speaking, this interactor
84+
/// should be bound to the lifecycle of a `View`. See `ViewLifecycleObserver` for more details.
85+
///
86+
/// - Parameters:
87+
/// - interactor: The interactor to bind the task's lifecycle to.
88+
public func cancelOnViewDidDisappear<InteractorType: Interactor>(of interactor: InteractorType) {
89+
if !interactor.hasViewAppeared {
90+
fatalError("\(interactor)'s view has not appeared")
91+
}
92+
interactor.viewAppearanceCancelBag.store(AnyCancellable(self))
93+
}
94+
}

0 commit comments

Comments
 (0)