Skip to content

Commit

Permalink
Merge pull request #221 from ReactiveCocoa/as-fix-action-replay-loop
Browse files Browse the repository at this point in the history
Fix infinite feedback loop between Action states
  • Loading branch information
mdiep authored Jan 13, 2017
2 parents 8900ee8 + cc1a75a commit 3925cad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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

0 comments on commit 3925cad

Please sign in to comment.