Skip to content

Commit

Permalink
Let feedback effects start immediately. (#21)
Browse files Browse the repository at this point in the history
* Test cases for immediate observation.

* Let feedback effects start immediately.
`start(on:)` is not essential for breaking signal recursion.
  • Loading branch information
andersio authored and RuiAAPeres committed Apr 24, 2018
1 parent f6b6be1 commit 1c99379
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
8 changes: 4 additions & 4 deletions ReactiveFeedback/Feedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct Feedback<State, Event> {
.flatMap(.latest) { control -> SignalProducer<Event, NoError> in
guard let control = control else { return .empty }
return effects(control).producer
.enqueue(on: scheduler)
.observe(on: scheduler)
}
}
}
Expand All @@ -51,7 +51,7 @@ public struct Feedback<State, Event> {
.flatMap(.latest) { control -> SignalProducer<Event, NoError> in
guard let control = control else { return .empty }
return effects(control).producer
.enqueue(on: scheduler)
.observe(on: scheduler)
}
}
}
Expand All @@ -69,7 +69,7 @@ public struct Feedback<State, Event> {
return state.filter(predicate)
.flatMap(.latest) { state -> SignalProducer<Event, NoError> in
return effects(state).producer
.enqueue(on: scheduler)
.observe(on: scheduler)
}
}
}
Expand All @@ -83,7 +83,7 @@ public struct Feedback<State, Event> {
self.events = { scheduler, state in
return state.flatMap(.latest) { state -> SignalProducer<Event, NoError> in
return effects(state).producer
.enqueue(on: scheduler)
.observe(on: scheduler)
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions ReactiveFeedback/SignalProducer+System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,4 @@ extension SignalProducer where Error == NoError {
private static func deferred(_ producer: @escaping () -> SignalProducer<Value, Error>) -> SignalProducer<Value, Error> {
return SignalProducer { $1 += producer().start($0) }
}

func enqueue(on scheduler: Scheduler) -> SignalProducer<Value, Error> {
return producer.start(on: scheduler)
.observe(on: scheduler)
}
}
66 changes: 66 additions & 0 deletions ReactiveFeedbackTests/SystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, NoError>.pipe()

let system = SignalProducer<String, NoError>.system(
initial: "initial",
scheduler: scheduler,
reduce: { (state: String, event: String) -> String in
return state + event
},
feedbacks: [
Feedback { state -> Signal<String, NoError> 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<String, NoError>.system(
initial: "initial",
scheduler: scheduler,
reduce: { (state: String, event: String) -> String in
return state + event
},
feedbacks: [
Feedback { state -> SignalProducer<String, NoError> 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
}
}

0 comments on commit 1c99379

Please sign in to comment.