Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite feedback loop between Action states #221

Merged
merged 2 commits into from
Jan 13, 2017
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
4 changes: 2 additions & 2 deletions Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public final class Action<Input, Output, Error: Swift.Error> {
}
}

self.isEnabled = state.map { $0.isEnabled }
self.isExecuting = state.map { $0.isExecuting }
self.isEnabled = state.map { $0.isEnabled }.skipRepeats()
self.isExecuting = state.map { $0.isExecuting }.skipRepeats()
}

/// Initializes an action that will be conditionally enabled, and creates a
Expand Down
32 changes: 31 additions & 1 deletion Tests/ReactiveSwiftTests/ActionSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import Foundation

import Dispatch
import Result
import Nimble
import Quick
Expand Down Expand Up @@ -114,6 +114,36 @@ class ActionSpec: QuickSpec {
expect(action.isExecuting.value) == false
}

if #available(macOS 10.10, *) {
it("should not loop indefinitely") {
let condition = MutableProperty(1)

let action = Action<Void, Void, NoError>(state: condition, enabledIf: { $0 == 0 }) { _ in
return .empty
}

let disposable = CompositeDisposable()

waitUntil(timeout: 0.01) { done in
let target = DispatchQueue(label: "test target queue")
let highPriority = QueueScheduler(qos: .userInitiated, targeting: target)
let lowPriority = QueueScheduler(qos: .default, targeting: target)

disposable += action.isExecuting.producer
.observe(on: highPriority)
.startWithValues { _ in
condition.value = 10
}

disposable += lowPriority.schedule {
done()
}
}

disposable.dispose()
}
}

describe("completed") {
beforeEach {
enabled.value = true
Expand Down