Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public final class Action<Input, Output, Error: Swift.Error> {
public init<State: PropertyProtocol>(state property: State, enabledIf isEnabled: @escaping (State.Value) -> Bool, _ execute: @escaping (State.Value, Input) -> SignalProducer<Output, Error>) {
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) }

Expand Down
20 changes: 20 additions & 0 deletions Tests/ReactiveSwiftTests/ActionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ class ActionSpec: QuickSpec {
action.errors.observeValues { errors.append($0) }
action.completed.observeValues { completedCount += 1 }
}

it("should retain the state property") {
var property: MutableProperty<Bool>? = MutableProperty(false)
weak var weakProperty = property

var action: Action<(), (), NoError>? = Action(state: property!, enabledIf: { _ in true }) { _ in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is triggering a "variable is written to but never read" warning. Mind changing this to a let constant? Probably doesn't need to be optional either.

Copy link
Member Author

@andersio andersio Dec 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of the test case. action is set to nil below to test if the property is really retained by the Action.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

return .empty
}

expect(weakProperty).toNot(beNil())

property = nil
expect(weakProperty).toNot(beNil())

action = nil
expect(weakProperty).to(beNil())

// Mute "unused variable" warning.
_ = action
}

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