Skip to content

Commit

Permalink
Fix the race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
andersio committed Feb 28, 2019
1 parent ef04747 commit b29d979
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ReactiveFeedback/SignalProducer+System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,23 @@ extension SignalProducer where Error == NoError {

return SignalProducer<Event, NoError>(Signal.merge(events))
.scan(initial, reduce)
.prefix(value: initial)
.on(value: stateObserver.send(value:))
.prefix(value: initial)
.on(started: {
// NOTE: Due to the nature of `prefix` lazily starting the producer being prefixed, we cannot rely
// on `on(value:)` to ignite the feedbacks with the initial state.
//
// At the time `prefix(value:)` calls `on(value:)` for the initial value, the events-reducer
// producer has not yet been started yet. Consequentially, it would lead to dropped events
// when the system is instantiated on a queue different from the queue used for
// serializing events.
//
// Having said that, `prefix(value:)` is guaranteed to have started the prefixed producer as
// part of the synchronous producer starting process. So we can addres the issue by applying
// `on(started:)` after `prefix(value:)` to ignite the system, while having `on(value:)`
// instead applied before `prefix(value:)` to keep the reducer-to-feedbacks path open.
stateObserver.send(value: initial)
})
}
}

Expand Down

0 comments on commit b29d979

Please sign in to comment.