From 6be0287c39bccc268709ee8d3781b2085e334503 Mon Sep 17 00:00:00 2001 From: Anders Ha Date: Thu, 15 Dec 2016 03:06:05 +0100 Subject: [PATCH 1/3] Make `Action` retain its state property. --- Sources/Action.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/Action.swift b/Sources/Action.swift index 1e047816e..664fa58a5 100644 --- a/Sources/Action.swift +++ b/Sources/Action.swift @@ -78,6 +78,9 @@ public final class Action { public init(state property: State, enabledIf isEnabled: @escaping (State.Value) -> Bool, _ execute: @escaping (State.Value, Input) -> SignalProducer) { deinitToken = Lifetime.Token() lifetime = Lifetime(deinitToken) + + // Retain the `property` for the created `Action`. + lifetime.ended.observeCompleted { _ = property } executeClosure = { state, input in execute(state as! State.Value, input) } From 50b14c8d8876334973e2202e75f1f4cf45dc76f9 Mon Sep 17 00:00:00 2001 From: Anders Ha Date: Thu, 15 Dec 2016 03:27:10 +0100 Subject: [PATCH 2/3] Added an `Action` test case. --- Tests/ReactiveSwiftTests/ActionSpec.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Tests/ReactiveSwiftTests/ActionSpec.swift b/Tests/ReactiveSwiftTests/ActionSpec.swift index 652924709..ca6dfa66f 100755 --- a/Tests/ReactiveSwiftTests/ActionSpec.swift +++ b/Tests/ReactiveSwiftTests/ActionSpec.swift @@ -58,6 +58,23 @@ class ActionSpec: QuickSpec { action.errors.observeValues { errors.append($0) } action.completed.observeValues { completedCount += 1 } } + + it("should retain the state property") { + var property: MutableProperty? = MutableProperty(false) + weak var weakProperty = property + + var action: Action<(), (), NoError>? = Action(state: property!, enabledIf: { _ in true }) { _ in + return .empty + } + + expect(weakProperty).toNot(beNil()) + + property = nil + expect(weakProperty).toNot(beNil()) + + action = nil + expect(weakProperty).to(beNil()) + } it("should be disabled and not executing after initialization") { expect(action.isEnabled.value) == false From 4c0d805173bc54a9875cbebd423ffd650dc424a3 Mon Sep 17 00:00:00 2001 From: Anders Ha Date: Thu, 15 Dec 2016 04:27:51 +0100 Subject: [PATCH 3/3] Mute "unused variable" warning in ActionSpec. --- Tests/ReactiveSwiftTests/ActionSpec.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/ReactiveSwiftTests/ActionSpec.swift b/Tests/ReactiveSwiftTests/ActionSpec.swift index ca6dfa66f..d739fa26b 100755 --- a/Tests/ReactiveSwiftTests/ActionSpec.swift +++ b/Tests/ReactiveSwiftTests/ActionSpec.swift @@ -74,6 +74,9 @@ class ActionSpec: QuickSpec { action = nil expect(weakProperty).to(beNil()) + + // Mute "unused variable" warning. + _ = action } it("should be disabled and not executing after initialization") {