├── .github └── workflows │ ├── deploy_docc.yml │ └── master.yml ├── .gitignore ├── .swiftlint.yml ├── Docs └── img │ ├── logo.svg │ └── unidirectional-flow.png ├── LICENSE ├── Package.swift ├── README.md ├── Scripts └── build_docc.sh ├── UDF.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── UDF.xcscheme ├── UDF ├── Action.swift ├── ActionDispatcher.swift ├── ActionListener │ ├── ActionListener.swift │ ├── Connector │ │ ├── ActionListenerConnector.swift │ │ └── ClosureActionListenerConnector.swift │ ├── ServiceActionListener.swift │ └── ViewActionListener.swift ├── Component │ ├── Component+Connector.swift │ ├── Component+SelfConnector.swift │ ├── Component+stateToProps.swift │ ├── Component.swift │ ├── Propsable.swift │ ├── ServiceComponent.swift │ └── ViewComponent.swift ├── Connector │ ├── ClosureConnector.swift │ ├── Connector.swift │ └── Mapper.swift ├── Disposer.swift ├── Info.plist ├── Reducer.swift ├── SideEffect │ ├── CombineSideEffect.swift │ ├── SideEffectProtocol.swift │ ├── SideEffectTypealiases.swift │ └── SideEffectsBuilder.swift ├── Store.swift ├── Subscription.swift ├── UDF.h ├── Utils.swift └── ViewStore.swift └── UDFTests ├── ActionListenerTests.swift ├── CombineSideEffectTests.swift ├── Component+ConnectorTests.swift ├── Component+SelfConnectorTests.swift ├── Component+stateToPropsTests.swift ├── Info.plist ├── SideEffectTests.swift ├── StoreBasicTests.swift ├── StoreDoubleScopeTests.swift ├── StoreScopeTests.swift └── TestDoubles ├── FakeAction.swift ├── FakeActionListener.swift ├── FakeActionListenerAndConnector.swift ├── FakeActionListenerConnector.swift ├── FakeComponent.swift ├── FakeComponentConnector.swift ├── FakeConnector.swift ├── FakeSideEffect.swift └── TestState.swift /.github/workflows/deploy_docc.yml: -------------------------------------------------------------------------------- 1 | name: 00_deploy_docc 2 | 3 | on: 4 | push: 5 | branches: ["master"] 6 | workflow_dispatch: 7 | 8 | # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages 9 | permissions: 10 | contents: read 11 | pages: write 12 | id-token: write 13 | 14 | # Allow one concurrent deployment 15 | concurrency: 16 | group: "pages" 17 | cancel-in-progress: true 18 | 19 | jobs: 20 | deploy: 21 | environment: 22 | name: github-pages 23 | url: ${{ steps.deployment.outputs.page_url }} 24 | runs-on: macos-latest 25 | timeout-minutes: 90 26 | steps: 27 | - name: 📥 checkout 28 | uses: actions/checkout@v4 29 | 30 | - name: 📋 Build DocC 31 | run: source Scripts/build_docc.sh 32 | 33 | - name: 📜 Upload artifact 34 | uses: actions/upload-pages-artifact@v3 35 | with: 36 | # Upload docs directory 37 | path: 'docs' 38 | 39 | - name: 🐙 Deploy to GitHub Pages 40 | id: deployment 41 | uses: actions/deploy-pages@v4 42 | -------------------------------------------------------------------------------- /.github/workflows/master.yml: -------------------------------------------------------------------------------- 1 | name: 01_master 2 | 3 | on: 4 | # Trigger the workflow on push, 5 | push: 6 | branches: 7 | - 'develop' 8 | - 'master' 9 | - 'feature/**' 10 | # Триггер – пул реквесты *В* бранчи 11 | # т.е. базовой веткой pr'а должна быть одна из перечисленных 12 | pull_request: 13 | types: [ opened, synchronize ] 14 | # фичи могут мерджится в эти ветки 15 | branches: 16 | - 'feature/**' 17 | - 'develop' 18 | # Trigger manually 19 | workflow_dispatch: 20 | 21 | permissions: 22 | contents: read 23 | 24 | jobs: 25 | build: 26 | name: develop build 27 | runs-on: macos-latest 28 | timeout-minutes: 90 29 | environment: production 30 | env: 31 | LC_ALL: en_US.UTF-8 32 | LANG: en_US.UTF-8 33 | LANGUAGE: en_US.UTF-8 34 | steps: 35 | - name: checkout 36 | uses: actions/checkout@v4 37 | 38 | - name: Build 39 | run: xcodebuild -project UDF.xcodeproj 40 | 41 | - name: Show SDK 42 | # For available image environment showcase 43 | run: xcodebuild -showsdks 44 | 45 | - name: Test 46 | run: > 47 | xcodebuild test 48 | -project UDF.xcodeproj 49 | -scheme UDF 50 | -destination 'platform=iOS Simulator,name=iPhone 11,OS=16.2' 51 | CODE_SIGN_IDENTITY="" 52 | CODE_SIGN_ALLOWED=NO 53 | CODE_SIGNING_ALLOWED=NO 54 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | .build/ 41 | .swiftpm 42 | Package.resolved 43 | 44 | # CocoaPods 45 | # 46 | # We recommend against adding the Pods directory to your .gitignore. However 47 | # you should judge for yourself, the pros and cons are mentioned at: 48 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 49 | # 50 | # Pods/ 51 | 52 | # Carthage 53 | # 54 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 55 | # Carthage/Checkouts 56 | 57 | Carthage/Build 58 | 59 | # fastlane 60 | # 61 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 62 | # screenshots whenever they are needed. 63 | # For more information about the recommended setup visit: 64 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 65 | 66 | fastlane/report.xml 67 | fastlane/Preview.html 68 | fastlane/screenshots 69 | fastlane/test_output 70 | 71 | .DS_Store 72 | 73 | # App Code 74 | .idea 75 | -------------------------------------------------------------------------------- /.swiftlint.yml: -------------------------------------------------------------------------------- 1 | disabled_rules: 2 | - todo 3 | excluded: 4 | - Pods 5 | - Carthage 6 | - Modules 7 | identifier_name: 8 | excluded: 9 | - id 10 | - vc 11 | - to 12 | - di 13 | - up 14 | - by 15 | - x 16 | - y 17 | type_name: 18 | excluded: 19 | - ID 20 | nesting: 21 | type_level: 3 22 | cyclomatic_complexity: 23 | error: 100 24 | -------------------------------------------------------------------------------- /Docs/img/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Docs/img/unidirectional-flow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/inDriver/UDF/3ce4193bc60f28fb5a3770a921e8b1e7d5fec29b/Docs/img/unidirectional-flow.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.2 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "UDF", 8 | platforms: [ 9 | .iOS(.v13) 10 | ], 11 | products: [ 12 | // Products define the executables and libraries a package produces, and make them visible to other packages. 13 | .library( 14 | name: "UDF", 15 | targets: ["UDF"]), 16 | ], 17 | dependencies: [ 18 | // Dependencies declare other packages that this package depends on. 19 | // .package(url: /* package url */, from: "1.0.0"), 20 | ], 21 | targets: [ 22 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 23 | // Targets can depend on other targets in this package, and on products in packages this package depends on. 24 | .target( 25 | name: "UDF", 26 | path: "UDF"), 27 | .testTarget( 28 | name: "UDFTests", 29 | dependencies: ["UDF"], 30 | path: "UDFTests"), 31 | ] 32 | ) 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Swift Version](https://img.shields.io/badge/Swift-5.7-F16D39.svg?style=flat)](https://developer.apple.com/swift) 2 | [![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager) 3 | 4 | # UDF 5 | 6 |

7 | UDF 8 |

