Skip to content

Commit 253a8d9

Browse files
authored
Merge pull request #155 from ReactiveCocoa/action-retaining
Make `Action` retain its state property.
2 parents 0f2fc54 + 4c0d805 commit 253a8d9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Sources/Action.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public final class Action<Input, Output, Error: Swift.Error> {
7878
public init<State: PropertyProtocol>(state property: State, enabledIf isEnabled: @escaping (State.Value) -> Bool, _ execute: @escaping (State.Value, Input) -> SignalProducer<Output, Error>) {
7979
deinitToken = Lifetime.Token()
8080
lifetime = Lifetime(deinitToken)
81+
82+
// Retain the `property` for the created `Action`.
83+
lifetime.ended.observeCompleted { _ = property }
8184

8285
executeClosure = { state, input in execute(state as! State.Value, input) }
8386

Tests/ReactiveSwiftTests/ActionSpec.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ class ActionSpec: QuickSpec {
5858
action.errors.observeValues { errors.append($0) }
5959
action.completed.observeValues { completedCount += 1 }
6060
}
61+
62+
it("should retain the state property") {
63+
var property: MutableProperty<Bool>? = MutableProperty(false)
64+
weak var weakProperty = property
65+
66+
var action: Action<(), (), NoError>? = Action(state: property!, enabledIf: { _ in true }) { _ in
67+
return .empty
68+
}
69+
70+
expect(weakProperty).toNot(beNil())
71+
72+
property = nil
73+
expect(weakProperty).toNot(beNil())
74+
75+
action = nil
76+
expect(weakProperty).to(beNil())
77+
78+
// Mute "unused variable" warning.
79+
_ = action
80+
}
6181

6282
it("should be disabled and not executing after initialization") {
6383
expect(action.isEnabled.value) == false

0 commit comments

Comments
 (0)