All Projects → babylonhealth → Reactivefeedback

babylonhealth / Reactivefeedback

Licence: mit
Unidirectional reactive architecture

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Reactivefeedback

Cycle.swift
An experiment in unidirectional architecture inspired by Cycle.js. https://cycle.js.org
Stars: ✭ 24 (-84.62%)
Mutual labels:  architecture, unidirectional-data-flow
ReMVVM
ReMVVM is an application architecture concept, marriage of Unidirectional Data Flow (Redux) with MVVM.
Stars: ✭ 180 (+15.38%)
Mutual labels:  architecture, unidirectional-data-flow
Remvvm
ReMVVM is an application architecture concept, marriage of Unidirectional Data Flow (Redux) with MVVM.
Stars: ✭ 168 (+7.69%)
Mutual labels:  unidirectional-data-flow, architecture
Unidirectional Architecture On Mobile
Dive into 📱 Unidirectional Architecture!
Stars: ✭ 115 (-26.28%)
Mutual labels:  unidirectional-data-flow, architecture
Vueflux
♻️ Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux
Stars: ✭ 315 (+101.92%)
Mutual labels:  unidirectional-data-flow, architecture
Kunidirectional
The goal of this sample app is to show how we can implement unidirectional data flow architecture based on Flux and Redux on Android... using Kotlin 😉
Stars: ✭ 303 (+94.23%)
Mutual labels:  unidirectional-data-flow, architecture
Reactor
🔄 Unidirectional data flow in Swift.
Stars: ✭ 174 (+11.54%)
Mutual labels:  unidirectional-data-flow, architecture
Norris
Showcase for Unidirectional Data Flow architecture for Android, powered by Kotlin Coroutines
Stars: ✭ 315 (+101.92%)
Mutual labels:  unidirectional-data-flow, architecture
Swift Composable Architecture
A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
Stars: ✭ 5,199 (+3232.69%)
Mutual labels:  unidirectional-data-flow, architecture
Awesome Tca
Awesome list for The Composable Architecture
Stars: ✭ 124 (-20.51%)
Mutual labels:  unidirectional-data-flow, architecture
Android Base Mvp
Android Base MVP Concept with RXJava, Dagger, Event Bus, Retrofit, Glide, OkHTTP
Stars: ✭ 141 (-9.62%)
Mutual labels:  architecture
Cleanarchitecture.workerservice
A solution template using Clean Architecture for building a .NET Core Worker Service.
Stars: ✭ 142 (-8.97%)
Mutual labels:  architecture
Pytorch Vae
A Collection of Variational Autoencoders (VAE) in PyTorch.
Stars: ✭ 2,704 (+1633.33%)
Mutual labels:  architecture
Silicon Info
Mac menu bar tool to view the architecture of the running application
Stars: ✭ 153 (-1.92%)
Mutual labels:  architecture
Style
[Deprecated], Not maintain anymore. A live wallpaper project
Stars: ✭ 141 (-9.62%)
Mutual labels:  architecture
Kingly
Zero-cost state-machine library for robust, testable and portable user interfaces (most machines compile ~1-2KB)
Stars: ✭ 147 (-5.77%)
Mutual labels:  architecture
Android Modular Architecture
📚 Sample Android Components Architecture on a modular word focused on the scalability, testability and maintainability written in Kotlin, following best practices using Jetpack.
Stars: ✭ 2,048 (+1212.82%)
Mutual labels:  architecture
Pipelinr
PipelinR is a lightweight command processing pipeline ❍ ⇢ ❍ ⇢ ❍ for your Java awesome app.
Stars: ✭ 134 (-14.1%)
Mutual labels:  architecture
Mosby Conductor
Plugin for conductor to integrate Mosby
Stars: ✭ 134 (-14.1%)
Mutual labels:  architecture
Javainterview
最全的Java技术知识点,以及Java源码分析。为开源贡献自己的一份力。
Stars: ✭ 154 (-1.28%)
Mutual labels:  architecture

ReactiveFeedback

