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

Let feedback effects start immediately. #21

Merged
merged 2 commits into from
Apr 24, 2018
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
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
}
}