{"id":17088022,"url":"https://github.com/sergdort/combinefeedback","last_synced_at":"2025-04-04T16:15:31.907Z","repository":{"id":45970734,"uuid":"190697492","full_name":"sergdort/CombineFeedback","owner":"sergdort","description":"Unidirectional reactive architecture using new Apple Combine framework https://developer.apple.com/documentation/combine","archived":false,"fork":false,"pushed_at":"2021-11-23T12:59:46.000Z","size":14434,"stargazers_count":688,"open_issues_count":12,"forks_count":54,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-04-03T19:47:14.626Z","etag":null,"topics":["architecture","combine","swift","swiftui","unidirectional"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sergdort.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-07T06:22:44.000Z","updated_at":"2025-03-23T13:25:55.000Z","dependencies_parsed_at":"2022-09-17T08:01:07.114Z","dependency_job_id":null,"html_url":"https://github.com/sergdort/CombineFeedback","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergdort%2FCombineFeedback","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergdort%2FCombineFeedback/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergdort%2FCombineFeedback/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sergdort%2FCombineFeedback/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sergdort","download_url":"https://codeload.github.com/sergdort/CombineFeedback/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208151,"owners_count":20901570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["architecture","combine","swift","swiftui","unidirectional"],"created_at":"2024-10-14T13:35:53.879Z","updated_at":"2025-04-04T16:15:31.887Z","avatar_url":"https://github.com/sergdort.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CombineFeedback\n\nUnidirectional Reactive Architecture. This is a [Combine](https://developer.apple.com/documentation/combine) implemetation of [ReactiveFeedback](https://github.com/Babylonpartners/ReactiveFeedback) and [RxFeedback](https://github.com/kzaher/RxFeedback)\n\n## Diagram\n\n![](diagrams/ReactiveFeedback.jpg)\n\n## Motivation\n\nRequirements 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.\n\nThe goal of this library is to provide a simple and intuitive approach to designing reactive state machines.\n\n## Core Concepts\n\n### State \n\n`State` is the single source of truth. It represents a state of your system and is usually a plain Swift type. Your state is immutable. The only way to transition from one `State` to another is to emit an `Event`.\n\n### Event\n\nRepresents all possible events that can happen in your system which can cause a transition to a new `State`.\n\n### Reducer \n\nA Reducer is a pure function with a signature of `( inout State, Event) -\u003e Void`. 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.\n\n### Feedback\n\nWhile `State` represents where the system is at a given time, `Event` represents a state change, and a `Reducer` is the pure function that enacts the event causing the state to change, there is not as of yet any type to decide which event should take place 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. Feedbacks don't directly mutate states. Instead, they only emit events which then cause states to change in reducers.\n\nTo some extent it's like reactive [Middleware](https://redux.js.org/advanced/middleware) in [Redux](https://redux.js.org)\n\n### Dependency\n\nDependency is the type that holds all services that feature needs, such as API clients, analytics clients, etc.\n\n#### Store\n\nStore - is a base class responsible for initializing a UI state machine. It provides two ways to interact with it. \n\n- We can start a state machine by observing `var state: AnyPublisher\u003cState, Never\u003e`. \n- We can send input events into it via `public final func send(event: Event)`. \n\nThis is useful if we want to mutate our state in response to user input. Let's consider a `Counter` example\n\n```swift\nstruct State {\n    var count = 0\n}\n\nenum Event {\n    case increment\n    case decrement\n}\n```\nWhen we press **+** button we want the `State` of the system to be incremented by `1`. To do that somewhere in our UI we can do:\n\n```swift\nButton(action: {\n    store.send(event: .increment)\n}) {\n    return Text(\"+\").font(.largeTitle)\n}\n```\n\nAlso, we can use the `send(event:)` method to initiate side effects. For example, imagine that we are building an infinite list, and we want to trigger the next batch load when a user reaches the end of the list. \n\n```swift\nenum Event {\n    case didLoad(Results)\n    case didFail(Error)\n    case fetchNext\n}\n\nstruct State: Builder {\n    var batch: Results\n    var movies: [Movie]\n    var status: Status\n}\nenum Status {\n    case idle\n    case loading\n    case failed(Error)\n}\n\nstruct MoviesView: View {\n    typealias State = MoviesViewModel.State\n    typealias Event = MoviesViewModel.Event\n    let context: Context\u003cState, Event\u003e\n\n    var body: some View {\n        List {\n            ForEach(context.movies.identified(by: \\.id)) { movie in\n                MovieCell(movie: movie).onAppear {\n                // When we reach the end of the list\n                // we send `fetchNext` event\n                    if self.context.movies.last == movie {\n                        self.context.send(event: .fetchNext)\n                    }\n                }\n            }\n        }\n    }\n}\n```\nWhen we send `.fetchNext` event, it goes to the `reducer` where we put our system into `.loading`  state, which in response triggers effect in the `whenLoading` feedback, which is reacting to particular state changes\n\n```swift\n    static func reducer(state: inout State, event: Event) {\n        switch event {\n        case .didLoad(let batch):\n            state.movies += batch.results\n            state.status = .idle\n            state.batch = batch\n        case .didFail(let error):\n            state.status = .failed(error)\n        case .retry:\n            state.status = .loading\n        case .fetchNext:\n            state.status = .loading\n        }\n    }\n\n    static var feedback: Feedback\u003cState, Event\u003e {\n        return Feedback(lensing: { $0.nextPage }) { page in\n            URLSession.shared\n                .fetchMovies(page: page)\n                .map(Event.didLoad)\n                .replaceError(replace: Event.didFail)\n                .receive(on: DispatchQueue.main)\n        }\n    }\n```\n\n#### Composition\n\nTaking inspiration from [TCA](https://github.com/pointfreeco/swift-composable-architecture) `CombineFeedback` is build with a composition in mind.\n\nMeaning that we can compose smaller states into bigger states. For more details please see Example App.\n\n#### ViewContext\n\n`ViewContext\u003cState, Event\u003e` - is a rendering context that we can use to interact with UI and render information. Via  `@dynamicMemberLookup` it has all of the properties of the `State` and several conveniences methods for more seamless integration with SwiftUI. (Credits to [@andersio](https://github.com/andersio))\n\n```swift\nstruct State  {\n    var email = \"\"\n    var password = \"\"\n}\nenum Event {\n\tcase signIn\n}\nstruct SignInView: View {\n    private let store: Store\u003cState, Event\u003e\n    \n    init(store: Store\u003cState, Event\u003e) {\n        self.store = store\n    }\n    \n    var body: some View {\n      WithContextView(store: store) { context in\n        Form {\n            Section {\n                TextField(context.binding(for: \\.email, event: Event.emailDidChange))\n                TextField(context.binding(for: \\.password, event: Event.passwordDidCange))\n                Button(action: context.action(for: .signIn)) {\n                    Text(\"Sign In\")\n                }\n            }\n        }\n      }\n    }\n}\n```\n\n### Example\n\n| Counter | Infinite List | SignIn Form | Traffic Light |\n| --- | --- | --- | --- |\n|\u003cimg src=\"diagrams/counter.gif\" width=\"250\"/\u003e | \u003cimg src=\"diagrams/movies.gif\" width=\"250\"/\u003e | \u003cimg src=\"diagrams/signin.png\" width=\"250\"/\u003e | \u003cimg src=\"diagrams/traffic_light.gif\" width=\"250\"/\u003e \n\n\n### References\n\n[Automata theory](https://en.wikipedia.org/wiki/Automata_theory)\n[TCA](https://github.com/pointfreeco/swift-composable-architecture)\n[Finite-state machine](https://en.wikipedia.org/wiki/Finite-state_machine)\n[Mealy machine](https://en.wikipedia.org/wiki/Mealy_machine)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergdort%2Fcombinefeedback","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsergdort%2Fcombinefeedback","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsergdort%2Fcombinefeedback/lists"}