Unidirectional Reactive Architecture. This is a ReactiveSwift implemetation of RxFeedback

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

State is 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 one State to another is to emit an Event.

struct Results<T: JSONSerializable> {
    let page: Int
    let totalResults: Int
    let totalPages: Int
    let results: [T]

    static func empty() -> Results<T> {
        return Results<T>(page: 0, totalResults: 0, totalPages: 0, results: [])
    }
}

struct Context {
    var batch: Results<Movie>
    var movies: [Movie]

    static var empty: Context {
        return Context(batch: Results.empty(), movies: [])
    }
}

enum State {
    case initial
    case paging(context: Context)
    case loadedPage(context: Context)
    case refreshing(context: Context)
    case refreshed(context: Context)
    case error(error: NSError, context: Context)
    case retry(context: Context)
}
Event

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

enum Event {
    case startLoadingNextPage
    case response(Results<Movie>)
    case failed(NSError)
    case retry
}
Reducer

A Reducer is a pure function with a signature of (State, Event) -> State. While Event represents an action that results in a State change, it's actually not what causes the change. An Event is just that, a representation of the intention to transition from one state to another. What actually causes the State to change, the embodiment of the corresponding Event, is a Reducer. A Reducer is the only place where a State can be changed.

static func reduce(state: State, event: Event) -> State {
    switch event {
    case .startLoadingNextPage:
        return .paging(context: state.context)
    case .response(let batch):
        var copy = state.context
        copy.batch = batch
        copy.movies += batch.results
        return .loadedPage(context: copy)
    case .failed(let error):
        return .error(error: error, context: state.context)
    case .retry:
        return .retry(context: state.context)
    }
}
Feedback

While State represents where the system is at a given time, Event represents a trigger for state change, and a Reducer is 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 the Feedback. It's essentially a "processing engine", listening to changes in the current State and emitting the corresponding next events to take place. It's represented by a pure function with a signature of Signal<State, NoError> -> Signal<Event, NoError>. Feedbacks don't directly mutate states. Instead, they only emit events which then cause states to change in reducers.

public struct Feedback<State, Event> {
    public let events: (Scheduler, Signal<State, NoError>) -> Signal<Event, NoError>
}

func loadNextFeedback(for nearBottomSignal: Signal<Void, NoError>) -> Feedback<State, Event> {
    return Feedback(predicate: { !$0.paging }) { _ in
        return nearBottomSignal
            .map { Event.startLoadingNextPage }
        }
}

func pagingFeedback() -> Feedback<State, Event> {
    return Feedback<State, Event>(skippingRepeated: { $0.nextPage }) { (nextPage) -> SignalProducer<Event, NoError> in
        return URLSession.shared.fetchMovies(page: nextPage)
            .map(Event.response)
            .flatMapError { (error) -> SignalProducer<Event, NoError> in
                return SignalProducer(value: Event.failed(error))
            }
        }
}

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

func retryPagingFeedback() -> Feedback<State, Event> {
    return Feedback<State, Event>(skippingRepeated: { $0.retryPage }) { (nextPage) -> SignalProducer<Event, NoError> in
        return URLSession.shared.fetchMovies(page: nextPage)
            .map(Event.response)
            .flatMapError { (error) -> SignalProducer<Event, NoError> in
                return SignalProducer(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 the State will be then delivered to all Feedback loops that were added to the system.
  3. Feedback then decides whether any action should be performed with a subset of the State (e.g calling API, observe UI events) by dispatching an Event, or ignoring it by returning SignalProducer.empty.
  4. Dispatched Event then goes to the Reducer which applies it and returns a new value of the State.
  5. And then cycle starts all over (see 2).
Example
let increment = Feedback<Int, Event> { _ in
    return self.plusButton.reactive
        .controlEvents(.touchUpInside)
        .map { _ in Event.increment }
}

let decrement = Feedback<Int, Event> { _ in
    return self.minusButton.reactive
        .controlEvents(.touchUpInside)
        .map { _ in Event.decrement }
}

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

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

Advantages

TODO

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].