From 74e3d1d786a425ebe2227c8aa09d6c7c910b5e96 Mon Sep 17 00:00:00 2001 From: Anders Ha Date: Sun, 1 Apr 2018 18:15:10 +0100 Subject: [PATCH 1/2] Test cases for immediate observation. --- ReactiveFeedbackTests/SystemTests.swift | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/ReactiveFeedbackTests/SystemTests.swift b/ReactiveFeedbackTests/SystemTests.swift index 962a30d..dad2737 100644 --- a/ReactiveFeedbackTests/SystemTests.swift +++ b/ReactiveFeedbackTests/SystemTests.swift @@ -115,4 +115,70 @@ class SystemTests: XCTestCase { ] expect(result).toEventually(equal(expected)) } + + func test_should_observe_signals_immediately() { + let scheduler = TestScheduler() + let (signal, observer) = Signal.pipe() + + let system = SignalProducer.system( + initial: "initial", + scheduler: scheduler, + reduce: { (state: String, event: String) -> String in + return state + event + }, + feedbacks: [ + Feedback { state -> Signal in + return signal + } + ] + ) + + var value: String? + system.startWithValues { value = $0 } + + expect(value) == "initial" + + observer.send(value: "_a") + expect(value) == "initial" + + scheduler.advance() + expect(value) == "initial_a" + } + + + func test_should_start_producers_immediately() { + let scheduler = TestScheduler() + var startCount = 0 + + let system = SignalProducer.system( + initial: "initial", + scheduler: scheduler, + reduce: { (state: String, event: String) -> String in + return state + event + }, + feedbacks: [ + Feedback { state -> SignalProducer in + return SignalProducer(value: "_a") + .on(starting: { startCount += 1 }) + } + ] + ) + + var value: String? + system + .skipRepeats() + .take(first: 2) + .startWithValues { value = $0 } + + expect(value) == "initial" + expect(startCount) == 1 + + scheduler.advance() + expect(value) == "initial_a" + expect(startCount) == 2 + + scheduler.advance() + expect(value) == "initial_a" + expect(startCount) == 2 + } } From 0090fec93648d33dd2b74f7f11afe0ae66f61918 Mon Sep 17 00:00:00 2001 From: Anders Ha Date: Sun, 1 Apr 2018 18:23:25 +0100 Subject: [PATCH 2/2] Let feedback effects start immediately. `start(on:)` is not essential for breaking signal recursion. --- ReactiveFeedback/Feedback.swift | 8 ++++---- ReactiveFeedback/SignalProducer+System.swift | 5 ----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/ReactiveFeedback/Feedback.swift b/ReactiveFeedback/Feedback.swift index 4dc2fa6..0380316 100644 --- a/ReactiveFeedback/Feedback.swift +++ b/ReactiveFeedback/Feedback.swift @@ -30,7 +30,7 @@ public struct Feedback { .flatMap(.latest) { control -> SignalProducer in guard let control = control else { return .empty } return effects(control).producer - .enqueue(on: scheduler) + .observe(on: scheduler) } } } @@ -51,7 +51,7 @@ public struct Feedback { .flatMap(.latest) { control -> SignalProducer in guard let control = control else { return .empty } return effects(control).producer - .enqueue(on: scheduler) + .observe(on: scheduler) } } } @@ -69,7 +69,7 @@ public struct Feedback { return state.filter(predicate) .flatMap(.latest) { state -> SignalProducer in return effects(state).producer - .enqueue(on: scheduler) + .observe(on: scheduler) } } } @@ -83,7 +83,7 @@ public struct Feedback { self.events = { scheduler, state in return state.flatMap(.latest) { state -> SignalProducer in return effects(state).producer - .enqueue(on: scheduler) + .observe(on: scheduler) } } } diff --git a/ReactiveFeedback/SignalProducer+System.swift b/ReactiveFeedback/SignalProducer+System.swift index 3ce4d9d..4a49299 100644 --- a/ReactiveFeedback/SignalProducer+System.swift +++ b/ReactiveFeedback/SignalProducer+System.swift @@ -56,9 +56,4 @@ extension SignalProducer where Error == NoError { private static func deferred(_ producer: @escaping () -> SignalProducer) -> SignalProducer { return SignalProducer { $1 += producer().start($0) } } - - func enqueue(on scheduler: Scheduler) -> SignalProducer { - return producer.start(on: scheduler) - .observe(on: scheduler) - } }