Skip to content

Composable unidirectional data flow with ReactiveSwift.

License

Notifications You must be signed in to change notification settings

ReactiveCocoa/Loop

Repository files navigation

Unidirectional Reactive Architecture. This is aReactiveSwiftcounterpart ofRxFeedback.

Documentation

Motivation

Requirements for iOS apps have become huge. Our code has to manage a lot of state e.g. server responses, cached data, UI state, routing etc. Some may say that Reactive Programming can help us a lot but, in the wrong hands, it can do even more harm to your code base.

The goal of this library is to provide a simple and intuitive approach to designing reactive state machines.

Core Concepts

State

Stateis the single source of truth. It represents a state of your system and is usually a plain Swift type (which doesn't contain any ReactiveSwift primitives). Your state is immutable. The only way to transition from oneStateto another is to emit anEvent.

structResults<T:JSONSerializable>{
letpage:Int
lettotalResults:Int
lettotalPages:Int
letresults:[T]

staticfuncempty()->Results<T>{
returnResults<T>(page:0,totalResults:0,totalPages:0,results:[])
}
}

structContext{
varbatch:Results<Movie>
varmovies:[Movie]

staticvarempty:Context{
returnContext(batch:Results.empty(),movies:[])
}
}

enumState{
caseinitial
casepaging(context:Context)
caseloadedPage(context:Context)
caserefreshing(context:Context)
caserefreshed(context:Context)
caseerror(error:NSError,context:Context)
caseretry(context:Context)
}
Event

Represents all possible events that can happen in your system which can cause a transition to a newState.

enumEvent{
casestartLoadingNextPage
caseresponse(Results<Movie>)
casefailed(NSError)
caseretry
}
Reducer

A Reducer is a pure function with a signature of(State, Event) -> State.WhileEventrepresents an action that results in aStatechange, it's actually not whatcausesthe change. AnEventis just that, a representation of the intention to transition from one state to another. What actually causes theStateto change, the embodiment of the correspondingEvent,is a Reducer. A Reducer is the only place where aStatecan be changed.

staticfuncreduce(state:State,event:Event)->State{
switch event{
case.startLoadingNextPage:
return.paging(context:state.context)
case.response(letbatch):
varcopy=state.context
copy.batch=batch
copy.movies+=batch.results
return.loadedPage(context:copy)
case.failed(leterror):
return.error(error:error,context:state.context)
case.retry:
return.retry(context:state.context)
}
}
Feedback

WhileStaterepresents where the system is at a given time,Eventrepresents a trigger for state change, and aReduceris the pure function that changes the state depending on current state and type of event received, there is not as of yet any type to emit events given a particular current state. That's the job of theFeedback.It's essentially a "processing engine", listening to changes in the currentStateand emitting the corresponding next events to take place. It's represented by a pure function with a signature ofSignal<State, NoError> -> Signal<Event, NoError>.Feedbacks don't directly mutate states. Instead, they only emit events which then cause states to change in reducers.

publicstructFeedback<State,Event>{
publicletevents:(Scheduler,Signal<State,NoError>)->Signal<Event,NoError>
}

funcloadNextFeedback(for nearBottomSignal:Signal<Void,NoError>)->Feedback<State,Event>{
returnFeedback(predicate:{!$0.paging}){_in
returnnearBottomSignal
.map{Event.startLoadingNextPage}
}
}

funcpagingFeedback()->Feedback<State,Event>{
returnFeedback<State,Event>(skippingRepeated:{$0.nextPage}){(nextPage)->SignalProducer<Event,NoError>in
returnURLSession.shared.fetchMovies(page:nextPage)
.map(Event.response)
.flatMapError{(error)->SignalProducer<Event,NoError>in
returnSignalProducer(value:Event.failed(error))
}
}
}

funcretryFeedback(for retrySignal:Signal<Void,NoError>)->Feedback<State,Event>{
returnFeedback<State,Event>(skippingRepeated:{$0.lastError}){_->Signal<Event,NoError>in
returnretrySignal.map{Event.retry}
}
}

funcretryPagingFeedback()->Feedback<State,Event>{
returnFeedback<State,Event>(skippingRepeated:{$0.retryPage}){(nextPage)->SignalProducer<Event,NoError>in
returnURLSession.shared.fetchMovies(page:nextPage)
.map(Event.response)
.flatMapError{(error)->SignalProducer<Event,NoError>in
returnSignalProducer(value:Event.failed(error))
}
}
}

The Flow

  1. As you can see from the diagram above we always start with an initial state.
  2. Every change to theStatewill be then delivered to allFeedbackloops that were added to the system.
  3. Feedbackthen decides whether any action should be performed with a subset of theState(e.g calling API, observe UI events) by dispatching anEvent,or ignoring it by returningSignalProducer.empty.
  4. DispatchedEventthen goes to theReducerwhich applies it and returns a new value of theState.
  5. And then cycle starts all over (see 2).
Example
letincrement=Feedback<Int,Event>{_in
returnself.plusButton.reactive
.controlEvents(.touchUpInside)
.map{_inEvent.increment}
}

letdecrement=Feedback<Int,Event>{_in
returnself.minusButton.reactive
.controlEvents(.touchUpInside)
.map{_inEvent.decrement}
}

letsystem=SignalProducer<Int,NoError>.system(initial:0,
reduce:{(count,event)->Intin
switch event{
case.increment:
returncount+1
case.decrement:
returncount-1
}
},
feedbacks:[increment,decrement])

label.reactive.text<~system.map(String.init)

Advantages

TODO

Acknowledgements

This is a community fork of theReactiveFeedbackproject (with the MIT license) from Babylon Health.