9 | 10 | **UDF** (Unidirectional Data Flow) is a library based on [Unidirectional Data Flow](https://en.wikipedia.org/wiki/Unidirectional_Data_Flow_(computer_science)) pattern. It lets you build maintainable, testable, and scalable apps. 11 | 12 | - [Unidirectional Data Flow Design Pattern](#unidirectional-data-flow-design-pattern) 13 | - [Advantages](#advantages) 14 | - [Basic Usage](#basic-usage) 15 | - [Building Domain](#building-domain) 16 | - [View Component](#view-component) 17 | - [Modularization](#modularization) 18 | - [Installation](#installation) 19 | 20 | ## Documentation 21 | 22 | The documentation for releases and main are available here: 23 | 24 | - [main](https://indriver.github.io/UDF/) 25 | 26 | ## Unidirectional Data Flow Design Pattern 27 | A unidirectional data flow is a design pattern where a state (data) flows down and events (actions) flow up. It's important that UI never edits or sends back data. That's why UI is usually provided with immutable data. It allows having a single source of truth for a whole app and effectively separates domain logic from UI. 28 |

29 | UDF 30 |

31 | 32 | Unidirectional Data Flow design pattern has been popular for a long time in the web development. Now it's time for the mobile development. Having started from multi-platform solutions like React Native and Flutter, Unidirectional Data Flow now becomes a part of native. [SwiftUI](https://developer.apple.com/documentation/swiftui/model-data) for Swift and [Jetpack Compose](https://developer.android.com/jetpack/compose/architecture#udf) for Kotlin are implemented based on ideas of UDF. That's why we in inDriver decided to develop our own UDF library for our purposes. 33 | 34 | ## Advantages 35 | 36 | Here are the main advantages of this UDF implementation: 37 | * **Testable**. All domain logic implements in pure functions, so it's easy to unit test it. All UI depends only on provided data, so it's easy to configure and cover by snapshot tests. 38 | * **Scalable and Reusable**. Low Coupling and High Cohesion are examples of the basic principles of good software design. UDF implements such principles in practice. It allows to decouple UI and Domain, create reusable features, and scale business logic in a convenient way. 39 | * **Easy working with concurrency**. The UDF obviously doesn't solve all potential concurrent problems. But it alleviates working with concurrency in regular cases. State updating always runs on separate serial queue. It guarantees the consistency of a state after any changes. For UI or an async task you can use ViewComponent or ServiceComponent protocols respectively. They will subscribe your components on main or background thread so you can concentrate on business tasks rather than concurrency. 40 | * **Free of FRP frameworks**. We decided not to use functional reactive frameworks in our library. Instead, we provided it with a convenient way for subscribing to state updates, so in most cases, you don't even need to know how it works. The absence of FRP frameworks also means that you can't use the UDF with SwiftUI right now. But We plan to add Combine version of the UDF in near future. It will only affect the subscription process, so you will not have to rewrite your domain logic. 41 | 42 | Differences from other popular UDF implementations: 43 | 44 | [RxFeedback](https://github.com/NoTests/RxFeedback.swift) - requires RxSwift 45 | 46 | [The Composable Architecture](https://github.com/pointfreeco/swift-composable-architecture) - iOS13+ only because of Combine 47 | 48 | [ReSwift](https://github.com/ReSwift/ReSwift) - no instruments for modularization 49 | 50 | ## Basic Usage 51 | 52 | Let's imagine a simple counter app. It shows a counter label and two buttons "+" and "-" to increment and decrement the counter. Let's consider the stages of its creation. 53 | 54 | ### Building Domain 55 | 56 | Firstly we need to declare the state of the app: 57 | 58 | ```swift 59 | struct AppState: Equatable { 60 | var counter = 0 61 | } 62 | ``` 63 | **State** is all the data of an app. In our case, it's just an int counter. 64 | Next, we need to know when buttons are tapped: 65 | 66 | ```swift 67 | enum CounterAction: Action { 68 | case decrementButtonTapped 69 | case incrementButtonTapped 70 | } 71 | ``` 72 | We use an enum and an `Action` protocol for that. **Action** describes all of the actions that can occur in your app. 73 | Next, we need to update our state according to an action: 74 | 75 | ```swift 76 | func counterReducer(state: inout AppState, action: Action) { 77 | guard let action = action as? CounterAction else { return } 78 | 79 | switch action { 80 | case .decrementButtonTapped: 81 | state.counter -= 1 82 | case .incrementButtonTapped: 83 | state.counter += 1 84 | } 85 | } 86 | ``` 87 | **Reducer** is a pure function that updates a state. 88 | Now we need to glue it together: 89 | 90 | ```swift 91 | let store = Store(state: .init(), reducer: counterReducer) 92 | ``` 93 | **Store** combines all the above things together. 94 | 95 | ### View Component 96 | 97 | Let's take a look at UI now: 98 | ```swift 99 | class CounterViewController: UIViewController, ViewComponent { 100 | 101 | typealias Props = Int 102 | 103 | @IBOutlet var counterLabel: UILabel! 104 | var disposer = Disposer() 105 | 106 | var props: Int = 0 { 107 | didSet { 108 | guard isViewLoaded else { return } 109 | counterLabel.text = "\(props)" 110 | } 111 | } 112 | 113 | @IBAction func decreaseButtonTapped(_ sender: UIButton) { 114 | store.dispatch(CounterAction.decrementButtonTapped) 115 | } 116 | 117 | @IBAction func increaseButtonTapped(_ sender: UIButton) { 118 | store.dispatch(CounterAction.incrementButtonTapped) 119 | } 120 | } 121 | ``` 122 | `CounterViewController` implements `ViewComponent` protocol. It guarantees that a component receives a new state only if the state was changed and this process always occurs in the main thread. In `CounterViewController` we declare props property and update UI in its didSet. Now we have to connect our ViewController to the store: 123 | 124 | ```swift 125 | let counterViewController = CounterViewController() 126 | counterViewController.connect(to: store, state: \.counter) 127 | ``` 128 | Notice that we can choose witch part of the state we want to observe. 129 | 130 | ### Modularization 131 | 132 | Imagine that you would like to reuse your `CounterViewController` in another app. Or you have a much bigger reusable feature with many View Controllers. In this case, your AppState will look like this: 133 | 134 | ```swift 135 | struct AppState: Equatable { 136 | var counter = 0 137 | var bigFeature = BigFeature() 138 | } 139 | ``` 140 | Obviously you don't want your features to know about AppState. You can easily decouple them by scope: 141 | 142 | ```swift 143 | let store = Store(state: .init(), reducer: counterReducer) 144 | 145 | connectCounter(to: store.scope(\.counter)) 146 | connectBigFeature(to: store.scope(\.bigFeature)) 147 | 148 | ... 149 | 150 | //Somewhere in Counter.framework 151 | func connectCounter(to store: Store) { 152 | ... 153 | counterViewController.connect(to: store) 154 | } 155 | 156 | //Somewhere in BigFeature.framework 157 | func connectCounter(to store: Store) { 158 | ... 159 | firstViewController.connect(to: store, state: \.first) 160 | } 161 | 162 | ``` 163 | Now you can move your features to separate frameworks and use them wherever you want. 164 | 165 | ## Installation 166 | 167 | You can add the UDF to an Xcode project by adding it as a package dependency. 168 | 169 | 1. Open File › Swift Packages › Add Package Dependency… 170 | 2. Enter `https://github.com/inDriver/UDF` 171 | 3. Choose the last version 172 | 173 | ## Inspiration & Acknowledgments 174 | 175 | The UDF idea is based on [Alexey Demedetskiy](https://github.com/AlexeyDemedetskiy) ideas and [MaximBazarov](https://github.com/MaximBazarov) implementation of Unidirection Data Flow Pattern. Originally the UDF was a fork of [Unicore](https://github.com/MaximBazarov/Unicore). 176 | 177 | [The Composable Architecture](https://github.com/pointfreeco/swift-composable-architecture) inspired our implementation of a scope function and modularisation. 178 | 179 | Also, we would like to thank all people that took part in development, testing, and using the UDF: 180 | [Artem Lytkin](https://github.com/artem-lytkin), [Ivan Dyagilev](https://github.com/jinnerrer), [Andrey Zamorshchikov](https://github.com/andreyzamorshchikov), [Dmitry Filippov](https://github.com/DimFilippov), [Eldar Adelshin](https://github.com/AdelshinEldar), [Anton Nochnoy](https://github.com/nochnoy-anton), [Denis Sidorenko](https://github.com/justaSid), [Ilya Kuznetsov](https://github.com/imkuznetsov) and [Viktor Gordienko](https://github.com/v-gordienko). 181 | 182 | If you have any questions or suggestions, please do not hesitate to contact [Anton Goncharov](https://github.com/MasterWatcher) or [Yuri Trykov](https://github.com/trykovyura). 183 | 184 | ## License 185 | 186 | UDF is released under the Apache 2.0 license. [See LICENSE](https://github.com/inDriver/UDF/blob/master/LICENSE) for details. 187 | 188 | Copyright 2021 Suol Innovations Ltd. 189 | 190 | Licensed under the Apache License, Version 2.0 (the "License"); 191 | you may not use this file except in compliance with the License. 192 | You may obtain a copy of the License at 193 | 194 | http://www.apache.org/licenses/LICENSE-2.0 195 | 196 | Unless required by applicable law or agreed to in writing, software 197 | distributed under the License is distributed on an "AS IS" BASIS, 198 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 199 | See the License for the specific language governing permissions and 200 | limitations under the License. 201 | -------------------------------------------------------------------------------- /Scripts/build_docc.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | xcodebuild docbuild \ 4 | -scheme UDF \ 5 | -derivedDataPath /tmp/docbuild/udf \ 6 | -destination 'generic/platform=iOS' 7 | 8 | $(xcrun --find docc) process-archive \ 9 | transform-for-static-hosting /tmp/docbuild/udf/Build/Products/Debug-iphoneos/UDF.doccarchive \ 10 | --output-path docs \ 11 | --hosting-base-path UDF 12 | 13 | echo "" > docs/index.html 14 | -------------------------------------------------------------------------------- /UDF.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 7D1847E72747DE510027823F /* Propsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D1847E62747DE500027823F /* Propsable.swift */; }; 11 | 7D7894AF29A506F000DD20CE /* ViewStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D7894AE29A506F000DD20CE /* ViewStore.swift */; }; 12 | 7D8020FA27266F6C0088B46D /* ActionListenerConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8020F927266F6C0088B46D /* ActionListenerConnector.swift */; }; 13 | 7D8020FC27266FDF0088B46D /* ClosureActionListenerConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8020FB27266FDF0088B46D /* ClosureActionListenerConnector.swift */; }; 14 | 7D8020FE2726700E0088B46D /* ServiceActionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8020FD2726700E0088B46D /* ServiceActionListener.swift */; }; 15 | 7D802100272670370088B46D /* ViewActionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8020FF272670370088B46D /* ViewActionListener.swift */; }; 16 | 7D802102272682420088B46D /* ClosureConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D802101272682420088B46D /* ClosureConnector.swift */; }; 17 | 7D802104272682E70088B46D /* FakeActionListenerConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D802103272682E70088B46D /* FakeActionListenerConnector.swift */; }; 18 | 7D8021062726834A0088B46D /* FakeActionListenerAndConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8021052726834A0088B46D /* FakeActionListenerAndConnector.swift */; }; 19 | 7D93C265291E7B27008B1D6D /* SideEffectProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D93C264291E7B27008B1D6D /* SideEffectProtocol.swift */; }; 20 | 7D93C268291E7BCC008B1D6D /* CombineSideEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D93C267291E7BCC008B1D6D /* CombineSideEffect.swift */; }; 21 | 7D93C26A291E7BE1008B1D6D /* SideEffectTypealiases.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D93C269291E7BE1008B1D6D /* SideEffectTypealiases.swift */; }; 22 | 7D93C26C291E7C37008B1D6D /* SideEffectsBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D93C26B291E7C37008B1D6D /* SideEffectsBuilder.swift */; }; 23 | 7D93C26E291E86A1008B1D6D /* FakeSideEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D93C26D291E86A1008B1D6D /* FakeSideEffect.swift */; }; 24 | 7DE8FE8C298BB8FB009E212C /* Component+stateToProps.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE8FE89298BB8FB009E212C /* Component+stateToProps.swift */; }; 25 | 7DE8FE8D298BB8FB009E212C /* Component+Connector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE8FE8A298BB8FB009E212C /* Component+Connector.swift */; }; 26 | 7DE8FE8E298BB8FB009E212C /* Component+SelfConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE8FE8B298BB8FB009E212C /* Component+SelfConnector.swift */; }; 27 | 7DE8FE90298BBAFC009E212C /* Component+ConnectorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE8FE8F298BBAFC009E212C /* Component+ConnectorTests.swift */; }; 28 | 7DE8FE92298BBCC2009E212C /* Component+SelfConnectorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE8FE91298BBCC2009E212C /* Component+SelfConnectorTests.swift */; }; 29 | 7DE8FE94298BBCD2009E212C /* Component+stateToPropsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE8FE93298BBCD2009E212C /* Component+stateToPropsTests.swift */; }; 30 | 7DFFAB47290979D600804542 /* SideEffectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DFFAB46290979D600804542 /* SideEffectTests.swift */; }; 31 | 9E45E1862694A640003B0321 /* Subscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E17C2694A640003B0321 /* Subscription.swift */; }; 32 | 9E45E1882694A640003B0321 /* ActionDispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E17E2694A640003B0321 /* ActionDispatcher.swift */; }; 33 | 9E45E18A2694A640003B0321 /* Store.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1802694A640003B0321 /* Store.swift */; }; 34 | 9E45E18B2694A640003B0321 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1812694A640003B0321 /* Utils.swift */; }; 35 | 9E45E18C2694A640003B0321 /* Reducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1822694A640003B0321 /* Reducer.swift */; }; 36 | 9E45E18D2694A640003B0321 /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1832694A640003B0321 /* Action.swift */; }; 37 | 9E45E18F2694A640003B0321 /* Disposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1852694A640003B0321 /* Disposer.swift */; }; 38 | 9E45E19C2694A647003B0321 /* ActionListenerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1952694A647003B0321 /* ActionListenerTests.swift */; }; 39 | 9E45E19D2694A647003B0321 /* StoreScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1962694A647003B0321 /* StoreScopeTests.swift */; }; 40 | 9E45E19F2694A647003B0321 /* StoreBasicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1982694A647003B0321 /* StoreBasicTests.swift */; }; 41 | 9E45E1A02694A647003B0321 /* StoreDoubleScopeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1992694A647003B0321 /* StoreDoubleScopeTests.swift */; }; 42 | 9E45E1B42694A701003B0321 /* Connector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1AC2694A701003B0321 /* Connector.swift */; }; 43 | 9E45E1B52694A701003B0321 /* Mapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1AD2694A701003B0321 /* Mapper.swift */; }; 44 | 9E45E1B62694A701003B0321 /* ActionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1AF2694A701003B0321 /* ActionListener.swift */; }; 45 | 9E45E1B72694A701003B0321 /* Component.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1B12694A701003B0321 /* Component.swift */; }; 46 | 9E45E1B82694A701003B0321 /* ViewComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1B22694A701003B0321 /* ViewComponent.swift */; }; 47 | 9E45E1B92694A701003B0321 /* ServiceComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1B32694A701003B0321 /* ServiceComponent.swift */; }; 48 | 9E45E1C52694A713003B0321 /* FakeComponentConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1BF2694A713003B0321 /* FakeComponentConnector.swift */; }; 49 | 9E45E1C62694A713003B0321 /* TestState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1C02694A713003B0321 /* TestState.swift */; }; 50 | 9E45E1C72694A713003B0321 /* FakeComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1C12694A713003B0321 /* FakeComponent.swift */; }; 51 | 9E45E1C82694A713003B0321 /* FakeConnector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1C22694A713003B0321 /* FakeConnector.swift */; }; 52 | 9E45E1C92694A713003B0321 /* FakeActionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1C32694A713003B0321 /* FakeActionListener.swift */; }; 53 | 9E45E1CA2694A713003B0321 /* FakeAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E45E1C42694A713003B0321 /* FakeAction.swift */; }; 54 | 9EBAC3F12694A5CC008EDFD5 /* UDF.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EBAC3E72694A5CC008EDFD5 /* UDF.framework */; }; 55 | 9EBAC3F82694A5CC008EDFD5 /* UDF.h in Headers */ = {isa = PBXBuildFile; fileRef = 9EBAC3EA2694A5CC008EDFD5 /* UDF.h */; settings = {ATTRIBUTES = (Public, ); }; }; 56 | /* End PBXBuildFile section */ 57 | 58 | /* Begin PBXContainerItemProxy section */ 59 | 9EBAC3F22694A5CC008EDFD5 /* PBXContainerItemProxy */ = { 60 | isa = PBXContainerItemProxy; 61 | containerPortal = 9EBAC3DE2694A5CC008EDFD5 /* Project object */; 62 | proxyType = 1; 63 | remoteGlobalIDString = 9EBAC3E62694A5CC008EDFD5; 64 | remoteInfo = UDF; 65 | }; 66 | /* End PBXContainerItemProxy section */ 67 | 68 | /* Begin PBXFileReference section */ 69 | 7D1847E62747DE500027823F /* Propsable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Propsable.swift; sourceTree = ""; }; 70 | 7D7894AE29A506F000DD20CE /* ViewStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewStore.swift; sourceTree = ""; }; 71 | 7D8020F927266F6C0088B46D /* ActionListenerConnector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionListenerConnector.swift; sourceTree = ""; }; 72 | 7D8020FB27266FDF0088B46D /* ClosureActionListenerConnector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClosureActionListenerConnector.swift; sourceTree = ""; }; 73 | 7D8020FD2726700E0088B46D /* ServiceActionListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceActionListener.swift; sourceTree = ""; }; 74 | 7D8020FF272670370088B46D /* ViewActionListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewActionListener.swift; sourceTree = ""; }; 75 | 7D802101272682420088B46D /* ClosureConnector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClosureConnector.swift; sourceTree = ""; }; 76 | 7D802103272682E70088B46D /* FakeActionListenerConnector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeActionListenerConnector.swift; sourceTree = ""; }; 77 | 7D8021052726834A0088B46D /* FakeActionListenerAndConnector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeActionListenerAndConnector.swift; sourceTree = ""; }; 78 | 7D93C264291E7B27008B1D6D /* SideEffectProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideEffectProtocol.swift; sourceTree = ""; }; 79 | 7D93C267291E7BCC008B1D6D /* CombineSideEffect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineSideEffect.swift; sourceTree = ""; }; 80 | 7D93C269291E7BE1008B1D6D /* SideEffectTypealiases.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideEffectTypealiases.swift; sourceTree = ""; }; 81 | 7D93C26B291E7C37008B1D6D /* SideEffectsBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideEffectsBuilder.swift; sourceTree = ""; }; 82 | 7D93C26D291E86A1008B1D6D /* FakeSideEffect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeSideEffect.swift; sourceTree = ""; }; 83 | 7DE8FE89298BB8FB009E212C /* Component+stateToProps.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Component+stateToProps.swift"; sourceTree = ""; }; 84 | 7DE8FE8A298BB8FB009E212C /* Component+Connector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Component+Connector.swift"; sourceTree = ""; }; 85 | 7DE8FE8B298BB8FB009E212C /* Component+SelfConnector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Component+SelfConnector.swift"; sourceTree = ""; }; 86 | 7DE8FE8F298BBAFC009E212C /* Component+ConnectorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Component+ConnectorTests.swift"; sourceTree = ""; }; 87 | 7DE8FE91298BBCC2009E212C /* Component+SelfConnectorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Component+SelfConnectorTests.swift"; sourceTree = ""; }; 88 | 7DE8FE93298BBCD2009E212C /* Component+stateToPropsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Component+stateToPropsTests.swift"; sourceTree = ""; }; 89 | 7DFFAB46290979D600804542 /* SideEffectTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SideEffectTests.swift; sourceTree = ""; }; 90 | 9E45E17C2694A640003B0321 /* Subscription.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Subscription.swift; sourceTree = ""; }; 91 | 9E45E17E2694A640003B0321 /* ActionDispatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionDispatcher.swift; sourceTree = ""; }; 92 | 9E45E1802694A640003B0321 /* Store.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Store.swift; sourceTree = ""; }; 93 | 9E45E1812694A640003B0321 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = ""; }; 94 | 9E45E1822694A640003B0321 /* Reducer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reducer.swift; sourceTree = ""; }; 95 | 9E45E1832694A640003B0321 /* Action.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Action.swift; sourceTree = ""; }; 96 | 9E45E1852694A640003B0321 /* Disposer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Disposer.swift; sourceTree = ""; }; 97 | 9E45E1952694A647003B0321 /* ActionListenerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionListenerTests.swift; sourceTree = ""; }; 98 | 9E45E1962694A647003B0321 /* StoreScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoreScopeTests.swift; sourceTree = ""; }; 99 | 9E45E1982694A647003B0321 /* StoreBasicTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoreBasicTests.swift; sourceTree = ""; }; 100 | 9E45E1992694A647003B0321 /* StoreDoubleScopeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoreDoubleScopeTests.swift; sourceTree = ""; }; 101 | 9E45E1AC2694A701003B0321 /* Connector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Connector.swift; sourceTree = ""; }; 102 | 9E45E1AD2694A701003B0321 /* Mapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mapper.swift; sourceTree = ""; }; 103 | 9E45E1AF2694A701003B0321 /* ActionListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionListener.swift; sourceTree = ""; }; 104 | 9E45E1B12694A701003B0321 /* Component.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Component.swift; sourceTree = ""; }; 105 | 9E45E1B22694A701003B0321 /* ViewComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewComponent.swift; sourceTree = ""; }; 106 | 9E45E1B32694A701003B0321 /* ServiceComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceComponent.swift; sourceTree = ""; }; 107 | 9E45E1BF2694A713003B0321 /* FakeComponentConnector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeComponentConnector.swift; sourceTree = ""; }; 108 | 9E45E1C02694A713003B0321 /* TestState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestState.swift; sourceTree = ""; }; 109 | 9E45E1C12694A713003B0321 /* FakeComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeComponent.swift; sourceTree = ""; }; 110 | 9E45E1C22694A713003B0321 /* FakeConnector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeConnector.swift; sourceTree = ""; }; 111 | 9E45E1C32694A713003B0321 /* FakeActionListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeActionListener.swift; sourceTree = ""; }; 112 | 9E45E1C42694A713003B0321 /* FakeAction.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FakeAction.swift; sourceTree = ""; }; 113 | 9EBAC3E72694A5CC008EDFD5 /* UDF.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = UDF.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 114 | 9EBAC3EA2694A5CC008EDFD5 /* UDF.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UDF.h; sourceTree = ""; }; 115 | 9EBAC3EB2694A5CC008EDFD5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 116 | 9EBAC3F02694A5CC008EDFD5 /* UDFTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = UDFTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 117 | 9EBAC3F72694A5CC008EDFD5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 118 | /* End PBXFileReference section */ 119 | 120 | /* Begin PBXFrameworksBuildPhase section */ 121 | 9EBAC3E42694A5CC008EDFD5 /* Frameworks */ = { 122 | isa = PBXFrameworksBuildPhase; 123 | buildActionMask = 2147483647; 124 | files = ( 125 | ); 126 | runOnlyForDeploymentPostprocessing = 0; 127 | }; 128 | 9EBAC3ED2694A5CC008EDFD5 /* Frameworks */ = { 129 | isa = PBXFrameworksBuildPhase; 130 | buildActionMask = 2147483647; 131 | files = ( 132 | 9EBAC3F12694A5CC008EDFD5 /* UDF.framework in Frameworks */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXFrameworksBuildPhase section */ 137 | 138 | /* Begin PBXGroup section */ 139 | 7D8020F827266F2C0088B46D /* Connector */ = { 140 | isa = PBXGroup; 141 | children = ( 142 | 7D8020F927266F6C0088B46D /* ActionListenerConnector.swift */, 143 | 7D8020FB27266FDF0088B46D /* ClosureActionListenerConnector.swift */, 144 | ); 145 | path = Connector; 146 | sourceTree = ""; 147 | }; 148 | 7D93C266291E7BB2008B1D6D /* SideEffect */ = { 149 | isa = PBXGroup; 150 | children = ( 151 | 7D93C264291E7B27008B1D6D /* SideEffectProtocol.swift */, 152 | 7D93C267291E7BCC008B1D6D /* CombineSideEffect.swift */, 153 | 7D93C269291E7BE1008B1D6D /* SideEffectTypealiases.swift */, 154 | 7D93C26B291E7C37008B1D6D /* SideEffectsBuilder.swift */, 155 | ); 156 | path = SideEffect; 157 | sourceTree = ""; 158 | }; 159 | 9E45E1AB2694A701003B0321 /* Connector */ = { 160 | isa = PBXGroup; 161 | children = ( 162 | 7D802101272682420088B46D /* ClosureConnector.swift */, 163 | 9E45E1AC2694A701003B0321 /* Connector.swift */, 164 | 9E45E1AD2694A701003B0321 /* Mapper.swift */, 165 | ); 166 | path = Connector; 167 | sourceTree = ""; 168 | }; 169 | 9E45E1AE2694A701003B0321 /* ActionListener */ = { 170 | isa = PBXGroup; 171 | children = ( 172 | 7D8020F827266F2C0088B46D /* Connector */, 173 | 9E45E1AF2694A701003B0321 /* ActionListener.swift */, 174 | 7D8020FD2726700E0088B46D /* ServiceActionListener.swift */, 175 | 7D8020FF272670370088B46D /* ViewActionListener.swift */, 176 | ); 177 | path = ActionListener; 178 | sourceTree = ""; 179 | }; 180 | 9E45E1B02694A701003B0321 /* Component */ = { 181 | isa = PBXGroup; 182 | children = ( 183 | 9E45E1B12694A701003B0321 /* Component.swift */, 184 | 7DE8FE8A298BB8FB009E212C /* Component+Connector.swift */, 185 | 7DE8FE8B298BB8FB009E212C /* Component+SelfConnector.swift */, 186 | 7DE8FE89298BB8FB009E212C /* Component+stateToProps.swift */, 187 | 9E45E1B22694A701003B0321 /* ViewComponent.swift */, 188 | 9E45E1B32694A701003B0321 /* ServiceComponent.swift */, 189 | 7D1847E62747DE500027823F /* Propsable.swift */, 190 | ); 191 | path = Component; 192 | sourceTree = ""; 193 | }; 194 | 9E45E1BE2694A713003B0321 /* TestDoubles */ = { 195 | isa = PBXGroup; 196 | children = ( 197 | 9E45E1BF2694A713003B0321 /* FakeComponentConnector.swift */, 198 | 9E45E1C02694A713003B0321 /* TestState.swift */, 199 | 9E45E1C12694A713003B0321 /* FakeComponent.swift */, 200 | 9E45E1C22694A713003B0321 /* FakeConnector.swift */, 201 | 9E45E1C32694A713003B0321 /* FakeActionListener.swift */, 202 | 9E45E1C42694A713003B0321 /* FakeAction.swift */, 203 | 7D802103272682E70088B46D /* FakeActionListenerConnector.swift */, 204 | 7D8021052726834A0088B46D /* FakeActionListenerAndConnector.swift */, 205 | 7D93C26D291E86A1008B1D6D /* FakeSideEffect.swift */, 206 | ); 207 | path = TestDoubles; 208 | sourceTree = ""; 209 | }; 210 | 9EBAC3DD2694A5CC008EDFD5 = { 211 | isa = PBXGroup; 212 | children = ( 213 | 9EBAC3E92694A5CC008EDFD5 /* UDF */, 214 | 9EBAC3F42694A5CC008EDFD5 /* UDFTests */, 215 | 9EBAC3E82694A5CC008EDFD5 /* Products */, 216 | ); 217 | sourceTree = ""; 218 | }; 219 | 9EBAC3E82694A5CC008EDFD5 /* Products */ = { 220 | isa = PBXGroup; 221 | children = ( 222 | 9EBAC3E72694A5CC008EDFD5 /* UDF.framework */, 223 | 9EBAC3F02694A5CC008EDFD5 /* UDFTests.xctest */, 224 | ); 225 | name = Products; 226 | sourceTree = ""; 227 | }; 228 | 9EBAC3E92694A5CC008EDFD5 /* UDF */ = { 229 | isa = PBXGroup; 230 | children = ( 231 | 7D93C266291E7BB2008B1D6D /* SideEffect */, 232 | 9E45E1AE2694A701003B0321 /* ActionListener */, 233 | 9E45E1B02694A701003B0321 /* Component */, 234 | 9E45E1AB2694A701003B0321 /* Connector */, 235 | 9E45E1832694A640003B0321 /* Action.swift */, 236 | 9E45E17E2694A640003B0321 /* ActionDispatcher.swift */, 237 | 9E45E1852694A640003B0321 /* Disposer.swift */, 238 | 9E45E1822694A640003B0321 /* Reducer.swift */, 239 | 9E45E1802694A640003B0321 /* Store.swift */, 240 | 7D7894AE29A506F000DD20CE /* ViewStore.swift */, 241 | 9E45E17C2694A640003B0321 /* Subscription.swift */, 242 | 9E45E1812694A640003B0321 /* Utils.swift */, 243 | 9EBAC3EA2694A5CC008EDFD5 /* UDF.h */, 244 | 9EBAC3EB2694A5CC008EDFD5 /* Info.plist */, 245 | ); 246 | path = UDF; 247 | sourceTree = ""; 248 | }; 249 | 9EBAC3F42694A5CC008EDFD5 /* UDFTests */ = { 250 | isa = PBXGroup; 251 | children = ( 252 | 9E45E1BE2694A713003B0321 /* TestDoubles */, 253 | 9E45E1952694A647003B0321 /* ActionListenerTests.swift */, 254 | 7DE8FE8F298BBAFC009E212C /* Component+ConnectorTests.swift */, 255 | 7DE8FE91298BBCC2009E212C /* Component+SelfConnectorTests.swift */, 256 | 7DE8FE93298BBCD2009E212C /* Component+stateToPropsTests.swift */, 257 | 9E45E1982694A647003B0321 /* StoreBasicTests.swift */, 258 | 9E45E1992694A647003B0321 /* StoreDoubleScopeTests.swift */, 259 | 9E45E1962694A647003B0321 /* StoreScopeTests.swift */, 260 | 7DFFAB46290979D600804542 /* SideEffectTests.swift */, 261 | 9EBAC3F72694A5CC008EDFD5 /* Info.plist */, 262 | ); 263 | path = UDFTests; 264 | sourceTree = ""; 265 | }; 266 | /* End PBXGroup section */ 267 | 268 | /* Begin PBXHeadersBuildPhase section */ 269 | 9EBAC3E22694A5CC008EDFD5 /* Headers */ = { 270 | isa = PBXHeadersBuildPhase; 271 | buildActionMask = 2147483647; 272 | files = ( 273 | 9EBAC3F82694A5CC008EDFD5 /* UDF.h in Headers */, 274 | ); 275 | runOnlyForDeploymentPostprocessing = 0; 276 | }; 277 | /* End PBXHeadersBuildPhase section */ 278 | 279 | /* Begin PBXNativeTarget section */ 280 | 9EBAC3E62694A5CC008EDFD5 /* UDF */ = { 281 | isa = PBXNativeTarget; 282 | buildConfigurationList = 9EBAC3FB2694A5CC008EDFD5 /* Build configuration list for PBXNativeTarget "UDF" */; 283 | buildPhases = ( 284 | 9EBAC3E22694A5CC008EDFD5 /* Headers */, 285 | 9EBAC3E32694A5CC008EDFD5 /* Sources */, 286 | 9EBAC3E42694A5CC008EDFD5 /* Frameworks */, 287 | 9EBAC3E52694A5CC008EDFD5 /* Resources */, 288 | ); 289 | buildRules = ( 290 | ); 291 | dependencies = ( 292 | ); 293 | name = UDF; 294 | productName = UDF; 295 | productReference = 9EBAC3E72694A5CC008EDFD5 /* UDF.framework */; 296 | productType = "com.apple.product-type.framework"; 297 | }; 298 | 9EBAC3EF2694A5CC008EDFD5 /* UDFTests */ = { 299 | isa = PBXNativeTarget; 300 | buildConfigurationList = 9EBAC3FE2694A5CC008EDFD5 /* Build configuration list for PBXNativeTarget "UDFTests" */; 301 | buildPhases = ( 302 | 9EBAC3EC2694A5CC008EDFD5 /* Sources */, 303 | 9EBAC3ED2694A5CC008EDFD5 /* Frameworks */, 304 | 9EBAC3EE2694A5CC008EDFD5 /* Resources */, 305 | ); 306 | buildRules = ( 307 | ); 308 | dependencies = ( 309 | 9EBAC3F32694A5CC008EDFD5 /* PBXTargetDependency */, 310 | ); 311 | name = UDFTests; 312 | productName = UDFTests; 313 | productReference = 9EBAC3F02694A5CC008EDFD5 /* UDFTests.xctest */; 314 | productType = "com.apple.product-type.bundle.unit-test"; 315 | }; 316 | /* End PBXNativeTarget section */ 317 | 318 | /* Begin PBXProject section */ 319 | 9EBAC3DE2694A5CC008EDFD5 /* Project object */ = { 320 | isa = PBXProject; 321 | attributes = { 322 | LastSwiftUpdateCheck = 1220; 323 | LastUpgradeCheck = 1220; 324 | TargetAttributes = { 325 | 9EBAC3E62694A5CC008EDFD5 = { 326 | CreatedOnToolsVersion = 12.2; 327 | LastSwiftMigration = 1220; 328 | }; 329 | 9EBAC3EF2694A5CC008EDFD5 = { 330 | CreatedOnToolsVersion = 12.2; 331 | }; 332 | }; 333 | }; 334 | buildConfigurationList = 9EBAC3E12694A5CC008EDFD5 /* Build configuration list for PBXProject "UDF" */; 335 | compatibilityVersion = "Xcode 9.3"; 336 | developmentRegion = en; 337 | hasScannedForEncodings = 0; 338 | knownRegions = ( 339 | en, 340 | Base, 341 | ); 342 | mainGroup = 9EBAC3DD2694A5CC008EDFD5; 343 | productRefGroup = 9EBAC3E82694A5CC008EDFD5 /* Products */; 344 | projectDirPath = ""; 345 | projectRoot = ""; 346 | targets = ( 347 | 9EBAC3E62694A5CC008EDFD5 /* UDF */, 348 | 9EBAC3EF2694A5CC008EDFD5 /* UDFTests */, 349 | ); 350 | }; 351 | /* End PBXProject section */ 352 | 353 | /* Begin PBXResourcesBuildPhase section */ 354 | 9EBAC3E52694A5CC008EDFD5 /* Resources */ = { 355 | isa = PBXResourcesBuildPhase; 356 | buildActionMask = 2147483647; 357 | files = ( 358 | ); 359 | runOnlyForDeploymentPostprocessing = 0; 360 | }; 361 | 9EBAC3EE2694A5CC008EDFD5 /* Resources */ = { 362 | isa = PBXResourcesBuildPhase; 363 | buildActionMask = 2147483647; 364 | files = ( 365 | ); 366 | runOnlyForDeploymentPostprocessing = 0; 367 | }; 368 | /* End PBXResourcesBuildPhase section */ 369 | 370 | /* Begin PBXSourcesBuildPhase section */ 371 | 9EBAC3E32694A5CC008EDFD5 /* Sources */ = { 372 | isa = PBXSourcesBuildPhase; 373 | buildActionMask = 2147483647; 374 | files = ( 375 | 7D93C26A291E7BE1008B1D6D /* SideEffectTypealiases.swift in Sources */, 376 | 9E45E18A2694A640003B0321 /* Store.swift in Sources */, 377 | 7D802102272682420088B46D /* ClosureConnector.swift in Sources */, 378 | 7DE8FE8E298BB8FB009E212C /* Component+SelfConnector.swift in Sources */, 379 | 7D93C268291E7BCC008B1D6D /* CombineSideEffect.swift in Sources */, 380 | 9E45E1B62694A701003B0321 /* ActionListener.swift in Sources */, 381 | 9E45E18D2694A640003B0321 /* Action.swift in Sources */, 382 | 9E45E1882694A640003B0321 /* ActionDispatcher.swift in Sources */, 383 | 9E45E1B72694A701003B0321 /* Component.swift in Sources */, 384 | 9E45E1862694A640003B0321 /* Subscription.swift in Sources */, 385 | 9E45E1B52694A701003B0321 /* Mapper.swift in Sources */, 386 | 7D7894AF29A506F000DD20CE /* ViewStore.swift in Sources */, 387 | 9E45E18F2694A640003B0321 /* Disposer.swift in Sources */, 388 | 7D93C265291E7B27008B1D6D /* SideEffectProtocol.swift in Sources */, 389 | 9E45E1B82694A701003B0321 /* ViewComponent.swift in Sources */, 390 | 7DE8FE8C298BB8FB009E212C /* Component+stateToProps.swift in Sources */, 391 | 9E45E1B42694A701003B0321 /* Connector.swift in Sources */, 392 | 9E45E18C2694A640003B0321 /* Reducer.swift in Sources */, 393 | 7D8020FE2726700E0088B46D /* ServiceActionListener.swift in Sources */, 394 | 7D8020FC27266FDF0088B46D /* ClosureActionListenerConnector.swift in Sources */, 395 | 7D93C26C291E7C37008B1D6D /* SideEffectsBuilder.swift in Sources */, 396 | 7D1847E72747DE510027823F /* Propsable.swift in Sources */, 397 | 9E45E18B2694A640003B0321 /* Utils.swift in Sources */, 398 | 9E45E1B92694A701003B0321 /* ServiceComponent.swift in Sources */, 399 | 7D802100272670370088B46D /* ViewActionListener.swift in Sources */, 400 | 7D8020FA27266F6C0088B46D /* ActionListenerConnector.swift in Sources */, 401 | 7DE8FE8D298BB8FB009E212C /* Component+Connector.swift in Sources */, 402 | ); 403 | runOnlyForDeploymentPostprocessing = 0; 404 | }; 405 | 9EBAC3EC2694A5CC008EDFD5 /* Sources */ = { 406 | isa = PBXSourcesBuildPhase; 407 | buildActionMask = 2147483647; 408 | files = ( 409 | 9E45E1C72694A713003B0321 /* FakeComponent.swift in Sources */, 410 | 9E45E1A02694A647003B0321 /* StoreDoubleScopeTests.swift in Sources */, 411 | 7D802104272682E70088B46D /* FakeActionListenerConnector.swift in Sources */, 412 | 7DE8FE94298BBCD2009E212C /* Component+stateToPropsTests.swift in Sources */, 413 | 9E45E19D2694A647003B0321 /* StoreScopeTests.swift in Sources */, 414 | 7DE8FE90298BBAFC009E212C /* Component+ConnectorTests.swift in Sources */, 415 | 9E45E1C52694A713003B0321 /* FakeComponentConnector.swift in Sources */, 416 | 9E45E1C92694A713003B0321 /* FakeActionListener.swift in Sources */, 417 | 9E45E1C82694A713003B0321 /* FakeConnector.swift in Sources */, 418 | 7D93C26E291E86A1008B1D6D /* FakeSideEffect.swift in Sources */, 419 | 9E45E19C2694A647003B0321 /* ActionListenerTests.swift in Sources */, 420 | 9E45E1C62694A713003B0321 /* TestState.swift in Sources */, 421 | 9E45E19F2694A647003B0321 /* StoreBasicTests.swift in Sources */, 422 | 7D8021062726834A0088B46D /* FakeActionListenerAndConnector.swift in Sources */, 423 | 7DE8FE92298BBCC2009E212C /* Component+SelfConnectorTests.swift in Sources */, 424 | 9E45E1CA2694A713003B0321 /* FakeAction.swift in Sources */, 425 | 7DFFAB47290979D600804542 /* SideEffectTests.swift in Sources */, 426 | ); 427 | runOnlyForDeploymentPostprocessing = 0; 428 | }; 429 | /* End PBXSourcesBuildPhase section */ 430 | 431 | /* Begin PBXTargetDependency section */ 432 | 9EBAC3F32694A5CC008EDFD5 /* PBXTargetDependency */ = { 433 | isa = PBXTargetDependency; 434 | target = 9EBAC3E62694A5CC008EDFD5 /* UDF */; 435 | targetProxy = 9EBAC3F22694A5CC008EDFD5 /* PBXContainerItemProxy */; 436 | }; 437 | /* End PBXTargetDependency section */ 438 | 439 | /* Begin XCBuildConfiguration section */ 440 | 9EBAC3F92694A5CC008EDFD5 /* Debug */ = { 441 | isa = XCBuildConfiguration; 442 | buildSettings = { 443 | ALWAYS_SEARCH_USER_PATHS = NO; 444 | CLANG_ANALYZER_NONNULL = YES; 445 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 446 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 447 | CLANG_CXX_LIBRARY = "libc++"; 448 | CLANG_ENABLE_MODULES = YES; 449 | CLANG_ENABLE_OBJC_ARC = YES; 450 | CLANG_ENABLE_OBJC_WEAK = YES; 451 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 452 | CLANG_WARN_BOOL_CONVERSION = YES; 453 | CLANG_WARN_COMMA = YES; 454 | CLANG_WARN_CONSTANT_CONVERSION = YES; 455 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 456 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 457 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 458 | CLANG_WARN_EMPTY_BODY = YES; 459 | CLANG_WARN_ENUM_CONVERSION = YES; 460 | CLANG_WARN_INFINITE_RECURSION = YES; 461 | CLANG_WARN_INT_CONVERSION = YES; 462 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 463 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 464 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 465 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 466 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 467 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 468 | CLANG_WARN_STRICT_PROTOTYPES = YES; 469 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 470 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 471 | CLANG_WARN_UNREACHABLE_CODE = YES; 472 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 473 | COPY_PHASE_STRIP = NO; 474 | CURRENT_PROJECT_VERSION = 1; 475 | DEBUG_INFORMATION_FORMAT = dwarf; 476 | ENABLE_STRICT_OBJC_MSGSEND = YES; 477 | ENABLE_TESTABILITY = YES; 478 | GCC_C_LANGUAGE_STANDARD = gnu11; 479 | GCC_DYNAMIC_NO_PIC = NO; 480 | GCC_NO_COMMON_BLOCKS = YES; 481 | GCC_OPTIMIZATION_LEVEL = 0; 482 | GCC_PREPROCESSOR_DEFINITIONS = ( 483 | "DEBUG=1", 484 | "$(inherited)", 485 | ); 486 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 487 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 488 | GCC_WARN_UNDECLARED_SELECTOR = YES; 489 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 490 | GCC_WARN_UNUSED_FUNCTION = YES; 491 | GCC_WARN_UNUSED_VARIABLE = YES; 492 | IPHONEOS_DEPLOYMENT_TARGET = 14.2; 493 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 494 | MTL_FAST_MATH = YES; 495 | ONLY_ACTIVE_ARCH = YES; 496 | SDKROOT = iphoneos; 497 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 498 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 499 | VERSIONING_SYSTEM = "apple-generic"; 500 | VERSION_INFO_PREFIX = ""; 501 | }; 502 | name = Debug; 503 | }; 504 | 9EBAC3FA2694A5CC008EDFD5 /* Release */ = { 505 | isa = XCBuildConfiguration; 506 | buildSettings = { 507 | ALWAYS_SEARCH_USER_PATHS = NO; 508 | CLANG_ANALYZER_NONNULL = YES; 509 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 510 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 511 | CLANG_CXX_LIBRARY = "libc++"; 512 | CLANG_ENABLE_MODULES = YES; 513 | CLANG_ENABLE_OBJC_ARC = YES; 514 | CLANG_ENABLE_OBJC_WEAK = YES; 515 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 516 | CLANG_WARN_BOOL_CONVERSION = YES; 517 | CLANG_WARN_COMMA = YES; 518 | CLANG_WARN_CONSTANT_CONVERSION = YES; 519 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 520 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 521 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 522 | CLANG_WARN_EMPTY_BODY = YES; 523 | CLANG_WARN_ENUM_CONVERSION = YES; 524 | CLANG_WARN_INFINITE_RECURSION = YES; 525 | CLANG_WARN_INT_CONVERSION = YES; 526 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 527 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 528 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 529 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 530 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 531 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 532 | CLANG_WARN_STRICT_PROTOTYPES = YES; 533 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 534 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 535 | CLANG_WARN_UNREACHABLE_CODE = YES; 536 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 537 | COPY_PHASE_STRIP = NO; 538 | CURRENT_PROJECT_VERSION = 1; 539 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 540 | ENABLE_NS_ASSERTIONS = NO; 541 | ENABLE_STRICT_OBJC_MSGSEND = YES; 542 | GCC_C_LANGUAGE_STANDARD = gnu11; 543 | GCC_NO_COMMON_BLOCKS = YES; 544 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 545 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 546 | GCC_WARN_UNDECLARED_SELECTOR = YES; 547 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 548 | GCC_WARN_UNUSED_FUNCTION = YES; 549 | GCC_WARN_UNUSED_VARIABLE = YES; 550 | IPHONEOS_DEPLOYMENT_TARGET = 14.2; 551 | MTL_ENABLE_DEBUG_INFO = NO; 552 | MTL_FAST_MATH = YES; 553 | SDKROOT = iphoneos; 554 | SWIFT_COMPILATION_MODE = wholemodule; 555 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 556 | VALIDATE_PRODUCT = YES; 557 | VERSIONING_SYSTEM = "apple-generic"; 558 | VERSION_INFO_PREFIX = ""; 559 | }; 560 | name = Release; 561 | }; 562 | 9EBAC3FC2694A5CC008EDFD5 /* Debug */ = { 563 | isa = XCBuildConfiguration; 564 | buildSettings = { 565 | CLANG_ENABLE_MODULES = YES; 566 | CODE_SIGN_STYLE = Automatic; 567 | DEFINES_MODULE = YES; 568 | DYLIB_COMPATIBILITY_VERSION = 1; 569 | DYLIB_CURRENT_VERSION = 1; 570 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 571 | INFOPLIST_FILE = UDF/Info.plist; 572 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 573 | LD_RUNPATH_SEARCH_PATHS = ( 574 | "$(inherited)", 575 | "@executable_path/Frameworks", 576 | "@loader_path/Frameworks", 577 | ); 578 | PRODUCT_BUNDLE_IDENTIFIER = com.inDriver.UDF; 579 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 580 | SKIP_INSTALL = YES; 581 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 582 | SWIFT_VERSION = 5.0; 583 | TARGETED_DEVICE_FAMILY = "1,2"; 584 | }; 585 | name = Debug; 586 | }; 587 | 9EBAC3FD2694A5CC008EDFD5 /* Release */ = { 588 | isa = XCBuildConfiguration; 589 | buildSettings = { 590 | CLANG_ENABLE_MODULES = YES; 591 | CODE_SIGN_STYLE = Automatic; 592 | DEFINES_MODULE = YES; 593 | DYLIB_COMPATIBILITY_VERSION = 1; 594 | DYLIB_CURRENT_VERSION = 1; 595 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 596 | INFOPLIST_FILE = UDF/Info.plist; 597 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 598 | LD_RUNPATH_SEARCH_PATHS = ( 599 | "$(inherited)", 600 | "@executable_path/Frameworks", 601 | "@loader_path/Frameworks", 602 | ); 603 | PRODUCT_BUNDLE_IDENTIFIER = com.inDriver.UDF; 604 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 605 | SKIP_INSTALL = YES; 606 | SWIFT_VERSION = 5.0; 607 | TARGETED_DEVICE_FAMILY = "1,2"; 608 | }; 609 | name = Release; 610 | }; 611 | 9EBAC3FF2694A5CC008EDFD5 /* Debug */ = { 612 | isa = XCBuildConfiguration; 613 | buildSettings = { 614 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 615 | CODE_SIGN_STYLE = Automatic; 616 | INFOPLIST_FILE = UDFTests/Info.plist; 617 | LD_RUNPATH_SEARCH_PATHS = ( 618 | "$(inherited)", 619 | "@executable_path/Frameworks", 620 | "@loader_path/Frameworks", 621 | ); 622 | PRODUCT_BUNDLE_IDENTIFIER = com.inDriver.UDFTests; 623 | PRODUCT_NAME = "$(TARGET_NAME)"; 624 | SWIFT_VERSION = 5.0; 625 | TARGETED_DEVICE_FAMILY = "1,2"; 626 | }; 627 | name = Debug; 628 | }; 629 | 9EBAC4002694A5CC008EDFD5 /* Release */ = { 630 | isa = XCBuildConfiguration; 631 | buildSettings = { 632 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 633 | CODE_SIGN_STYLE = Automatic; 634 | INFOPLIST_FILE = UDFTests/Info.plist; 635 | LD_RUNPATH_SEARCH_PATHS = ( 636 | "$(inherited)", 637 | "@executable_path/Frameworks", 638 | "@loader_path/Frameworks", 639 | ); 640 | PRODUCT_BUNDLE_IDENTIFIER = com.inDriver.UDFTests; 641 | PRODUCT_NAME = "$(TARGET_NAME)"; 642 | SWIFT_VERSION = 5.0; 643 | TARGETED_DEVICE_FAMILY = "1,2"; 644 | }; 645 | name = Release; 646 | }; 647 | /* End XCBuildConfiguration section */ 648 | 649 | /* Begin XCConfigurationList section */ 650 | 9EBAC3E12694A5CC008EDFD5 /* Build configuration list for PBXProject "UDF" */ = { 651 | isa = XCConfigurationList; 652 | buildConfigurations = ( 653 | 9EBAC3F92694A5CC008EDFD5 /* Debug */, 654 | 9EBAC3FA2694A5CC008EDFD5 /* Release */, 655 | ); 656 | defaultConfigurationIsVisible = 0; 657 | defaultConfigurationName = Release; 658 | }; 659 | 9EBAC3FB2694A5CC008EDFD5 /* Build configuration list for PBXNativeTarget "UDF" */ = { 660 | isa = XCConfigurationList; 661 | buildConfigurations = ( 662 | 9EBAC3FC2694A5CC008EDFD5 /* Debug */, 663 | 9EBAC3FD2694A5CC008EDFD5 /* Release */, 664 | ); 665 | defaultConfigurationIsVisible = 0; 666 | defaultConfigurationName = Release; 667 | }; 668 | 9EBAC3FE2694A5CC008EDFD5 /* Build configuration list for PBXNativeTarget "UDFTests" */ = { 669 | isa = XCConfigurationList; 670 | buildConfigurations = ( 671 | 9EBAC3FF2694A5CC008EDFD5 /* Debug */, 672 | 9EBAC4002694A5CC008EDFD5 /* Release */, 673 | ); 674 | defaultConfigurationIsVisible = 0; 675 | defaultConfigurationName = Release; 676 | }; 677 | /* End XCConfigurationList section */ 678 | }; 679 | rootObject = 9EBAC3DE2694A5CC008EDFD5 /* Project object */; 680 | } 681 | -------------------------------------------------------------------------------- /UDF.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /UDF.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /UDF.xcodeproj/xcshareddata/xcschemes/UDF.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 53 | 54 | 60 | 61 | 67 | 68 | 69 | 70 | 72 | 73 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /UDF/Action.swift: -------------------------------------------------------------------------------- 1 | /// Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | /// `Action` is a marker protocol. 17 | /// All actions dispatched to a ``Store`` should conform to this protocol. 18 | public protocol Action { } 19 | -------------------------------------------------------------------------------- /UDF/ActionDispatcher.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// Use this protocol to abstract ``Action`` dispatching. 19 | public protocol ActionDispatcher { 20 | func dispatch(_ action: Action) 21 | } 22 | -------------------------------------------------------------------------------- /UDF/ActionListener/ActionListener.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// Parent protocol for action listeners. Use ``ViewActionListener`` or ``ServiceActionListener`` for your action listener. 19 | public protocol ActionListener: AnyObject { 20 | 21 | associatedtype Props 22 | 23 | var queue: DispatchQueue { get } 24 | var disposer: Disposer { get } 25 | 26 | func update(props: Props) 27 | 28 | /// Connects an action listener to a store using a connector. 29 | /// 30 | /// - Parameters: 31 | /// - store: A `Store` to connect to. 32 | /// - by: A `ActionListenerConnector` that transforms State to Props. 33 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `ActionListenerConnector`. 34 | func connect( 35 | to store: Store, 36 | by connector: ConnectorType, 37 | transform: @escaping (State) -> ConnectorType.State 38 | ) where ConnectorType.Props == Props 39 | 40 | } 41 | 42 | public extension ActionListener { 43 | 44 | /// Connects an action listener to a store using a connector with whole `Store`'s `State`. 45 | /// 46 | /// - Parameters: 47 | /// - store: A `Store` to connect to. 48 | /// - by: A `ActionListenerConnector` that transforms State to Props. 49 | func connect( 50 | to store: Store, 51 | by connector: ConnectorType 52 | ) where ConnectorType.State == State, ConnectorType.Props == Props { 53 | connect(to: store, by: connector) { $0 } 54 | } 55 | 56 | /// Connects an action listener to a store using a connector and a keypath. 57 | /// 58 | /// - Parameters: 59 | /// - store: A `Store` to connect to. 60 | /// - by: A `ActionListenerConnector` that transforms State to Props. 61 | /// - keypath: A keypath for a `State` of the `ActionListener`. 62 | func connect( 63 | to store: Store, 64 | by connector: ConnectorType, 65 | state keypath: KeyPath 66 | ) where ConnectorType.Props == Props { 67 | connect(to: store, by: connector) { $0[keyPath: keypath] } 68 | } 69 | } 70 | 71 | public extension ActionListener { 72 | 73 | /// Connects an action listener to a store with stateAndActionToProps closure and whole `Store`'s `State`. 74 | /// 75 | /// - Parameters: 76 | /// - store: A `Store` to connect to. 77 | /// - stateAndActionsToProps: A closure that transforms the `Store`'s `State` into a `Props` of the `ActionListener`. 78 | func connect( 79 | to store: Store, 80 | stateAndActionToProps: @escaping (State, Action) -> Props) { 81 | connect(to: store, stateAndActionToProps: stateAndActionToProps) { $0 } 82 | } 83 | 84 | /// Connects an action listener to a store with stateAndActionToProps closure and keypath. 85 | /// 86 | /// - Parameters: 87 | /// - store: A `Store` to connect to. 88 | /// - stateAndActionsToProps: A closure that transforms the `ActionListener`'s `State` and dispatched `Action` into a `Props` of the `ActionListener`. 89 | /// - keypath: A keypath for a `State` of the `ActionListener`. 90 | func connect( 91 | to store: Store, 92 | stateAndActionToProps: @escaping (ConnectorState, Action) -> Props, 93 | state keypath: KeyPath) { 94 | connect(to: store, stateAndActionToProps: stateAndActionToProps) { $0[keyPath: keypath] } 95 | } 96 | 97 | /// Connects an action listener to a store with stateAndActionToProps closure. 98 | /// 99 | /// - Parameters: 100 | /// - store: A ``Store`` to connect to. 101 | /// - stateAndActionsToProps: A closure that transforms the `ActionListener`'s `State` and dispatched `Action` into a `Props` of the `ActionListener`. 102 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `ActionListener`. 103 | func connect( 104 | to store: Store, 105 | stateAndActionToProps: @escaping (ConnectorState, Action) -> Props, 106 | transform: @escaping (State) -> ConnectorState) { 107 | connect(to: store, by: ClosureActionListenerConnector(closure: stateAndActionToProps), transform: transform) 108 | } 109 | } 110 | 111 | public extension ActionListener where Self: ActionListenerConnector { 112 | /// Connects an action listener to a store when the `ActionListener` is a `ActionListenerConnector` and with whole `Store`'s `State`. 113 | /// 114 | /// - Parameters: 115 | /// - store: A `Store` to connect to. 116 | func connect(to store: Store) where Self.State == State { 117 | connect(to: store) { $0 } 118 | } 119 | 120 | /// Connects an action listener to a store when the `ActionListener` is a `ActionListenerConnector`and with a keypath. 121 | /// 122 | /// - Parameters: 123 | /// - store: A `Store` to connect to. 124 | /// - keypath: A keypath for a `State` of the `ActionListener`. 125 | func connect(to store: Store, state keypath: KeyPath) { 126 | connect(to: store) { $0[keyPath: keypath] } 127 | } 128 | 129 | /// Connects an action listener to a store when the `ActionListener` is a `ActionListenerConnector`. 130 | /// 131 | /// - Parameters: 132 | /// - store: A `Store` to connect to. 133 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `ActionListenerConnector`. 134 | func connect(to store: Store, transform: @escaping (State) -> Self.State) { 135 | store.onAction(on: queue) { [weak self] (state, action) in 136 | guard let self = self else { return } 137 | self.updateProps(state: state, action: action, connector: self, transform: transform) 138 | }.dispose(on: disposer) 139 | } 140 | } 141 | 142 | public extension ActionListener { 143 | func connect( 144 | to store: Store, 145 | by connector: ConnectorType, 146 | transform: @escaping (State) -> ConnectorType.State 147 | ) where ConnectorType.Props == Props { 148 | store.onAction(on: queue) { [weak self] (state, action) in 149 | self?.updateProps(state: state, action: action, connector: connector, transform: transform) 150 | }.dispose(on: disposer) 151 | } 152 | } 153 | 154 | extension ActionListener { 155 | func updateProps( 156 | state: State, 157 | action: Action, 158 | connector: ConnectorType, 159 | transform: @escaping (State) -> ConnectorType.State 160 | ) where ConnectorType.Props == Props { 161 | let props = connector.stateAndActionToProps(state: transform(state), action: action) 162 | update(props: props) 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /UDF/ActionListener/Connector/ActionListenerConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// A protocol for mapping `State` to `Props` for a ``Component``. 19 | /// You can use instances of ``Mapper`` inside a ``Connector`` to decompose process of mapping. 20 | public protocol ActionListenerConnector { 21 | associatedtype State 22 | associatedtype Props 23 | 24 | func stateAndActionToProps(state: State, action: Action) -> Props 25 | } 26 | -------------------------------------------------------------------------------- /UDF/ActionListener/Connector/ClosureActionListenerConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | class ClosureActionListenerConnector: ActionListenerConnector { 19 | 20 | let closure: (State, Action) -> Props 21 | 22 | init(closure: @escaping (State, Action) -> Props) { 23 | self.closure = closure 24 | } 25 | 26 | func stateAndActionToProps(state: State, action: Action) -> Props { 27 | closure(state, action) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /UDF/ActionListener/ServiceActionListener.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// A protocol for service action listeners. Executes on `.global()` queue. 19 | /// If you need to use custom queue then override `queue` property. 20 | /// Use it only if you really need specific ``Action``. 21 | /// Good candidates for ``ServiceActionListener`` are App's Analytics. 22 | /// Otherwise use ``ServiceComponent`` instead. 23 | public protocol ServiceActionListener: ActionListener {} 24 | 25 | public extension ServiceActionListener { 26 | var queue: DispatchQueue { .global() } 27 | } 28 | -------------------------------------------------------------------------------- /UDF/ActionListener/ViewActionListener.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// A protocol for view action listener. Executes on `.main` queue. 19 | /// Overriding of `queue` property is not welcome. 20 | /// Use it only if you really need specific ``Action``. 21 | /// Good candidates for ``ViewActionListener`` are Global Toasts or Window Alerts. 22 | /// Otherwise use ``ViewComponent`` instead. 23 | public protocol ViewActionListener: ActionListener {} 24 | 25 | public extension ViewActionListener { 26 | var queue: DispatchQueue { .main } 27 | } 28 | -------------------------------------------------------------------------------- /UDF/Component/Component+Connector.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Anton Goncharov on 01.02.2023. 6 | // 7 | 8 | public extension Component { 9 | /// Connects a component to a store when Component`'s `Props` is equal to `Store`'s `State`. 10 | /// 11 | /// - Parameters: 12 | /// - store: A `Store` to connect to. 13 | func connect(to store: Store) where State == Props { 14 | connect(to: store) { state, _ in state } 15 | } 16 | 17 | /// Connects a component to a store using a connector with whole `Store`'s `State`. 18 | /// 19 | /// - Parameters: 20 | /// - store: A `Store` to connect to. 21 | /// - by: A `Connector` that transforms State to Props. 22 | func connect( 23 | to store: Store, 24 | by connector: ConnectorType 25 | ) where ConnectorType.State == State, ConnectorType.Props == Props { 26 | connect(to: store, by: connector) { $0 } 27 | } 28 | 29 | /// Connects a component to a store using a connector and a keypath. 30 | /// 31 | /// - Parameters: 32 | /// - store: A `Store` to connect to. 33 | /// - by: A `Connector` that transforms State to Props. 34 | /// - keypath: A keypath for a `State` of the `Component`. 35 | func connect( 36 | to store: Store, 37 | by connector: ConnectorType, 38 | state keypath: KeyPath 39 | ) where ConnectorType.Props == Props { 40 | connect(to: store, by: connector) { $0[keyPath: keypath] } 41 | } 42 | 43 | /// Connects a component to a store. 44 | /// 45 | /// - Parameters: 46 | /// - store: A `Store` to connect to. 47 | /// - by: A `Connector` that transforms State to Props. 48 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `Component`. 49 | func connect( 50 | to store: Store, 51 | by connector: ConnectorType, 52 | transform: @escaping (State) -> ConnectorType.State 53 | ) where ConnectorType.Props == Props { 54 | connect(to: store, removeDuplicates: { _, _ in false }, by: connector, transform: transform) 55 | } 56 | } 57 | 58 | public extension Component { 59 | /// Connects a component to a store using a connector with whole `Store`'s `State`. 60 | /// 61 | /// - Parameters: 62 | /// - store: A `Store` to connect to. 63 | /// - removeDuplicates: if true than ignore equal States 64 | /// - by: A `Connector` that transforms State to Props. 65 | func connect( 66 | to store: Store, 67 | removeDuplicates: Bool, 68 | by connector: ConnectorType 69 | ) where ConnectorType.State == State, ConnectorType.Props == Props, State: Equatable { 70 | connect(to: store, removeDuplicates: removeDuplicates, by: connector) { $0 } 71 | } 72 | 73 | /// Connects a component to a store using a connector and a keypath. 74 | /// 75 | /// - Parameters: 76 | /// - store: A `Store` to connect to. 77 | /// - removeDuplicates: if true than ignore equal States 78 | /// - by: A `Connector` that transforms State to Props. 79 | /// - keypath: A keypath for a `State` of the `Component`. 80 | func connect( 81 | to store: Store, 82 | removeDuplicates: Bool, 83 | by connector: ConnectorType, 84 | state keypath: KeyPath 85 | ) where ConnectorType.Props == Props, ConnectorType.State: Equatable { 86 | connect(to: store, removeDuplicates: removeDuplicates, by: connector) { $0[keyPath: keypath] } 87 | } 88 | 89 | /// Connects a component to a store. 90 | /// 91 | /// - Parameters: 92 | /// - store: A `Store` to connect to. 93 | /// - removeDuplicates: if true than ignore equal States 94 | /// - by: A `Connector` that transforms State to Props. 95 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `Component`. 96 | func connect( 97 | to store: Store, 98 | removeDuplicates: Bool, 99 | by connector: ConnectorType, 100 | transform: @escaping (State) -> ConnectorType.State 101 | ) where ConnectorType.Props == Props, ConnectorType.State: Equatable { 102 | connect( 103 | to: store, 104 | removeDuplicates: removeDuplicates ? { $0 == $1 } : { _, _ in false }, 105 | by: connector, 106 | transform: transform 107 | ) 108 | } 109 | } 110 | 111 | extension Component { 112 | func connect( 113 | to store: Store, 114 | removeDuplicates: @escaping (ConnectorType.State, ConnectorType.State) -> Bool, 115 | by connector: ConnectorType, 116 | transform: @escaping (State) -> ConnectorType.State 117 | ) where ConnectorType.Props == Props { 118 | store.publisher 119 | .receive(on: queue) 120 | .map(transform) 121 | .removeDuplicates(by: removeDuplicates) 122 | .sink { [weak self] state in 123 | guard let self = self else { return } 124 | self.updateProps(state: state, connector: connector, dispatcher: store) 125 | }.store(in: &disposer.subscriptions) 126 | } 127 | 128 | func updateProps( 129 | state: ConnectorType.State, 130 | connector: ConnectorType, 131 | dispatcher: ActionDispatcher 132 | ) where ConnectorType.Props == Props { 133 | let newProps = connector.stateToProps(state: state, dispatcher: dispatcher) 134 | guard props != newProps else { return } 135 | props = newProps 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /UDF/Component/Component+SelfConnector.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Anton Goncharov on 01.02.2023. 6 | // 7 | 8 | public extension Component where Self: Connector { 9 | /// Connects a component to a store when the `Component` is a `Connector` and with whole `Store`'s `State`. 10 | /// 11 | /// - Parameters: 12 | /// - store: A `Store` to connect to. 13 | func connect( 14 | to store: Store 15 | ) where Self.State == State { 16 | connect(to: store) { $0 } 17 | } 18 | 19 | /// Connects a component to a store when the `Component` is a `Connector`and with a keypath. 20 | /// 21 | /// - Parameters: 22 | /// - store: A `Store` to connect to. 23 | /// - keypath: A keypath for a `State` of the `Component`. 24 | func connect( 25 | to store: Store, 26 | state keypath: KeyPath 27 | ) { 28 | connect(to: store) { $0[keyPath: keypath] } 29 | } 30 | 31 | /// Connects a component to a store when the `Component` is a `Connector`. 32 | /// 33 | /// - Parameters: 34 | /// - store: A `Store` to connect to. 35 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `Connector`. 36 | func connect( 37 | to store: Store, 38 | transform: @escaping (State) -> Self.State 39 | ) { 40 | connect(to: store, removeDuplicates: { _, _ in false }, transform: transform) 41 | } 42 | } 43 | 44 | public extension Component where Self: Connector, Self.State: Equatable { 45 | /// Connects a component to a store when the `Component` is a `Connector` and with whole `Store`'s `State`. 46 | /// 47 | /// - Parameters: 48 | /// - store: A `Store` to connect to. 49 | /// - removeDuplicates: if true than ignore equal States 50 | func connect( 51 | to store: Store, 52 | removeDuplicates: Bool 53 | ) where Self.State == State { 54 | connect(to: store, removeDuplicates: removeDuplicates) { $0 } 55 | } 56 | 57 | /// Connects a component to a store when the `Component` is a `Connector`and with a keypath. 58 | /// 59 | /// - Parameters: 60 | /// - store: A `Store` to connect to. 61 | /// - removeDuplicates: if true than ignore equal States 62 | /// - keypath: A keypath for a `State` of the `Component`. 63 | func connect( 64 | to store: Store, 65 | removeDuplicates: Bool, 66 | state keypath: KeyPath 67 | ) { 68 | connect( 69 | to: store, 70 | removeDuplicates: removeDuplicates 71 | ) { $0[keyPath: keypath] } 72 | } 73 | 74 | /// Connects a component to a store when the `Component` is a `Connector`. 75 | /// 76 | /// - Parameters: 77 | /// - store: A `Store` to connect to. 78 | /// - removeDuplicates: if true than ignore equal States 79 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `Connector`. 80 | func connect( 81 | to store: Store, 82 | removeDuplicates: Bool, 83 | transform: @escaping (State) -> Self.State 84 | ) { 85 | connect( 86 | to: store, 87 | removeDuplicates: removeDuplicates ? { $0 == $1 } : { _, _ in false }, 88 | transform: transform 89 | ) 90 | } 91 | } 92 | 93 | extension Component where Self: Connector { 94 | func connect( 95 | to store: Store, 96 | removeDuplicates: @escaping (Self.State, Self.State) -> Bool, 97 | transform: @escaping (State) -> Self.State 98 | ) { 99 | store.publisher 100 | .receive(on: queue) 101 | .map(transform) 102 | .removeDuplicates(by: removeDuplicates) 103 | .sink { [weak self] state in 104 | guard let self = self else { return } 105 | self.updateProps(state: state, connector: self, dispatcher: store) 106 | }.store(in: &disposer.subscriptions) 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /UDF/Component/Component+stateToProps.swift: -------------------------------------------------------------------------------- 1 | // 2 | // File.swift 3 | // 4 | // 5 | // Created by Anton Goncharov on 01.02.2023. 6 | // 7 | 8 | public extension Component { 9 | /// Connects a component to a store with stateToProps closure and whole `Store`'s `State`. 10 | /// 11 | /// - Parameters: 12 | /// - store: A `Store` to connect to. 13 | /// - stateToProps: A closure that transforms the `Store`'s `State` into a `Props` of the `Component`. 14 | func connect( 15 | to store: Store, 16 | stateToProps: @escaping (State, ActionDispatcher) -> Props 17 | ) { 18 | connect(to: store, stateToProps: stateToProps) { $0 } 19 | } 20 | 21 | /// Connects a component to a store with stateToProps closure and keypath. 22 | /// 23 | /// - Parameters: 24 | /// - store: A `Store` to connect to. 25 | /// - stateToProps: A closure that transforms the `Component`'s `State` into a `Props` of the `Component`. 26 | /// - keypath: A keypath for a `State` of the `Component`. 27 | func connect( 28 | to store: Store, 29 | stateToProps: @escaping (ConnectorState, ActionDispatcher) -> Props, 30 | state keypath: KeyPath 31 | ) { 32 | connect(to: store, stateToProps: stateToProps) { $0[keyPath: keypath] } 33 | } 34 | 35 | /// Connects a component to a store with stateToProps closure. 36 | /// 37 | /// - Parameters: 38 | /// - store: A ``Store`` to connect to. 39 | /// - stateToProps: A closure that transforms the `Component`'s `State` into a `Props` of the `Component`. 40 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `Component`. 41 | func connect( 42 | to store: Store, 43 | stateToProps: @escaping (ConnectorState, ActionDispatcher) -> Props, 44 | transform: @escaping (State) -> ConnectorState 45 | ) { 46 | connect( 47 | to: store, 48 | removeDuplicates: { _, _ in false }, 49 | by: ClosureConnector(closure: stateToProps), 50 | transform: transform 51 | ) 52 | } 53 | } 54 | 55 | public extension Component { 56 | /// Connects a component to a store with stateToProps closure and whole `Store`'s `State`. 57 | /// 58 | /// - Parameters: 59 | /// - store: A `Store` to connect to. 60 | /// - removeDuplicates: if true than ignore equal States 61 | /// - stateToProps: A closure that transforms the `Store`'s `State` into a `Props` of the `Component`. 62 | func connect( 63 | to store: Store, 64 | removeDuplicates: Bool, 65 | stateToProps: @escaping (State, ActionDispatcher) -> Props 66 | ) { 67 | connect(to: store, removeDuplicates: removeDuplicates, stateToProps: stateToProps) { $0 } 68 | } 69 | 70 | /// Connects a component to a store with stateToProps closure and keypath. 71 | /// 72 | /// - Parameters: 73 | /// - store: A `Store` to connect to. 74 | /// - removeDuplicates: if true than ignore equal States 75 | /// - stateToProps: A closure that transforms the `Component`'s `State` into a `Props` of the `Component`. 76 | /// - keypath: A keypath for a `State` of the `Component`. 77 | func connect( 78 | to store: Store, 79 | removeDuplicates: Bool, 80 | stateToProps: @escaping (ConnectorState, ActionDispatcher) -> Props, 81 | state keypath: KeyPath 82 | ) where ConnectorState: Equatable { 83 | connect(to: store, removeDuplicates: removeDuplicates, stateToProps: stateToProps) { $0[keyPath: keypath] } 84 | } 85 | 86 | /// Connects a component to a store with stateToProps closure. 87 | /// 88 | /// - Parameters: 89 | /// - store: A ``Store`` to connect to. 90 | /// - removeDuplicates: if true than ignore equal States 91 | /// - stateToProps: A closure that transforms the `Component`'s `State` into a `Props` of the `Component`. 92 | /// - transform: A closure that transforms the `Store`'s `State` to a `State` of the `Component`. 93 | func connect( 94 | to store: Store, 95 | removeDuplicates: Bool, 96 | stateToProps: @escaping (ConnectorState, ActionDispatcher) -> Props, 97 | transform: @escaping (State) -> ConnectorState 98 | ) where ConnectorState: Equatable { 99 | connect( 100 | to: store, 101 | removeDuplicates: removeDuplicates, 102 | by: ClosureConnector(closure: stateToProps), 103 | transform: transform 104 | ) 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /UDF/Component/Component.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | import Foundation 16 | import Combine 17 | 18 | /// Parent protocol for components. Use ``ViewComponent`` or ``ServiceComponent`` for your component. 19 | public protocol Component: Propsable { 20 | var queue: DispatchQueue { get } 21 | var disposer: Disposer { get } 22 | 23 | func connect( 24 | to store: Store, 25 | by connector: ConnectorType, 26 | transform: @escaping (State) -> ConnectorType.State 27 | ) where ConnectorType.Props == Props 28 | 29 | func connect( 30 | to store: Store, 31 | removeDuplicates: Bool, 32 | by connector: ConnectorType, 33 | transform: @escaping (State) -> ConnectorType.State 34 | ) where ConnectorType.Props == Props, ConnectorType.State: Equatable 35 | } 36 | -------------------------------------------------------------------------------- /UDF/Component/Propsable.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Propsable.swift 3 | // UDF 4 | // 5 | // Created by Anton Goncharov on 19.11.2021. 6 | // 7 | 8 | import Foundation 9 | 10 | /// A protocol for views that don't subscribe to store directly. 11 | /// For example ``UITableViewCell`` or ``UICollectionViewCell``. 12 | public protocol Propsable: AnyObject { 13 | associatedtype Props: Equatable 14 | var props: Props { get set } 15 | } 16 | -------------------------------------------------------------------------------- /UDF/Component/ServiceComponent.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// A protocol for service components. Executes on global queue with 19 | /// `.userInitiated` quality-of-service class. 20 | /// If you need to use custom queue then override `queue` property. 21 | public protocol ServiceComponent: Component {} 22 | 23 | public extension ServiceComponent { 24 | var queue: DispatchQueue { .global(qos: .userInitiated) } 25 | } 26 | -------------------------------------------------------------------------------- /UDF/Component/ViewComponent.swift: -------------------------------------------------------------------------------- 1 | /// Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// A protocol for view components. Executes on `.main` queue. 19 | /// Overriding of `queue` property is not welcome. 20 | public protocol ViewComponent: Component {} 21 | 22 | public extension ViewComponent { 23 | var queue: DispatchQueue { .main } 24 | } 25 | -------------------------------------------------------------------------------- /UDF/Connector/ClosureConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | class ClosureConnector: Connector { 19 | 20 | let closure: (State, ActionDispatcher) -> Props 21 | 22 | init(closure: @escaping (State, ActionDispatcher) -> Props) { 23 | self.closure = closure 24 | } 25 | 26 | func stateToProps(state: State, dispatcher: ActionDispatcher) -> Props { 27 | closure(state, dispatcher) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /UDF/Connector/Connector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | /// A protocol that allows you to connect a ``Component`` to a ``Store`` 17 | /// You can use it as an alternative to stateToProps closure. 18 | public protocol Connector: Mapper { } 19 | -------------------------------------------------------------------------------- /UDF/Connector/Mapper.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// A protocol for mapping `State` to `Props` for a ``Component``. 19 | /// You can use instances of ``Mapper`` inside a ``Connector`` to decompose process of mapping. 20 | public protocol Mapper { 21 | associatedtype State 22 | associatedtype Props: Equatable 23 | 24 | func stateToProps(state: State, dispatcher: ActionDispatcher) -> Props 25 | } 26 | -------------------------------------------------------------------------------- /UDF/Disposer.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Combine 17 | 18 | /// Disposable are just simple wrappers around Combine.AnyCancellable. 19 | public typealias Disposable = AnyCancellable 20 | 21 | public extension Disposable { 22 | func dispose(on disposer: Disposer) { 23 | store(in: &disposer.subscriptions) 24 | } 25 | } 26 | 27 | public final class Disposer { 28 | public var subscriptions = Set() 29 | public init() { } 30 | } 31 | -------------------------------------------------------------------------------- /UDF/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | 22 | 23 | -------------------------------------------------------------------------------- /UDF/Reducer.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | /// `Reducer` is a function that updates a state. 17 | /// The only side effect allowed inside a `Reducer` is updating inout `State`. 18 | /// - Parameters: 19 | /// - State: Generic inout parameter of a state. 20 | /// - Action: ``Action`` that occurred in some component. Cast to a specific ``Action`` type inside a ``Reducer``. 21 | public typealias Reducer = (inout State, Action) -> Void 22 | 23 | /// `SideEffectReducer` is a `Reducer` that returns a ``SideEffect``. 24 | /// - Parameters: 25 | /// - State: Generic inout parameter of a state. 26 | /// - Action: ``Action`` that occurred in some component. Cast to a specific ``Action`` type inside a ``Reducer``. 27 | /// - Returns: A ``SideEffect`` that will be executed after `Reducer` call. Return `nil` if ``SideEffect`` is not needed. 28 | public typealias SideEffectReducer = (inout State, Action) -> SideEffect 29 | -------------------------------------------------------------------------------- /UDF/SideEffect/CombineSideEffect.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | public struct CombineSideEffect: SideEffectProtocol { 17 | 18 | let effects: [SideEffectProtocol] 19 | 20 | public init(effects: [SideEffect]) { 21 | self.effects = effects.reduce(into:[]) { result, effect in 22 | switch effect { 23 | case let combineSideEffect as CombineSideEffect: 24 | result.append(contentsOf: combineSideEffect.effects) 25 | case nil: 26 | return 27 | case let .some(effect): 28 | result.append(effect) 29 | } 30 | } 31 | } 32 | 33 | public func execute(with dispatcher: ActionDispatcher) { 34 | effects.forEach { $0.execute(with: dispatcher) } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /UDF/SideEffect/SideEffectProtocol.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | /// A protocol that defines a `SideEffect`. `SideEffect` can be returned from ``SideEffectReducer`` 17 | /// and executed inside a ``Store`` 18 | /// 19 | ///`SideEffect` is a good place to make network request, save or load date from local memory, 20 | /// open web url, ask for permission etc. 21 | public protocol SideEffectProtocol { 22 | func execute(with: ActionDispatcher) 23 | } 24 | 25 | public typealias SideEffect = SideEffectProtocol? 26 | -------------------------------------------------------------------------------- /UDF/SideEffect/SideEffectTypealiases.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | ///typealias for ``SideEffect`` that should return a ``Result`` inside an ``Action``. 17 | public typealias ResultAction = (Result) -> Action 18 | -------------------------------------------------------------------------------- /UDF/SideEffect/SideEffectsBuilder.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | /// function that provide composition of multiple ``SideEffect``s. 17 | /// 18 | /// Multiple ``SideEffect``s can be combined as follows: 19 | /// ```swift 20 | /// func reducer(state: inout Int, action: Action) -> SideEffect { 21 | /// combine { 22 | /// firstReducer(&state, action) 23 | /// secondReducer(&state, action) 24 | /// thirdReducer(&state, action) 25 | /// } 26 | /// } 27 | /// ``` 28 | public func combine(@SideEffectsBuilder _ content: () -> SideEffectProtocol) -> SideEffectProtocol { 29 | content() 30 | } 31 | 32 | @resultBuilder 33 | public struct SideEffectsBuilder { 34 | public static func buildBlock(_ effects: SideEffect...) -> SideEffectProtocol { 35 | CombineSideEffect(effects: effects) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /UDF/Store.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | import Combine 18 | 19 | /// The Store is a simple `State` manager. 20 | /// An app usually has a single instance of the main store. 21 | /// Use the `scope` methods to derive proxy stores that can be passed to submodules. 22 | /// 23 | /// After action got dispatched, 24 | /// the store will get the new instance of the State by calling the reducer with the current state and an action. 25 | /// ``` 26 | /// state = reducer(state, action) 27 | /// ``` 28 | /// And then the Store will notify all the subscribers with the new State. 29 | public class Store: ActionDispatcher { 30 | public fileprivate(set) var publisher: CurrentValueSubject 31 | public fileprivate(set) var actionsSubject: PassthroughSubject<(State, Action), Never> = .init() 32 | 33 | fileprivate let disposer = Disposer() 34 | fileprivate let storeDispatchQueue: DispatchQueue 35 | 36 | private let reducer: SideEffectReducer 37 | private let effectDispatchQueue = DispatchQueue(label: "com.udf.effect-queue", attributes: .concurrent) 38 | 39 | public var state: State { publisher.value } 40 | 41 | public convenience init( 42 | state: State, 43 | reducer: @escaping Reducer, 44 | dispatchQueue: DispatchQueue = .init(label: "com.udf.store-lock-queue") 45 | ) { 46 | let sideEffectReducer: SideEffectReducer = { state, action in 47 | reducer(&state, action) 48 | return nil 49 | } 50 | 51 | self.init(state: state, reducer: sideEffectReducer, dispatchQueue: dispatchQueue) 52 | } 53 | 54 | public init( 55 | state: State, 56 | reducer: @escaping SideEffectReducer, 57 | dispatchQueue: DispatchQueue = .init(label: "com.udf.store-lock-queue") 58 | ) { 59 | self.reducer = reducer 60 | 61 | publisher = CurrentValueSubject(state) 62 | storeDispatchQueue = dispatchQueue 63 | } 64 | 65 | /// The only way to mutate the State is to dispatch an `Action`. 66 | /// After action got dispatched, 67 | /// the store will get the new instance of the State by calling the reducer with the current state and an action. 68 | /// Then the Store will notify all the subscribers with the new State. 69 | /// 70 | /// - Parameter action: Action regarding which state must be mutated. 71 | public func dispatch(_ action: Action) { 72 | storeDispatchQueue.async { 73 | self.dispatchSync(action) 74 | } 75 | } 76 | 77 | /// Sync version of the `dispatch` method. 78 | fileprivate func dispatchSync(_ action: Action) { 79 | let effect = reducer(&publisher.value, action) 80 | actionsSubject.send(((state, action))) 81 | 82 | guard let effect = effect else { return } 83 | effectDispatchQueue.async { 84 | effect.execute(with: self) 85 | } 86 | } 87 | 88 | /// Subscribe a component to observe the state **after** each change 89 | /// 90 | /// - Parameter observer: this closure will be called **when subscribe** and every time **after** state has changed. 91 | /// 92 | public func observe(on queue: DispatchQueue = .main, with observer: @escaping (State) -> Void) -> Disposable { 93 | let subscriber = publisher 94 | .receive(on: queue) 95 | // Allows a store live until a subscription exists. 96 | // See https://forums.swift.org/t/combine-best-practices-with-memory-management-using-subjects-to-publish-values/38724 97 | .map { (self, $0).1 } 98 | .sink(receiveValue: observer) 99 | return subscriber 100 | } 101 | 102 | /// Subscribes to observe Actions and the old State **after** the change when action has happened. 103 | /// Recommended using only for debugging purposes. 104 | /// ``` 105 | /// store.onAction{ action, state in 106 | /// print(action) 107 | /// } 108 | /// ``` 109 | /// - Parameter observe: this closure will be executed whenever the action happened **after** the state change 110 | /// 111 | /// - Returns: A `Disposable`, to stop observation call .dispose() on it, or add it to a `Disposer` 112 | public func onAction( 113 | on queue: DispatchQueue = .main, 114 | with observer: @escaping (State, Action) -> Void) -> Disposable { 115 | let subscriber = actionsSubject 116 | .receive(on: queue) 117 | // Allows a store live until a subscription exists. 118 | // See https://forums.swift.org/t/combine-best-practices-with-memory-management-using-subjects-to-publish-values/38724 119 | .map { (self, $0).1 } 120 | .sink {value in 121 | observer(value.0, value.1) 122 | } 123 | 124 | return subscriber 125 | } 126 | 127 | // MARK: - Scope 128 | 129 | /// Scopes the store to a local state. 130 | /// 131 | /// - Parameter keypath: A keypath for a `LocalState`. 132 | /// - Returns: A `Store` with scoped `State`. 133 | public func scope(_ keyPath: KeyPath) -> Store { 134 | scope { $0[keyPath: keyPath] } 135 | } 136 | 137 | /// Scopes the store to a local state. 138 | /// 139 | /// - Parameter keypath: A keypath for a `Equatable` `LocalState`. 140 | /// if `LocalState` is the same after update, `Store` subscrbers will not be notified. 141 | /// - Returns: A `Store` with scoped `State`. 142 | public func scope(_ keyPath: KeyPath) -> Store { 143 | scope { $0[keyPath: keyPath] } 144 | } 145 | 146 | /// Scopes the store to a local state. 147 | /// 148 | /// - Parameter transform: A function that transforms the `State` into a `LocalState`. 149 | /// - Returns: A `Store` with scoped `State`. 150 | public func scope(transform: @escaping (State) -> LocalState) -> Store { 151 | scope(transform: transform, shouldRemoveDublicates: { _, _ in false }) 152 | } 153 | 154 | /// Scopes the store to a local state. 155 | /// 156 | /// - Parameter transform: A function that transforms the `State` into a `LocalState`. 157 | /// if `LocalState` is the same after update, `Store` subscrbers will not be notified. 158 | /// - Returns: A `Store` with scoped `State`. 159 | public func scope(transform: @escaping (State) -> LocalState) -> Store { 160 | scope(transform: transform, shouldRemoveDublicates: ==) 161 | } 162 | 163 | fileprivate func scope( 164 | transform: @escaping (State) -> LocalState, 165 | shouldRemoveDublicates: @escaping (LocalState, LocalState) -> Bool 166 | ) -> Store { 167 | return ProxyStore( 168 | store: self, 169 | transform: transform, 170 | shouldRemoveDublicates: shouldRemoveDublicates, 171 | dispatchQueue: storeDispatchQueue 172 | ) 173 | } 174 | } 175 | 176 | // MARK: - ProxyStore 177 | 178 | /// ProxyStore is a specific type of `Store`. 179 | /// It doesn't have its own reducer. ProxyStore just proxy actions to parent store and get `State` update from it. 180 | class ProxyStore: Store { 181 | private let store: Store 182 | private let transform: (State) -> LocalState 183 | private let shouldRemoveDublicates: (LocalState, LocalState) -> Bool 184 | 185 | init( 186 | store: Store, 187 | transform: @escaping (State) -> LocalState, 188 | shouldRemoveDublicates: @escaping (LocalState, LocalState) -> Bool, 189 | dispatchQueue: DispatchQueue 190 | ) { 191 | self.store = store 192 | self.transform = transform 193 | self.shouldRemoveDublicates = shouldRemoveDublicates 194 | super.init(state: transform(store.state), reducer: { _, _ in nil }, dispatchQueue: dispatchQueue) 195 | 196 | store.actionsSubject 197 | .map { (state, action) in 198 | (transform(state), action) 199 | } 200 | .sink { [weak self] (state, action) in 201 | self?.actionsSubject.send((state, action)) 202 | } 203 | .store(in: &disposer.subscriptions) 204 | 205 | store.publisher 206 | .map(transform) 207 | .removeDuplicates(by: shouldRemoveDublicates) 208 | .sink { [weak self] state in 209 | self?.publisher.send(state) 210 | } 211 | .store(in: &disposer.subscriptions) 212 | } 213 | 214 | public override func dispatch(_ action: Action) { 215 | storeDispatchQueue.async { 216 | self.store.dispatchSync(action) 217 | } 218 | } 219 | 220 | override func scope( 221 | transform: @escaping (LocalState) -> ScopeState, 222 | shouldRemoveDublicates: @escaping (ScopeState, ScopeState) -> Bool 223 | ) -> Store { 224 | return ProxyStore( 225 | store: store, 226 | transform: pipe(self.transform, transform), 227 | shouldRemoveDublicates: shouldRemoveDublicates, 228 | dispatchQueue: storeDispatchQueue 229 | ) 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /UDF/Subscription.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | /// Subscription class incapsulates observer closures for `Store`. 19 | class Subscription { 20 | let action: (T) -> Void 21 | 22 | init(action: @escaping (T) -> Void) { 23 | self.action = action 24 | } 25 | 26 | func notify(with value: T) { 27 | action(value) 28 | } 29 | } 30 | 31 | /// Allows Subscription to be compared and stored in sets and dicts. 32 | extension Subscription: Hashable, Equatable { 33 | static func == (left: Subscription, right: Subscription) -> Bool { 34 | return ObjectIdentifier(left) == ObjectIdentifier(right) 35 | } 36 | 37 | func hash(into hasher: inout Hasher) { 38 | hasher.combine(ObjectIdentifier(self).hashValue) 39 | } 40 | } 41 | 42 | // MARK: - Queueing 43 | 44 | extension Subscription { 45 | // Moves subscription notification to other `DispatchQueue`. 46 | /// 47 | /// - Parameter queue: Desired `DispatchQueue`. 48 | /// 49 | /// - Returns: A `Subscription` that notifies on passed DispatchQueue`. 50 | func async(on queue: DispatchQueue) -> Subscription { 51 | return Subscription { value in 52 | queue.async { 53 | self.notify(with: value) 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /UDF/UDF.h: -------------------------------------------------------------------------------- 1 | // 2 | // UDF.h 3 | // UDF 4 | // 5 | // Created by Anton Goncharov on 28.09.2020. 6 | // Copyright © 2020 inDriver. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for UDF. 12 | FOUNDATION_EXPORT double UDFVersionNumber; 13 | 14 | //! Project version string for UDF. 15 | FOUNDATION_EXPORT const unsigned char UDFVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | -------------------------------------------------------------------------------- /UDF/Utils.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | // swiftlint:disable identifier_name 17 | /// Forward composition of functions. 18 | /// 19 | /// - Parameters: 20 | /// - f: A function that takes a value in `A` and returns a value in `B`. 21 | /// - a: An argument in `A`. 22 | /// - g: A function that takes a value in `B` and returns a value in `C`. 23 | /// - b: An argument in `B`. 24 | /// - Returns: A new function that takes a value in `A` and returns a value in `C`. 25 | /// - Note: This function is commonly seen in operator form as `>>>`. 26 | func pipe(_ f: @escaping (_ a: A) -> B, _ g: @escaping (_ b: B) -> C) -> (A) -> C { 27 | return { (a: A) -> C in 28 | g(f(a)) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /UDF/ViewStore.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Combine 17 | import UIKit 18 | /** 19 | If you need to use UDF in SwiftUI 20 | 1. Create SwiftUI View with: 21 | 22 | struct CounterView: View { 23 | 24 | @ObservedObject 25 | var store: ViewStore 26 | 27 | var body: some View { 28 | HStack { 29 | Button("-") { 30 | store.dispatch(.minusDidTap) 31 | } 32 | Text("\(store.state.count)") 33 | Button("+") { 34 | store.dispatch(.plusDidTap) 35 | } 36 | } 37 | .frame(alignment: .center) 38 | } 39 | } 40 | 41 | 2. Create ViewStore from Store and pass it to the View: 42 | 43 | let view = CounterView(store: store.viewStore(\.counterState)) 44 | return UIHostingController(rootView: view) 45 | */ 46 | @_spi(Private) public class ViewStore: ObservableObject, Dispatcher { 47 | 48 | @Published 49 | var store: Store 50 | var cancelables = Set() 51 | 52 | public var state: State { 53 | store.state 54 | } 55 | 56 | public init(store: Store) { 57 | self.store = store 58 | 59 | store.publisher.sink { [weak self] _ in 60 | DispatchQueue.main.async { 61 | self?.objectWillChange.send() 62 | } 63 | }.store(in: &cancelables) 64 | } 65 | 66 | public func dispatch(_ action: ActionType) { 67 | store.dispatch(action) 68 | } 69 | } 70 | 71 | @_spi(Private) public extension Store { 72 | func viewStore( 73 | _ keypath: KeyPath 74 | ) -> ViewStore { 75 | .init(store: scope(keypath)) 76 | } 77 | } 78 | 79 | @_spi(Private) public protocol Dispatcher { 80 | 81 | associatedtype ActionType: Action 82 | 83 | func dispatch(_ action: ActionType) 84 | } 85 | -------------------------------------------------------------------------------- /UDFTests/ActionListenerTests.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import XCTest 17 | @testable import UDF 18 | 19 | class ActionListenerTests: XCTestCase { 20 | 21 | func reducer(state: inout Int, action: Action) { 22 | if case let FakeComponentConnector.Actions.valueDidChange(newValue) = action { 23 | state = newValue 24 | } 25 | } 26 | 27 | func reducer(state: inout TestState, action: Action) { 28 | reducer(state: &state.intValue, action: action) 29 | } 30 | 31 | override func setUp() { 32 | super.setUp() 33 | } 34 | 35 | override func tearDown() { 36 | super.tearDown() 37 | } 38 | 39 | func stateAndActionToProps(value: Int, action:Action) -> FakeActionListener.Props { (value, action) } 40 | 41 | func testConnect() { 42 | // given 43 | let store = Store(state: 1, reducer: reducer) 44 | let exp = expectation(description: "props is updated") 45 | let actionListener = FakeActionListener(propsDidSet: { props in 46 | guard props.count == 1 else { return } 47 | exp.fulfill() 48 | }) 49 | 50 | // when 51 | actionListener.connect(to: store, stateAndActionToProps: stateAndActionToProps) { $0 } 52 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 53 | 54 | // then 55 | waitForExpectations(timeout: 0.1, handler: nil) 56 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 57 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 58 | } 59 | 60 | func testConnectWithWholeState() { 61 | // given 62 | let store = Store(state: 1, reducer: reducer) 63 | let exp = expectation(description: "props is updated") 64 | let actionListener = FakeActionListener(propsDidSet: { props in 65 | guard props.count == 1 else { return } 66 | exp.fulfill() 67 | }) 68 | 69 | // when 70 | actionListener.connect(to: store, stateAndActionToProps: stateAndActionToProps) 71 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 72 | 73 | // then 74 | waitForExpectations(timeout: 0.1, handler: nil) 75 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 76 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 77 | } 78 | 79 | func testConnectWithKeypath() { 80 | // given 81 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 82 | let exp = expectation(description: "props is updated") 83 | let actionListener = FakeActionListener(propsDidSet: { props in 84 | guard props.count == 1 else { return } 85 | exp.fulfill() 86 | }) 87 | 88 | // when 89 | actionListener.connect(to: store, stateAndActionToProps: stateAndActionToProps, state: \.intValue) 90 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 91 | 92 | // then 93 | waitForExpectations(timeout: 0.1, handler: nil) 94 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 95 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 96 | } 97 | 98 | func testConnectWithConnectorAndWholeState() { 99 | // given 100 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 101 | let exp = expectation(description: "props is updated") 102 | let actionListener = FakeActionListener(propsDidSet: { props in 103 | guard props.count == 1 else { return } 104 | exp.fulfill() 105 | }) 106 | let connector = FakeTestStateActionListenerConnector() 107 | 108 | // when 109 | actionListener.connect(to: store, by: connector) 110 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 111 | 112 | // then 113 | waitForExpectations(timeout: 0.1, handler: nil) 114 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 115 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 116 | } 117 | 118 | func testConnectWithConnectorAndKeypath() { 119 | // given 120 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 121 | let exp = expectation(description: "props is updated") 122 | let actionListener = FakeActionListener(propsDidSet: { props in 123 | guard props.count == 1 else { return } 124 | exp.fulfill() 125 | }) 126 | let connector = FakeActionListenerConnector() 127 | 128 | // when 129 | actionListener.connect(to: store, by: connector, state: \.intValue) 130 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 131 | 132 | // then 133 | waitForExpectations(timeout: 0.1, handler: nil) 134 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 135 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 136 | } 137 | 138 | func testConnectWithConnector() { 139 | // given 140 | let store = Store(state: 1, reducer: reducer) 141 | let exp = expectation(description: "props is updated") 142 | let actionListener = FakeActionListener(propsDidSet: { props in 143 | guard props.count == 1 else { return } 144 | exp.fulfill() 145 | }) 146 | let connector = FakeActionListenerConnector() 147 | 148 | // when 149 | actionListener.connect(to: store, by: connector) { $0 } 150 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 151 | 152 | // then 153 | waitForExpectations(timeout: 0.1, handler: nil) 154 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 155 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 156 | } 157 | 158 | func testConnectWhenComponentIsConnectorWithWholeState() { 159 | // given 160 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 161 | let exp = expectation(description: "props is updated") 162 | let actionListener = FakeTestStateActionListenerAndConnector(propsDidSet: { props in 163 | guard props.count == 1 else { return } 164 | exp.fulfill() 165 | }) 166 | 167 | // when 168 | actionListener.connect(to: store) 169 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 170 | 171 | // then 172 | waitForExpectations(timeout: 0.1, handler: nil) 173 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 174 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 175 | } 176 | 177 | func testConnectWhenComponentIsConnectorWithKeypath() { 178 | // given 179 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 180 | let exp = expectation(description: "props is updated") 181 | let actionListener = FakeActionListenerAndConnector(propsDidSet: { props in 182 | guard props.count == 1 else { return } 183 | exp.fulfill() 184 | }) 185 | 186 | // when 187 | actionListener.connect(to: store, state: \.intValue) 188 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 189 | 190 | // then 191 | waitForExpectations(timeout: 0.1, handler: nil) 192 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 193 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 194 | } 195 | 196 | func testConnectWhenComponentIsConnector() { 197 | // given 198 | let store = Store(state: 1, reducer: reducer) 199 | let exp = expectation(description: "props is updated") 200 | let actionListener = FakeActionListenerAndConnector(propsDidSet: { props in 201 | guard props.count == 1 else { return } 202 | exp.fulfill() 203 | }) 204 | 205 | // when 206 | actionListener.connect(to: store) { $0 } 207 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 208 | 209 | // then 210 | waitForExpectations(timeout: 0.1, handler: nil) 211 | XCTAssertEqual(actionListener.propsHistory.first?.0, 2) 212 | XCTAssertEqual(actionListener.propsHistory.first?.1 as? FakeComponentConnector.Actions, .valueDidChange(2)) 213 | } 214 | 215 | func testComponentAndConnector_DeinitCorrectly() { 216 | // given 217 | let store = Store(state: 1, reducer: reducer) 218 | let exp = expectation(description: "component is deinited") 219 | exp.expectedFulfillmentCount = 2 220 | var actionListener: FakeActionListener? = .init { 221 | exp.fulfill() 222 | } 223 | 224 | var connector: FakeActionListenerConnector? = .init { 225 | exp.fulfill() 226 | } 227 | 228 | // when 229 | actionListener?.connect(to: store, by: connector!) 230 | actionListener = nil 231 | connector = nil 232 | 233 | // then 234 | wait(for: [exp], timeout: 0.1) 235 | } 236 | 237 | func testWhenComponentIsConnector_DeinitCorrectly() { 238 | // given 239 | let store = Store(state: 1, reducer: reducer) 240 | let exp = expectation(description: "component is deinited") 241 | var actionListener: FakeActionListenerAndConnector? = .init(onDeinit: { 242 | exp.fulfill() 243 | }) 244 | 245 | // when 246 | actionListener?.connect(to: store) 247 | actionListener = nil 248 | 249 | // then 250 | wait(for: [exp], timeout: 0.1) 251 | } 252 | } 253 | -------------------------------------------------------------------------------- /UDFTests/CombineSideEffectTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CombineSideEffectTests.swift 3 | // 4 | // 5 | // Created by Anton Goncharov on 12.04.2023. 6 | // 7 | 8 | import XCTest 9 | @testable import UDF 10 | 11 | class CombineSideEffectTests: XCTestCase { 12 | 13 | func testCombineSideEffectCombinesTwoEffects() { 14 | let effect1 = FakeSideEffect() 15 | let effect2 = FakeSideEffect() 16 | let sut = CombineSideEffect(effects: [effect1, effect2]) 17 | 18 | XCTAssertEqual(sut.effects.count, 2) 19 | XCTAssertTrue(sut.effects.first is FakeSideEffect) 20 | XCTAssertTrue(sut.effects.first as? FakeSideEffect === effect1) 21 | XCTAssertTrue(sut.effects.last is FakeSideEffect) 22 | XCTAssertTrue(sut.effects.last as? FakeSideEffect === effect2) 23 | 24 | } 25 | 26 | func testCombineSideEffectEliminateNils() { 27 | let sut = CombineSideEffect(effects: [nil, nil]) 28 | 29 | XCTAssertTrue(sut.effects.isEmpty) 30 | } 31 | 32 | func testCombineSideEffectFlatMapEffects() { 33 | let effect1 = FakeSideEffect() 34 | let effect2 = FakeSideEffect() 35 | let sut = CombineSideEffect( 36 | effects: [ 37 | CombineSideEffect(effects: [effect1]), 38 | CombineSideEffect(effects: [effect2])] 39 | ) 40 | 41 | XCTAssertEqual(sut.effects.count, 2) 42 | XCTAssertTrue(sut.effects.first is FakeSideEffect) 43 | XCTAssertTrue(sut.effects.first as? FakeSideEffect === effect1) 44 | XCTAssertTrue(sut.effects.last is FakeSideEffect) 45 | XCTAssertTrue(sut.effects.last as? FakeSideEffect === effect2) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /UDFTests/Component+ConnectorTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Component+ConnectorTests.swift 3 | // UDFTests 4 | // 5 | // Created by Anton Goncharov on 02.02.2023. 6 | // 7 | 8 | import XCTest 9 | @testable import UDF 10 | 11 | class ComponentConnectorTests: XCTestCase { 12 | 13 | func reducer(state: inout Int, action: Action) { 14 | if case let FakeComponentConnector.Actions.valueDidChange(newValue) = action { 15 | state = newValue 16 | } 17 | } 18 | 19 | func reducer(state: inout TestState, action: Action) { 20 | reducer(state: &state.intValue, action: action) 21 | } 22 | 23 | func testConnect_store() { 24 | // given 25 | let store = Store(state: 1, reducer: reducer) 26 | let exp = expectation(description: "props is updated") 27 | let component = FakeComponent(propsDidSet: { props in 28 | guard props.count == 2 else { return } 29 | exp.fulfill() 30 | }) 31 | 32 | // when 33 | component.connect(to: store) 34 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 35 | 36 | // then 37 | waitForExpectations(timeout: 0.1, handler: nil) 38 | XCTAssertEqual(component.propsHistory, [1, 2]) 39 | } 40 | 41 | func testConnect_store_connector() { 42 | // given 43 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 44 | let exp = expectation(description: "props is updated") 45 | let component = FakeComponent(propsDidSet: { props in 46 | guard props.count == 2 else { return } 47 | exp.fulfill() 48 | }) 49 | let connector = FakeTestStateConnector() 50 | 51 | // when 52 | component.connect(to: store, by: connector) 53 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 54 | 55 | // then 56 | waitForExpectations(timeout: 0.1, handler: nil) 57 | XCTAssertEqual(component.propsHistory, [1, 2]) 58 | } 59 | 60 | func testConnect_store_connector_keypath() { 61 | // given 62 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 63 | let exp = expectation(description: "props is updated") 64 | let component = FakeComponent(propsDidSet: { props in 65 | guard props.count == 2 else { return } 66 | exp.fulfill() 67 | }) 68 | let connector = FakeConnector() 69 | 70 | // when 71 | component.connect(to: store, by: connector, state: \.intValue) 72 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 73 | 74 | // then 75 | waitForExpectations(timeout: 0.1, handler: nil) 76 | XCTAssertEqual(component.propsHistory, [1, 2]) 77 | } 78 | 79 | func testConnect_store_connector_transform() { 80 | // given 81 | let store = Store(state: 1, reducer: reducer) 82 | let exp = expectation(description: "props is updated") 83 | let component = FakeComponent(propsDidSet: { props in 84 | guard props.count == 2 else { return } 85 | exp.fulfill() 86 | }) 87 | let connector = FakeConnector() 88 | 89 | // when 90 | component.connect(to: store, by: connector) { $0 } 91 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 92 | 93 | // then 94 | waitForExpectations(timeout: 0.1, handler: nil) 95 | XCTAssertEqual(component.propsHistory, [1, 2]) 96 | } 97 | 98 | func testConnect_store_removeDuplicates_connector() { 99 | // Given 100 | let store = Store(state: 1, reducer: reducer) 101 | let exp = expectation(description: "all state updates received") 102 | let component = FakeComponent(propsDidSet: { props in 103 | guard props.count == 3 else { return } 104 | exp.fulfill() 105 | }) 106 | let connector = FakeConnector() 107 | component.connect(to: store, removeDuplicates: true, by: connector) 108 | 109 | // When 110 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 111 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 112 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 113 | 114 | // Then 115 | waitForExpectations(timeout: 0.1, handler: nil) 116 | XCTAssertEqual(connector.statesHistory, [1, 10, 20]) 117 | } 118 | 119 | func testConnect_store_removeDuplicates_connector_keypath() { 120 | // Given 121 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 122 | let exp = expectation(description: "all state updates received") 123 | let component = FakeComponent(propsDidSet: { props in 124 | guard props.count == 3 else { return } 125 | exp.fulfill() 126 | }) 127 | let connector = FakeConnector() 128 | component.connect(to: store, removeDuplicates: true, by: connector, state: \.intValue) 129 | 130 | // When 131 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 132 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 133 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 134 | 135 | // Then 136 | waitForExpectations(timeout: 0.1, handler: nil) 137 | XCTAssertEqual(connector.statesHistory, [1, 10, 20]) 138 | } 139 | 140 | func testConnect_store_removeDuplicates_connector_transform() { 141 | // Given 142 | let store = Store(state: 1, reducer: reducer) 143 | let exp = expectation(description: "all state updates received") 144 | let component = FakeComponent(propsDidSet: { props in 145 | guard props.count == 3 else { return } 146 | exp.fulfill() 147 | }) 148 | let connector = FakeConnector() 149 | component.connect(to: store, removeDuplicates: true, by: connector) { $0 } 150 | 151 | // When 152 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 153 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 154 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 155 | 156 | // Then 157 | waitForExpectations(timeout: 0.1, handler: nil) 158 | XCTAssertEqual(connector.statesHistory, [1, 10, 20]) 159 | } 160 | 161 | func testComponentAndConnector_DeinitCorrectly() { 162 | // given 163 | let store = Store(state: 1, reducer: reducer) 164 | let exp = expectation(description: "component is deinited") 165 | exp.expectedFulfillmentCount = 2 166 | var component: FakeComponent? = FakeComponent { 167 | exp.fulfill() 168 | } 169 | 170 | var connector: FakeConnector? = FakeConnector { 171 | exp.fulfill() 172 | } 173 | 174 | // when 175 | component?.connect(to: store, by: connector!) 176 | component = nil 177 | connector = nil 178 | 179 | // then 180 | wait(for: [exp], timeout: 0.1) 181 | } 182 | 183 | func testScopeStoreLivesUntillConnectExists() { 184 | // Given 185 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 186 | let exp = expectation(description: "all state updates received") 187 | let component = FakeComponent(propsDidSet: { props in 188 | guard props.count == 2 else { return } 189 | exp.fulfill() 190 | }) 191 | let connector = FakeConnector() 192 | component.connect(to: store.scope(\.intValue), removeDuplicates: ==, by: connector) { $0 } 193 | 194 | // When 195 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 196 | 197 | // Then 198 | waitForExpectations(timeout: 0.1, handler: nil) 199 | XCTAssertEqual(connector.statesHistory, [1, 10]) 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /UDFTests/Component+SelfConnectorTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Component+SelfConnectorTests.swift 3 | // UDFTests 4 | // 5 | // Created by Anton Goncharov on 02.02.2023. 6 | // 7 | 8 | import XCTest 9 | @testable import UDF 10 | 11 | class ComponentSelfConnectorTests: XCTestCase { 12 | 13 | func reducer(state: inout Int, action: Action) { 14 | if case let FakeComponentConnector.Actions.valueDidChange(newValue) = action { 15 | state = newValue 16 | } 17 | } 18 | 19 | func reducer(state: inout TestState, action: Action) { 20 | reducer(state: &state.intValue, action: action) 21 | } 22 | 23 | func testConnect_store() { 24 | // given 25 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 26 | let exp = expectation(description: "props is updated") 27 | let component = FakeTestStateComponentConnector(propsDidSet: { props in 28 | guard props.count == 2 else { return } 29 | exp.fulfill() 30 | }) 31 | 32 | // when 33 | component.connect(to: store) 34 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 35 | 36 | // then 37 | waitForExpectations(timeout: 0.1, handler: nil) 38 | XCTAssertEqual(component.propsHistory, [1, 2]) 39 | } 40 | 41 | func testConnect_store_keypath() { 42 | // given 43 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 44 | let exp = expectation(description: "props is updated") 45 | let component = FakeComponentConnector(propsDidSet: { props in 46 | guard props.count == 2 else { return } 47 | exp.fulfill() 48 | }) 49 | 50 | // when 51 | component.connect(to: store, state: \.intValue) 52 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 53 | 54 | // then 55 | waitForExpectations(timeout: 0.1, handler: nil) 56 | XCTAssertEqual(component.propsHistory, [1, 2]) 57 | } 58 | 59 | func testConnect_store_transform() { 60 | // given 61 | let store = Store(state: 1, reducer: reducer) 62 | let exp = expectation(description: "props is updated") 63 | let component = FakeComponentConnector(propsDidSet: { props in 64 | guard props.count == 2 else { return } 65 | exp.fulfill() 66 | }) 67 | 68 | // when 69 | component.connect(to: store) { $0 } 70 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 71 | 72 | // then 73 | waitForExpectations(timeout: 0.1, handler: nil) 74 | XCTAssertEqual(component.propsHistory, [1, 2]) 75 | } 76 | 77 | func testConnect_store_removeDuplicates() { 78 | // Given 79 | let store = Store(state: 1, reducer: reducer) 80 | let exp = expectation(description: "all state updates received") 81 | let component = FakeComponentConnector(propsDidSet: { props in 82 | guard props.count == 3 else { return } 83 | exp.fulfill() 84 | }) 85 | component.connect(to: store, removeDuplicates: true) 86 | 87 | // When 88 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 89 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 90 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 91 | 92 | // Then 93 | waitForExpectations(timeout: 0.1, handler: nil) 94 | XCTAssertEqual(component.statesHistory, [1, 10, 20]) 95 | } 96 | 97 | func testConnect_store_removeDuplicates_keypath() { 98 | // given 99 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 100 | let exp = expectation(description: "props is updated") 101 | let component = FakeComponentConnector(propsDidSet: { props in 102 | guard props.count == 3 else { return } 103 | exp.fulfill() 104 | }) 105 | 106 | // when 107 | component.connect(to: store, removeDuplicates: true, state: \.intValue) 108 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 109 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 110 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 111 | 112 | // then 113 | waitForExpectations(timeout: 0.1, handler: nil) 114 | XCTAssertEqual(component.statesHistory, [1, 10, 20]) 115 | } 116 | 117 | func testConnect_store_removeDuplicates_transform() { 118 | // given 119 | let store = Store(state: 1, reducer: reducer) 120 | let exp = expectation(description: "props is updated") 121 | let component = FakeComponentConnector(propsDidSet: { props in 122 | guard props.count == 3 else { return } 123 | exp.fulfill() 124 | }) 125 | 126 | // when 127 | component.connect(to: store, removeDuplicates: true) { $0 } 128 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 129 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 130 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 131 | 132 | // then 133 | waitForExpectations(timeout: 0.1, handler: nil) 134 | XCTAssertEqual(component.statesHistory, [1, 10, 20]) 135 | } 136 | 137 | func testWhenComponentIsConnector_DeinitCorrectly() { 138 | // given 139 | let store = Store(state: 1, reducer: reducer) 140 | let exp = expectation(description: "component is deinited") 141 | var component: FakeComponentConnector? = FakeComponentConnector(onDeinit: { 142 | exp.fulfill() 143 | }) 144 | 145 | // when 146 | component?.connect(to: store) 147 | component = nil 148 | 149 | // then 150 | wait(for: [exp], timeout: 0.1) 151 | } 152 | 153 | func testEqualPropsDoesntNotifyComponent() { 154 | // Given 155 | let store = Store(state: 1, reducer: reducer) 156 | let exp = expectation(description: "all state updates received") 157 | let component = FakeComponentConnector(propsDidSet: { props in 158 | guard props.count == 3 else { return } 159 | exp.fulfill() 160 | }) 161 | component.connect(to: store) 162 | 163 | // When 164 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 165 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 166 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 167 | 168 | // Then 169 | waitForExpectations(timeout: 0.1, handler: nil) 170 | XCTAssertEqual(component.propsHistory, [1, 10, 20]) 171 | } 172 | 173 | func testScopeStoreLivesUntillConnectExists() { 174 | // Given 175 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 176 | let exp = expectation(description: "all state updates received") 177 | let component = FakeComponentConnector(propsDidSet: { props in 178 | guard props.count == 2 else { return } 179 | exp.fulfill() 180 | }) 181 | component.connect(to: store.scope(\.intValue), removeDuplicates: ==) { $0 } 182 | 183 | // When 184 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 185 | 186 | // Then 187 | waitForExpectations(timeout: 0.1, handler: nil) 188 | XCTAssertEqual(component.propsHistory, [1, 10]) 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /UDFTests/Component+stateToPropsTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Component+stateToPropsTests.swift 3 | // UDFTests 4 | // 5 | // Created by Anton Goncharov on 02.02.2023. 6 | // 7 | 8 | import XCTest 9 | @testable import UDF 10 | 11 | class ComponentStateToPropsTests: XCTestCase { 12 | 13 | func reducer(state: inout Int, action: Action) { 14 | if case let FakeComponentConnector.Actions.valueDidChange(newValue) = action { 15 | state = newValue 16 | } 17 | } 18 | 19 | func reducer(state: inout TestState, action: Action) { 20 | reducer(state: &state.intValue, action: action) 21 | } 22 | 23 | var statesHistory = [Int]() 24 | func stateToProps(value: Int, dispatcher: ActionDispatcher) -> Int { 25 | statesHistory.append(value) 26 | return value 27 | } 28 | 29 | override func setUp() { 30 | super.setUp() 31 | statesHistory = [] 32 | } 33 | 34 | func testConnect_store_stateToProps() { 35 | // given 36 | let store = Store(state: 1, reducer: reducer) 37 | let exp = expectation(description: "props is updated") 38 | let component = FakeComponent(propsDidSet: { props in 39 | guard props.count == 2 else { return } 40 | exp.fulfill() 41 | }) 42 | 43 | // when 44 | component.connect(to: store, stateToProps: stateToProps) 45 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 46 | 47 | // then 48 | waitForExpectations(timeout: 0.1, handler: nil) 49 | XCTAssertEqual(component.propsHistory, [1, 2]) 50 | } 51 | 52 | func testConnect_store_stateToProps_keypath() { 53 | // given 54 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 55 | let exp = expectation(description: "props is updated") 56 | let component = FakeComponent(propsDidSet: { props in 57 | guard props.count == 2 else { return } 58 | exp.fulfill() 59 | }) 60 | 61 | // when 62 | component.connect(to: store, stateToProps: stateToProps, state: \.intValue) 63 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 64 | 65 | // then 66 | waitForExpectations(timeout: 0.1, handler: nil) 67 | XCTAssertEqual(component.propsHistory, [1, 2]) 68 | } 69 | 70 | func testConnect_store_stateToProps_transform() { 71 | // given 72 | let store = Store(state: 1, reducer: reducer) 73 | let exp = expectation(description: "props is updated") 74 | let component = FakeComponent(propsDidSet: { props in 75 | guard props.count == 2 else { return } 76 | exp.fulfill() 77 | }) 78 | 79 | // when 80 | component.connect(to: store, stateToProps: stateToProps) { $0 } 81 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(2)) 82 | 83 | // then 84 | waitForExpectations(timeout: 0.1, handler: nil) 85 | XCTAssertEqual(component.propsHistory, [1, 2]) 86 | } 87 | 88 | func testConnect_store_removeDuplicates_stateToProps() { 89 | // Given 90 | let store = Store(state: 1, reducer: reducer) 91 | let exp = expectation(description: "all state updates received") 92 | let component = FakeComponent(propsDidSet: { props in 93 | guard props.count == 3 else { return } 94 | exp.fulfill() 95 | }) 96 | component.connect(to: store, removeDuplicates: true, stateToProps: stateToProps) 97 | 98 | // When 99 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 100 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 101 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 102 | 103 | // Then 104 | waitForExpectations(timeout: 0.1, handler: nil) 105 | XCTAssertEqual(statesHistory, [1, 10, 20]) 106 | } 107 | 108 | func testConnect_store_removeDuplicates_stateToProps_keypath() { 109 | // Given 110 | let store = Store(state: TestState(intValue: 1), reducer: reducer) 111 | let exp = expectation(description: "all state updates received") 112 | let component = FakeComponent(propsDidSet: { props in 113 | guard props.count == 3 else { return } 114 | exp.fulfill() 115 | }) 116 | component.connect(to: store, removeDuplicates: true, stateToProps: stateToProps, state: \.intValue) 117 | 118 | // When 119 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 120 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 121 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 122 | 123 | // Then 124 | waitForExpectations(timeout: 0.1, handler: nil) 125 | XCTAssertEqual(statesHistory, [1, 10, 20]) 126 | } 127 | 128 | func testConnect_store_removeDuplicates_stateToProps_transform() { 129 | // Given 130 | let store = Store(state: 1, reducer: reducer) 131 | let exp = expectation(description: "all state updates received") 132 | let component = FakeComponent(propsDidSet: { props in 133 | guard props.count == 3 else { return } 134 | exp.fulfill() 135 | }) 136 | component.connect(to: store, removeDuplicates: true, stateToProps: stateToProps) { $0 } 137 | 138 | // When 139 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(10)) 140 | store.dispatch(FakeComponentConnector.Actions.nothingDidHappen) 141 | store.dispatch(FakeComponentConnector.Actions.valueDidChange(20)) 142 | 143 | // Then 144 | waitForExpectations(timeout: 0.1, handler: nil) 145 | XCTAssertEqual(statesHistory, [1, 10, 20]) 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /UDFTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /UDFTests/SideEffectTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SideEffectTests.swift 3 | // UDFTests 4 | // 5 | // Created by Anton Goncharov on 26.10.2022. 6 | // 7 | 8 | import XCTest 9 | @testable import UDF 10 | 11 | class SideEffectTests: XCTestCase { 12 | 13 | struct TestState: Equatable { 14 | var localState: Int = 0 15 | } 16 | 17 | var disposer: Disposer! 18 | 19 | override func setUp() { 20 | super.setUp() 21 | disposer = .init() 22 | } 23 | 24 | override func tearDown() { 25 | super.tearDown() 26 | disposer = nil 27 | } 28 | 29 | func testSideEffectExecution() { 30 | // given 31 | let exp = expectation(description: "sideEffect is executed") 32 | let fakeSideEffect = FakeSideEffect {_ in 33 | exp.fulfill() 34 | } 35 | let reducer: SideEffectReducer = { state, action in 36 | if action is FakeAction { 37 | return fakeSideEffect 38 | } 39 | return nil 40 | } 41 | let store = Store(state: 1, reducer: reducer) 42 | 43 | // when 44 | store.dispatch(FakeAction()) 45 | 46 | // then 47 | waitForExpectations(timeout: 0.1, handler: nil) 48 | 49 | guard let dispatcher = fakeSideEffect.dispatcher as? Store else { 50 | XCTFail("dispatcher is not a Store of expected type (Store)") 51 | return 52 | } 53 | XCTAssertEqual(dispatcher, store) 54 | } 55 | 56 | func testLongSideEffectDoesntBlockReduceExecution() { 57 | // given 58 | let exp = expectation(description: "OtherFakeAction is dispatched") 59 | let fakeSideEffect = FakeSideEffect {_ in 60 | sleep(10) 61 | } 62 | let reducer: SideEffectReducer = { state, action in 63 | if action is FakeAction { 64 | return fakeSideEffect 65 | } 66 | return nil 67 | } 68 | let store = Store(state: 1, reducer: reducer) 69 | 70 | store.onAction { _, action in 71 | if action is OtherFakeAction { 72 | exp.fulfill() 73 | } 74 | }.dispose(on: disposer) 75 | 76 | // when 77 | store.dispatch(FakeAction()) 78 | store.dispatch(OtherFakeAction()) 79 | 80 | // then 81 | waitForExpectations(timeout: 0.1, handler: nil) 82 | } 83 | 84 | func testLongSideEffectDoesntBlockNextSideEffect() { 85 | // given 86 | let exp = expectation(description: "next sideEffect is executed") 87 | let fakeSideEffect = FakeSideEffect {_ in 88 | sleep(10) 89 | } 90 | 91 | let nextSideEffect = FakeSideEffect {_ in 92 | exp.fulfill() 93 | } 94 | let reducer: SideEffectReducer = { state, action in 95 | if action is FakeAction { 96 | return fakeSideEffect 97 | } 98 | if action is OtherFakeAction { 99 | return nextSideEffect 100 | } 101 | return nil 102 | } 103 | let store = Store(state: 1, reducer: reducer) 104 | 105 | // when 106 | store.dispatch(FakeAction()) 107 | store.dispatch(OtherFakeAction()) 108 | 109 | // then 110 | waitForExpectations(timeout: 0.1, handler: nil) 111 | } 112 | 113 | func testSideEffectsExecuteInReducerCallOrder() { 114 | // given 115 | let exp = expectation(description: "last sideEffect is executed") 116 | var orderOfSideEffectExection = [Int]() 117 | 118 | let firstSideEffect = FakeSideEffect {_ in 119 | orderOfSideEffectExection.append(1) 120 | } 121 | 122 | let secondSideEffect = FakeSideEffect {_ in 123 | orderOfSideEffectExection.append(2) 124 | } 125 | 126 | let thirdSideEffect = FakeSideEffect {_ in 127 | orderOfSideEffectExection.append(3) 128 | exp.fulfill() 129 | } 130 | 131 | let firstReducer: SideEffectReducer = { state, action in 132 | if action is FakeAction { 133 | return firstSideEffect 134 | } 135 | return nil 136 | } 137 | 138 | let secondReducer: SideEffectReducer = { state, action in 139 | if action is FakeAction { 140 | return secondSideEffect 141 | } 142 | return nil 143 | } 144 | 145 | let thirdReducer: SideEffectReducer = { state, action in 146 | if action is FakeAction { 147 | return thirdSideEffect 148 | } 149 | return nil 150 | } 151 | 152 | func reducer(state: inout Int, action: Action) -> SideEffect { 153 | combine { 154 | firstReducer(&state, action) 155 | secondReducer(&state, action) 156 | thirdReducer(&state, action) 157 | } 158 | } 159 | 160 | 161 | let store = Store(state: 1, reducer: reducer) 162 | 163 | // when 164 | store.dispatch(FakeAction()) 165 | 166 | // then 167 | waitForExpectations(timeout: 0.1, handler: nil) 168 | XCTAssertEqual(orderOfSideEffectExection, [1, 2, 3]) 169 | } 170 | 171 | func testSideEffectDeinitAfterExecution() { 172 | // given 173 | let exp = expectation(description: "sideEffect is executed") 174 | var deinitIsCalled = false 175 | 176 | let reducer: SideEffectReducer = { state, action in 177 | if action is FakeAction { 178 | return FakeSideEffect( 179 | executeClosure: { dispatcher in 180 | dispatcher.dispatch(OtherFakeAction()) 181 | }, 182 | onDeinit: { deinitIsCalled = true }) 183 | } 184 | return nil 185 | } 186 | let store = Store(state: 1, reducer: reducer) 187 | 188 | store.onAction { _, action in 189 | if action is OtherFakeAction { 190 | exp.fulfill() 191 | } 192 | }.dispose(on: disposer) 193 | 194 | // when 195 | store.dispatch(FakeAction()) 196 | 197 | // then 198 | waitForExpectations(timeout: 0.1, handler: nil) 199 | XCTAssertTrue(deinitIsCalled) 200 | } 201 | 202 | func testSideEffectExecutesFromScopeStoreAction() { 203 | // given 204 | let exp = expectation(description: "sideEffect is executed") 205 | let fakeSideEffect = FakeSideEffect {_ in 206 | exp.fulfill() 207 | } 208 | 209 | let reducer: SideEffectReducer = { state, action in 210 | if action is FakeAction { 211 | return fakeSideEffect 212 | } 213 | return nil 214 | } 215 | 216 | let store = Store(state: TestState(), reducer: reducer) 217 | let scopeStore = store.scope(\.localState) 218 | 219 | // when 220 | scopeStore.dispatch(FakeAction()) 221 | 222 | // then 223 | waitForExpectations(timeout: 0.1, handler: nil) 224 | } 225 | } 226 | 227 | extension Store: Equatable { 228 | public static func == (left: Store, right: Store) -> Bool { 229 | return ObjectIdentifier(left) == ObjectIdentifier(right) 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /UDFTests/StoreBasicTests.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import XCTest 17 | 18 | @testable import UDF 19 | 20 | class StoreBasicTests: XCTestCase { 21 | 22 | let disposer = Disposer() 23 | 24 | // MARK: Components 25 | 26 | func testComponentSubscribedAndReceivesCurrentState() { 27 | // given 28 | let exp = expectation(description: "state is right") 29 | let state = 7 30 | let sut = Store(state: state) { _, _ in } 31 | 32 | // when 33 | sut.observe { value in 34 | if value == state { exp.fulfill() } 35 | }.dispose(on: disposer) 36 | 37 | // then 38 | wait(for: [exp], timeout: 0.1) 39 | } 40 | 41 | func testComponentSubscribed_StateHasChanged_ComponentRecevesAllStateChanges() { 42 | // given 43 | let exp = expectation(description: "state is right") 44 | let expectedStateSequence = [1, 2] 45 | func reduce(_ state: inout Int, _: Action) { 46 | state = 2 47 | } 48 | var result = [Int]() 49 | let sut = Store(state: 1, reducer: reduce) 50 | 51 | // when 52 | sut.observe { value in 53 | result.append(value) 54 | if result.count != expectedStateSequence.count { 55 | sut.dispatch(FakeAction()) 56 | } else if result == expectedStateSequence { 57 | exp.fulfill() 58 | } 59 | }.dispose(on: disposer) 60 | 61 | // then 62 | wait(for: [exp], timeout: 0.5) 63 | } 64 | 65 | func testMiddlewareSubscribedAndDontReceiveCurrentState() { 66 | // given 67 | let sut = Store(state: 7) { _, _ in } 68 | 69 | //when 70 | sut.onAction { _, _ in 71 | XCTFail("Received state on action") 72 | }.dispose(on: disposer) 73 | } 74 | 75 | func testMiddlewareSubscribed_StateHasChanged_MiddlewareReceivesStateAfterChangesAndAction() { 76 | // given 77 | let firstValue = 1 78 | let secondValue = 2 79 | let exp = expectation(description: "action and state is right") 80 | 81 | func reduce(_ state: inout Int, _: Action) { 82 | state = secondValue 83 | } 84 | 85 | let sut = Store(state: firstValue, reducer: reduce) 86 | 87 | // when 88 | sut.onAction { value, action in 89 | if value == secondValue, action is FakeAction { 90 | exp.fulfill() 91 | } else { 92 | XCTFail("Wrong value or action type") 93 | } 94 | 95 | }.dispose(on: disposer) 96 | 97 | sut.dispatch(FakeAction()) 98 | 99 | // then 100 | wait(for: [exp], timeout: 0.5) 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /UDFTests/StoreDoubleScopeTests.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import XCTest 17 | @testable import UDF 18 | 19 | class StoreDoubleScopeTests: XCTestCase { 20 | struct BigState: Equatable { 21 | var testState: TestState 22 | } 23 | 24 | struct TestState: Equatable { 25 | var localState: Int 26 | var otherLocalState: String 27 | } 28 | 29 | var store: Store! 30 | let disposer = Disposer() 31 | struct FakeAction: Action { } 32 | 33 | override func setUp() { 34 | super.setUp() 35 | } 36 | 37 | override class func tearDown() { 38 | super.tearDown() 39 | } 40 | 41 | func testScopeStoreReturnActualGlobalStateOnSubscribe() { 42 | // given 43 | let initState = BigState(testState: TestState(localState: 1, otherLocalState: "test")) 44 | func emptyReducer(state _: inout BigState, with _: Action) { } 45 | store = Store(state: initState, reducer: emptyReducer) 46 | var localState: Int? 47 | let expectation = self.expectation(description: #function) 48 | let localStore = store 49 | .scope(\.testState) 50 | .scope(\.localState) 51 | 52 | // when 53 | localStore.observe { 54 | localState = $0 55 | expectation.fulfill() 56 | }.dispose(on: disposer) 57 | 58 | // then 59 | waitForExpectations(timeout: 0.1, handler: nil) 60 | XCTAssertEqual(localState, 1) 61 | } 62 | 63 | func testScopeStoreReceiveStateUpdateFromGlobalStore() { 64 | // given 65 | let initState = BigState(testState: TestState(localState: 1, otherLocalState: "test")) 66 | func reducer(state: inout BigState, with _: Action) { 67 | state.testState.localState = 42 68 | } 69 | store = Store(state: initState, reducer: reducer) 70 | var localStates = [Int]() 71 | let expectation = self.expectation(description: #function) 72 | let localStore = store 73 | .scope(\.testState) 74 | .scope(\.localState) 75 | 76 | // when 77 | localStore.observe { 78 | localStates.append($0) 79 | if localStates.count == 2 { expectation.fulfill() } 80 | }.dispose(on: disposer) 81 | store.dispatch(FakeAction()) 82 | 83 | // then 84 | waitForExpectations(timeout: 0.1, handler: nil) 85 | XCTAssertEqual(localStates, [1, 42]) 86 | } 87 | 88 | func testScopeStoreReceiveActionFromGlobalStore() { 89 | // given 90 | let initState = BigState(testState: TestState(localState: 1, otherLocalState: "test")) 91 | func emptyReducer(state _: inout BigState, with _: Action) { } 92 | store = Store(state: initState, reducer: emptyReducer) 93 | var state: Int? 94 | var action: Action? 95 | let expectation = self.expectation(description: #function) 96 | let localStore = store 97 | .scope(\.testState) 98 | .scope(\.localState) 99 | 100 | // when 101 | localStore.onAction { 102 | state = $0 103 | action = $1 104 | expectation.fulfill() 105 | }.dispose(on: disposer) 106 | store.dispatch(FakeAction()) 107 | 108 | // then 109 | waitForExpectations(timeout: 0.1, handler: nil) 110 | XCTAssertEqual(state, 1) 111 | XCTAssertTrue(action is FakeAction) 112 | } 113 | 114 | func testGlobalStoreReceiveStateUpdateFromLocalStore() { 115 | // given 116 | let initState = BigState(testState: TestState(localState: 1, otherLocalState: "test")) 117 | func reducer(state: inout BigState, with _: Action) { 118 | state.testState.localState = 42 119 | } 120 | store = Store(state: initState, reducer: reducer) 121 | var globalStates = [BigState]() 122 | let expectation = self.expectation(description: #function) 123 | let localStore = store 124 | .scope(\.testState) 125 | .scope(\.localState) 126 | 127 | // when 128 | store.observe { 129 | globalStates.append($0) 130 | if globalStates.count == 2 { expectation.fulfill() } 131 | }.dispose(on: disposer) 132 | localStore.dispatch(FakeAction()) 133 | 134 | // then 135 | waitForExpectations(timeout: 0.1, handler: nil) 136 | XCTAssertEqual(globalStates.last, BigState(testState: TestState(localState: 42, otherLocalState: "test"))) 137 | } 138 | 139 | func testGlobalStoreReceiveActionFromLocalStore() { 140 | // given 141 | let initState = BigState(testState: TestState(localState: 1, otherLocalState: "test")) 142 | func emptyReducer(state _: inout BigState, with _: Action) { } 143 | store = Store(state: initState, reducer: emptyReducer) 144 | var state: BigState? 145 | var action: Action? 146 | let expectation = self.expectation(description: #function) 147 | let localStore = store 148 | .scope(\.testState) 149 | .scope(\.localState) 150 | 151 | // when 152 | store.onAction { 153 | state = $0 154 | action = $1 155 | expectation.fulfill() 156 | }.dispose(on: disposer) 157 | localStore.dispatch(FakeAction()) 158 | 159 | // then 160 | waitForExpectations(timeout: 0.1, handler: nil) 161 | XCTAssertEqual(state, BigState(testState: TestState(localState: 1, otherLocalState: "test"))) 162 | XCTAssertTrue(action is FakeAction) 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /UDFTests/StoreScopeTests.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | import XCTest 16 | @testable import UDF 17 | 18 | class StoreScopeTests: XCTestCase { 19 | struct TestState: Equatable { 20 | var localState: Int 21 | var otherLocalState: String 22 | } 23 | 24 | var store: Store! 25 | let disposer = Disposer() 26 | struct FakeAction: Action { } 27 | 28 | override func setUp() { 29 | super.setUp() 30 | } 31 | 32 | override class func tearDown() { 33 | super.tearDown() 34 | } 35 | 36 | func testScopeStoreReturnActualGlobalStateOnSubscribe() { 37 | // given 38 | let initState = TestState(localState: 1, otherLocalState: "test") 39 | func emptyReducer(state _: inout TestState, with _: Action) { } 40 | store = Store(state: initState, reducer: emptyReducer) 41 | var localState: Int? 42 | let expectation = self.expectation(description: #function) 43 | let localStore = store.scope(\.localState) 44 | 45 | // when 46 | localStore.observe { 47 | localState = $0 48 | expectation.fulfill() 49 | }.dispose(on: disposer) 50 | 51 | // then 52 | waitForExpectations(timeout: 0.1, handler: nil) 53 | XCTAssertEqual(localState, 1) 54 | } 55 | 56 | func testScopeStoreReceiveStateUpdateFromGlobalStore() { 57 | // given 58 | let initState = TestState(localState: 1, otherLocalState: "test") 59 | func reducer(state: inout TestState, with _: Action) { 60 | state.localState = 42 61 | } 62 | store = Store(state: initState, reducer: reducer) 63 | var localStates = [Int]() 64 | let expectation = self.expectation(description: #function) 65 | let localStore = store.scope(\.localState) 66 | 67 | // when 68 | localStore.observe { 69 | localStates.append($0) 70 | if localStates.count == 2 { expectation.fulfill() } 71 | }.dispose(on: disposer) 72 | store.dispatch(FakeAction()) 73 | 74 | // then 75 | waitForExpectations(timeout: 0.1, handler: nil) 76 | XCTAssertEqual(localStates, [1, 42]) 77 | } 78 | 79 | func testScopeStoreReceiveActionFromGlobalStore() { 80 | // given 81 | let initState = TestState(localState: 1, otherLocalState: "test") 82 | func emptyReducer(state _: inout TestState, with _: Action) { } 83 | store = Store(state: initState, reducer: emptyReducer) 84 | var state: Int? 85 | var action: Action? 86 | let expectation = self.expectation(description: #function) 87 | let localStore = store.scope(\.localState) 88 | 89 | // when 90 | localStore.onAction { 91 | state = $0 92 | action = $1 93 | expectation.fulfill() 94 | }.dispose(on: disposer) 95 | store.dispatch(FakeAction()) 96 | 97 | // then 98 | waitForExpectations(timeout: 0.1, handler: nil) 99 | XCTAssertEqual(state, 1) 100 | XCTAssertTrue(action is FakeAction) 101 | } 102 | 103 | func testScopeStoreReceiveUpdatedStateInActionListener() { 104 | // given 105 | let initState = TestState(localState: 1, otherLocalState: "test") 106 | func reducer(state: inout TestState, with _: Action) { 107 | state.localState = 42 108 | } 109 | store = Store(state: initState, reducer: reducer) 110 | var state: Int? 111 | var action: Action? 112 | let expectation = self.expectation(description: #function) 113 | let localStore = store.scope(\.localState) 114 | 115 | // when 116 | localStore.onAction { 117 | state = $0 118 | action = $1 119 | expectation.fulfill() 120 | }.dispose(on: disposer) 121 | store.dispatch(FakeAction()) 122 | 123 | // then 124 | waitForExpectations(timeout: 0.1, handler: nil) 125 | XCTAssertEqual(state, 42) 126 | XCTAssertTrue(action is FakeAction) 127 | 128 | } 129 | 130 | func testGlobalStoreReceiveStateUpdateFromLocalStore() { 131 | // given 132 | let initState = TestState(localState: 1, otherLocalState: "test") 133 | func reducer(state: inout TestState, with _: Action) { 134 | state.localState = 42 135 | } 136 | store = Store(state: initState, reducer: reducer) 137 | var globalStates = [TestState]() 138 | let expectation = self.expectation(description: #function) 139 | let localStore = store.scope(\.localState) 140 | 141 | // when 142 | store.observe { 143 | globalStates.append($0) 144 | if globalStates.count == 2 { expectation.fulfill() } 145 | }.dispose(on: disposer) 146 | localStore.dispatch(FakeAction()) 147 | 148 | // then 149 | waitForExpectations(timeout: 0.1, handler: nil) 150 | XCTAssertEqual(globalStates.last, TestState(localState: 42, otherLocalState: "test")) 151 | } 152 | 153 | func testGlobalStoreReceiveActionFromLocalStore() { 154 | // given 155 | let initState = TestState(localState: 1, otherLocalState: "test") 156 | func emptyReducer(state _: inout TestState, with _: Action) { } 157 | store = Store(state: initState, reducer: emptyReducer) 158 | var state: TestState? 159 | var action: Action? 160 | let expectation = self.expectation(description: #function) 161 | let localStore = store.scope(\.localState) 162 | 163 | // when 164 | store.onAction { 165 | state = $0 166 | action = $1 167 | expectation.fulfill() 168 | }.dispose(on: disposer) 169 | localStore.dispatch(FakeAction()) 170 | 171 | // then 172 | waitForExpectations(timeout: 0.1, handler: nil) 173 | XCTAssertEqual(state, TestState(localState: 1, otherLocalState: "test")) 174 | XCTAssertTrue(action is FakeAction) 175 | } 176 | 177 | func testLocalStoreDoesNotNotifyIfLocalStateDidntChange() { 178 | // given 179 | let initState = TestState(localState: 1, otherLocalState: "test") 180 | func reducer(state: inout TestState, with _: Action) { 181 | state.otherLocalState = "42" 182 | } 183 | store = Store(state: initState, reducer: reducer) 184 | var localStates = [Int]() 185 | let expectation = self.expectation(description: #function) 186 | let localStore = store.scope(\.localState) 187 | 188 | // when 189 | localStore.observe { 190 | localStates.append($0) 191 | if localStates.count == 1 { expectation.fulfill() } 192 | }.dispose(on: disposer) 193 | store.dispatch(FakeAction()) 194 | 195 | // then 196 | waitForExpectations(timeout: 0.1, handler: nil) 197 | XCTAssertEqual(localStates, [1]) 198 | } 199 | 200 | func testScopeStoreLivesUntillObserveSubscribeExists() { 201 | // given 202 | let initState = TestState(localState: 1, otherLocalState: "test") 203 | func reducer(state: inout TestState, with _: Action) { 204 | state.localState = 42 205 | } 206 | store = Store(state: initState, reducer: reducer) 207 | var localStates = [Int]() 208 | let expectation = self.expectation(description: #function) 209 | 210 | // when 211 | store.scope(\.localState).observe { 212 | localStates.append($0) 213 | if localStates.count == 2 { expectation.fulfill() } 214 | }.dispose(on: disposer) 215 | store.dispatch(FakeAction()) 216 | 217 | // then 218 | waitForExpectations(timeout: 0.1, handler: nil) 219 | XCTAssertEqual(localStates, [1, 42]) 220 | } 221 | 222 | func testScopeStoreLivesUntillOnActionSubscribeExists() { 223 | // given 224 | let initState = TestState(localState: 1, otherLocalState: "test") 225 | func emptyReducer(state _: inout TestState, with _: Action) { } 226 | store = Store(state: initState, reducer: emptyReducer) 227 | var state: Int? 228 | var action: Action? 229 | let expectation = self.expectation(description: #function) 230 | 231 | // when 232 | store.scope(\.localState).onAction { 233 | state = $0 234 | action = $1 235 | expectation.fulfill() 236 | }.dispose(on: disposer) 237 | store.dispatch(FakeAction()) 238 | 239 | // then 240 | waitForExpectations(timeout: 0.1, handler: nil) 241 | XCTAssertEqual(state, 1) 242 | XCTAssertTrue(action is FakeAction) 243 | } 244 | } 245 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeAction.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | @testable import UDF 17 | 18 | struct FakeAction: Action {} 19 | 20 | struct OtherFakeAction: Action {} 21 | 22 | struct YeatAnotherFakeAction: Action {} 23 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeActionListener.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import UDF 17 | import Foundation 18 | 19 | class FakeActionListener: ViewActionListener { 20 | 21 | typealias Props = (Int, Action) 22 | 23 | var propsHistory = [(Int, Action)]() 24 | 25 | func update(props: (Int, Action)) { 26 | propsHistory.append(props) 27 | propsDidSet(propsHistory) 28 | } 29 | 30 | var disposer = Disposer() 31 | 32 | let onDeinit: () -> Void 33 | let propsDidSet: ([(Int, Action)]) -> Void 34 | 35 | init(onDeinit: @escaping () -> Void = { }, propsDidSet: @escaping ([(Int, Action)]) -> Void = { _ in }) { 36 | self.onDeinit = onDeinit 37 | self.propsDidSet = propsDidSet 38 | } 39 | 40 | deinit { 41 | onDeinit() 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeActionListenerAndConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import UDF 17 | 18 | class FakeActionListenerAndConnector: ViewActionListener, ActionListenerConnector { 19 | 20 | typealias Props = (Int, Action) 21 | 22 | var propsHistory = [(Int, Action)]() 23 | 24 | func update(props: (Int, Action)) { 25 | propsHistory.append(props) 26 | propsDidSet(propsHistory) 27 | } 28 | 29 | var disposer = Disposer() 30 | 31 | let onDeinit: () -> Void 32 | let propsDidSet: ([(Int, Action)]) -> Void 33 | 34 | init(onDeinit: @escaping () -> Void = { }, propsDidSet: @escaping ([(Int, Action)]) -> Void = { _ in }) { 35 | self.onDeinit = onDeinit 36 | self.propsDidSet = propsDidSet 37 | } 38 | 39 | func stateAndActionToProps(state: Int, action: Action) -> (Int, Action) { (state, action) } 40 | 41 | deinit { 42 | onDeinit() 43 | } 44 | } 45 | 46 | class FakeTestStateActionListenerAndConnector: ViewActionListener, ActionListenerConnector { 47 | 48 | typealias Props = (Int, Action) 49 | 50 | var propsHistory = [(Int, Action)]() 51 | 52 | func update(props: (Int, Action)) { 53 | propsHistory.append(props) 54 | propsDidSet(propsHistory) 55 | } 56 | 57 | var disposer = Disposer() 58 | 59 | let onDeinit: () -> Void 60 | let propsDidSet: ([(Int, Action)]) -> Void 61 | 62 | init(onDeinit: @escaping () -> Void = { }, propsDidSet: @escaping ([(Int, Action)]) -> Void = { _ in }) { 63 | self.onDeinit = onDeinit 64 | self.propsDidSet = propsDidSet 65 | } 66 | 67 | func stateAndActionToProps(state: TestState, action: Action) -> (Int, Action) { (state.intValue, action) } 68 | 69 | deinit { 70 | onDeinit() 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeActionListenerConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | import UDF 19 | 20 | class FakeActionListenerConnector: ActionListenerConnector { 21 | let onDeinit: () -> Void 22 | 23 | init(onDeinit: @escaping () -> Void = { }) { 24 | self.onDeinit = onDeinit 25 | } 26 | 27 | func stateAndActionToProps(state: Int, action: Action) -> (Int, Action) { (state, action) } 28 | 29 | deinit { 30 | onDeinit() 31 | } 32 | } 33 | 34 | class FakeTestStateActionListenerConnector: ActionListenerConnector { 35 | let onDeinit: () -> Void 36 | 37 | init(onDeinit: @escaping () -> Void = { }) { 38 | self.onDeinit = onDeinit 39 | } 40 | 41 | func stateAndActionToProps(state: TestState, action: Action) -> (Int, Action) { (state.intValue, action) } 42 | 43 | deinit { 44 | onDeinit() 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeComponent.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import UDF 17 | 18 | class FakeComponent: ViewComponent { 19 | 20 | typealias Props = Int 21 | 22 | var propsHistory = [Int]() 23 | 24 | var props = 0 { 25 | didSet { 26 | propsHistory.append(props) 27 | propsDidSet(propsHistory) 28 | } 29 | } 30 | 31 | var disposer = Disposer() 32 | 33 | let onDeinit: () -> Void 34 | let propsDidSet: ([Int]) -> Void 35 | 36 | init(onDeinit: @escaping () -> Void = { }, propsDidSet: @escaping ([Int]) -> Void = { _ in }) { 37 | self.onDeinit = onDeinit 38 | self.propsDidSet = propsDidSet 39 | } 40 | 41 | deinit { 42 | onDeinit() 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeComponentConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import UDF 17 | 18 | class FakeComponentConnector: ViewComponent, Connector { 19 | typealias Props = Int 20 | 21 | var statesHistory = [Int]() 22 | var propsHistory = [Int]() 23 | 24 | var props = 0 { 25 | didSet { 26 | propsHistory.append(props) 27 | propsDidSet(propsHistory) 28 | } 29 | } 30 | 31 | var disposer = Disposer() 32 | 33 | let onDeinit: () -> Void 34 | let propsDidSet: ([Int]) -> Void 35 | 36 | init(onDeinit: @escaping () -> Void = { }, propsDidSet: @escaping ([Int]) -> Void = { _ in }) { 37 | self.onDeinit = onDeinit 38 | self.propsDidSet = propsDidSet 39 | } 40 | 41 | func stateToProps(state: Int, dispatcher _: ActionDispatcher) -> Int { 42 | statesHistory.append(state) 43 | return state 44 | } 45 | 46 | deinit { 47 | onDeinit() 48 | } 49 | } 50 | 51 | class FakeTestStateComponentConnector: ViewComponent, Connector { 52 | typealias Props = Int 53 | 54 | var propsHistory = [Int]() 55 | 56 | var props = 0 { 57 | didSet { 58 | propsHistory.append(props) 59 | propsDidSet(propsHistory) 60 | } 61 | } 62 | 63 | var disposer = Disposer() 64 | 65 | let onDeinit: () -> Void 66 | let propsDidSet: ([Int]) -> Void 67 | 68 | init(onDeinit: @escaping () -> Void = { }, propsDidSet: @escaping ([Int]) -> Void = { _ in }) { 69 | self.onDeinit = onDeinit 70 | self.propsDidSet = propsDidSet 71 | } 72 | 73 | func stateToProps(state: TestState, dispatcher _: ActionDispatcher) -> Int { state.intValue } 74 | 75 | deinit { 76 | onDeinit() 77 | } 78 | } 79 | 80 | extension FakeComponentConnector { 81 | enum Actions: Action, Equatable { 82 | case valueDidChange(Int) 83 | case nothingDidHappen 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeConnector.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import UDF 17 | 18 | class FakeConnector: Connector { 19 | 20 | var statesHistory = [Int]() 21 | 22 | let onDeinit: () -> Void 23 | 24 | init(onDeinit: @escaping () -> Void = { }) { 25 | self.onDeinit = onDeinit 26 | } 27 | 28 | func stateToProps(state: Int, dispatcher _: ActionDispatcher) -> Int { 29 | statesHistory.append(state) 30 | return state 31 | } 32 | 33 | deinit { 34 | onDeinit() 35 | } 36 | } 37 | 38 | class FakeTestStateConnector: Connector { 39 | let onDeinit: () -> Void 40 | 41 | init(onDeinit: @escaping () -> Void = { }) { 42 | self.onDeinit = onDeinit 43 | } 44 | 45 | func stateToProps(state: TestState, dispatcher _: ActionDispatcher) -> Int { state.intValue } 46 | 47 | deinit { 48 | onDeinit() 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/FakeSideEffect.swift: -------------------------------------------------------------------------------- 1 | // 2 | // FakeSideEffect.swift 3 | // UDFTests 4 | // 5 | // Created by Anton Goncharov on 11.11.2022. 6 | // 7 | 8 | import UDF 9 | 10 | class FakeSideEffect: SideEffectProtocol { 11 | 12 | typealias ExecuteClosure = (ActionDispatcher) -> Void 13 | 14 | var dispatcher: ActionDispatcher? 15 | var executeClosure: ExecuteClosure? 16 | let onDeinit: () -> Void 17 | 18 | init(executeClosure: ExecuteClosure? = nil, onDeinit: @escaping () -> Void = { }) { 19 | self.executeClosure = executeClosure 20 | self.onDeinit = onDeinit 21 | } 22 | 23 | func execute(with dispatcher: ActionDispatcher) { 24 | self.dispatcher = dispatcher 25 | executeClosure?(dispatcher) 26 | } 27 | 28 | deinit { 29 | onDeinit() 30 | } 31 | } 32 | 33 | enum FakeSideEffectActions: Action { 34 | case someAction 35 | } 36 | -------------------------------------------------------------------------------- /UDFTests/TestDoubles/TestState.swift: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Suol Innovations Ltd. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | import Foundation 17 | 18 | struct TestState: Equatable { 19 | var intValue: Int = 0 20 | var stringValue: String = "" 21 | } 22 | --------------------------------------------------------------------------------