diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md index 703a4725a..6f9886f04 100644 --- a/.github/CODE_OF_CONDUCT.md +++ b/.github/CODE_OF_CONDUCT.md @@ -2,83 +2,131 @@ ## Our Pledge -We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, caste, color, religion, or sexual +identity and orientation. -We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. ## Our Standards -Examples of behavior that contributes to a positive environment for our community include: +Examples of behavior that contributes to a positive environment for our +community include: * Demonstrating empathy and kindness toward other people * Being respectful of differing opinions, viewpoints, and experiences * Giving and gracefully accepting constructive feedback -* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience -* Focusing on what is best not just for us as individuals, but for the overall community +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the overall + community Examples of unacceptable behavior include: -* The use of sexualized language or imagery, and sexual attention or - advances of any kind +* The use of sexualized language or imagery, and sexual attention or advances of + any kind * Trolling, insulting or derogatory comments, and personal or political attacks * Public or private harassment -* Publishing others' private information, such as a physical or email - address, without their explicit permission +* Publishing others' private information, such as a physical or email address, + without their explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Enforcement Responsibilities -Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. -Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. ## Scope -This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. ## Enforcement -Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at support@pointfree.co. All complaints will be reviewed and investigated promptly and fairly. +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +. +All complaints will be reviewed and investigated promptly and fairly. -All community leaders are obligated to respect the privacy and security of the reporter of any incident. +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. ## Enforcement Guidelines -Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: ### 1. Correction -**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. -**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. ### 2. Warning -**Community Impact**: A violation through a single incident or series of actions. +**Community Impact**: A violation through a single incident or series of +actions. -**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or permanent +ban. ### 3. Temporary Ban -**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. -**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. ### 4. Permanent Ban -**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. -**Consequence**: A permanent ban from any sort of public interaction within the community. +**Consequence**: A permanent ban from any sort of public interaction within the +community. ## Attribution -This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, -available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.1, available at +[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]. -Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). - -[homepage]: https://www.contributor-covenant.org +Community Impact Guidelines were inspired by +[Mozilla's code of conduct enforcement ladder][Mozilla CoC]. For answers to common questions about this code of conduct, see the FAQ at -https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. +[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at +[https://www.contributor-covenant.org/translations][translations]. + +[homepage]: https://www.contributor-covenant.org +[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html +[Mozilla CoC]: https://github.com/mozilla/diversity +[FAQ]: https://www.contributor-covenant.org/faq +[translations]: https://www.contributor-covenant.org/translations diff --git a/README.md b/README.md index 707bef61a..c45b04c06 100644 --- a/README.md +++ b/README.md @@ -563,13 +563,14 @@ advanced usages. The documentation for releases and `main` are available here: * [`main`](https://pointfreeco.github.io/swift-composable-architecture/main/documentation/composablearchitecture) -* [0.49.0](https://pointfreeco.github.io/swift-composable-architecture/0.49.0/documentation/composablearchitecture/) +* [0.50.0](https://pointfreeco.github.io/swift-composable-architecture/0.50.0/documentation/composablearchitecture/)
Other versions - + + * [0.49.0](https://pointfreeco.github.io/swift-composable-architecture/0.49.0/documentation/composablearchitecture/) * [0.48.0](https://pointfreeco.github.io/swift-composable-architecture/0.48.0/documentation/composablearchitecture/) * [0.47.0](https://pointfreeco.github.io/swift-composable-architecture/0.47.0/documentation/composablearchitecture/) * [0.46.0](https://pointfreeco.github.io/swift-composable-architecture/0.46.0/documentation/composablearchitecture/) @@ -577,9 +578,10 @@ The documentation for releases and `main` are available here: * [0.44.0](https://pointfreeco.github.io/swift-composable-architecture/0.44.0/documentation/composablearchitecture/) * [0.43.0](https://pointfreeco.github.io/swift-composable-architecture/0.43.0/documentation/composablearchitecture/) * [0.42.0](https://pointfreeco.github.io/swift-composable-architecture/0.42.0/documentation/composablearchitecture/) - * [0.41.2](https://pointfreeco.github.io/swift-composable-architecture/0.41.0/documentation/composablearchitecture/) - * [0.40.2](https://pointfreeco.github.io/swift-composable-architecture/0.40.0/documentation/composablearchitecture/) + * [0.41.0](https://pointfreeco.github.io/swift-composable-architecture/0.41.0/documentation/composablearchitecture/) + * [0.40.0](https://pointfreeco.github.io/swift-composable-architecture/0.40.0/documentation/composablearchitecture/) * [0.39.0](https://pointfreeco.github.io/swift-composable-architecture/0.39.0/documentation/composablearchitecture/) + * [0.38.0](https://pointfreeco.github.io/swift-composable-architecture/0.38.0/documentation/composablearchitecture/)

diff --git a/Sources/ComposableArchitecture/Documentation.docc/Articles/MigratingToTheReducerProtocol.md b/Sources/ComposableArchitecture/Documentation.docc/Articles/MigratingToTheReducerProtocol.md index e5d33ff32..320f08a7c 100644 --- a/Sources/ComposableArchitecture/Documentation.docc/Articles/MigratingToTheReducerProtocol.md +++ b/Sources/ComposableArchitecture/Documentation.docc/Articles/MigratingToTheReducerProtocol.md @@ -492,10 +492,11 @@ But this means that you must explicitly thread all dependencies from the root of through to every child feature. This can be arduous and make it difficult to add, remove or change dependencies. -The library comes with a tool for managing dependencies in a more ergonomic manner, and even comes -with some common dependencies pre-integrated allowing you to access them with no additional work. -For example, the `date` dependency ships with the library so that you can declare your feature's -dependence on that functionality in the following way: +The Composable Architecture now uses the [Dependencies][swift-dependencies] library to manage +dependencies in a more ergonomic manner, and even comes with some common dependencies pre-integrated +allowing you to access them with no additional work. For example, the `date` dependency ships with +the library so that you can declare your feature's dependence on that functionality in the following +way: ```swift struct Feature: ReducerProtocol { @@ -508,6 +509,17 @@ struct Feature: ReducerProtocol { With that one declaration you can stop explicitly passing the date dependency through every layer of your application. A date function will be automatically provided to your feature's reducer. +> Important: [Dependencies][swift-dependencies] is powered by Swift task locals and is intended to +> be used in structured contexts. If your reducer's effects make use of escaping closures, then +> you must do additional work to propagate the dependencies to that context. For example, using +> a dependency from within a Combine operator such as `.map`, `.flatMap` and even `.filter` will +> use the default dependency value. +> +> See the [Dependencies documentation][swift-dependencies-docs] on +> [Dependency lifetimes][swift-dependencies-docs-lifetimes] for more information, and how to +> integrate the `@Dependency` property wrapper into pre-structured concurrency using the +> `withEscapedDependencies` function. + For domain-specific dependencies you can perform a little bit of upfront work to register your dependency with the system, and then it will be automatically available to every layer in your application: @@ -538,6 +550,10 @@ struct Feature: ReducerProtocol { For more information on designing your dependencies and providing live and test dependencies, see our article. +[swift-dependencies]: https://github.com/pointfreeco/swift-dependencies +[swift-dependencies-docs]: https://pointfreeco.github.io/swift-dependencies/main/documentation/dependencies/ +[swift-dependencies-docs-lifetimes]: https://pointfreeco.github.io/swift-dependencies/main/documentation/dependencies/lifetimes + ## Stores Stores can be initialized from an initial state and an instance of a type conforming to diff --git a/Sources/ComposableArchitecture/Effects/SignalProducer.swift b/Sources/ComposableArchitecture/Effects/SignalProducer.swift index 9d5d663c4..7bb92939c 100644 --- a/Sources/ComposableArchitecture/Effects/SignalProducer.swift +++ b/Sources/ComposableArchitecture/Effects/SignalProducer.swift @@ -323,9 +323,9 @@ extension EffectProducer where Failure == Swift.Error { } } -extension Effect { +extension EffectProducer { - /// Turns any effect into an ``Effect`` for any output and failure type by ignoring all output + /// Turns any effect into an ``EffectProducer`` for any output and failure type by ignoring all output /// and any failure. /// /// This is useful for times you want to fire off an effect but don't want to feed any data back @@ -348,7 +348,7 @@ extension Effect { public func fireAndForget( outputType: NewValue.Type = NewValue.self, failureType: NewError.Type = NewError.self - ) -> Effect { + ) -> EffectProducer { self .producer .flatMapError { _ in .empty } diff --git a/Sources/ComposableArchitecture/Store.swift b/Sources/ComposableArchitecture/Store.swift index 051516404..b38d3ddc1 100644 --- a/Sources/ComposableArchitecture/Store.swift +++ b/Sources/ComposableArchitecture/Store.swift @@ -315,10 +315,10 @@ public final class Store { self.threadCheck(status: .scope) #if swift(>=5.7) - return self.reducer.rescope(self, state: toChildState, action: fromChildAction) + return self.reducer.rescope(self, state: toChildState, action: { fromChildAction($1) }) #else return (self.scope ?? StoreScope(root: self)) - .rescope(self, state: toChildState, action: fromChildAction) + .rescope(self, state: toChildState, action: { fromChildAction($1) }) #endif } @@ -334,6 +334,19 @@ public final class Store { self.scope(state: toChildState, action: { $0 }) } + func filter( + _ isSent: @escaping (State, Action) -> Bool + ) -> Store { + self.threadCheck(status: .scope) + + #if swift(>=5.7) + return self.reducer.rescope(self, state: { $0 }, action: { isSent($0, $1) ? $1 : nil }) + #else + return (self.scope ?? StoreScope(root: self)) + .rescope(self, state: { $0 }, action: { isSent($0, $1) ? $1 : nil }) + #endif + } + @_spi(Internals) public func send( _ action: Action, originatingFrom originatingAction: Action? = nil @@ -590,7 +603,7 @@ public typealias StoreOf = Store fileprivate func rescope( _ store: Store, state toChildState: @escaping (State) -> ChildState, - action fromChildAction: @escaping (ChildAction) -> Action + action fromChildAction: @escaping (ChildState, ChildAction) -> Action? ) -> Store { (self as? any AnyScopedReducer ?? ScopedReducer(rootStore: store)) .rescope(store, state: toChildState, action: fromChildAction) @@ -603,7 +616,7 @@ public typealias StoreOf = Store let rootStore: Store let toScopedState: (RootState) -> ScopedState private let parentStores: [Any] - let fromScopedAction: (ScopedAction) -> RootAction + let fromScopedAction: (ScopedState, ScopedAction) -> RootAction? private(set) var isSending = false @inlinable @@ -612,14 +625,14 @@ public typealias StoreOf = Store self.rootStore = rootStore self.toScopedState = { $0 } self.parentStores = [] - self.fromScopedAction = { $0 } + self.fromScopedAction = { $1 } } @inlinable init( rootStore: Store, state toScopedState: @escaping (RootState) -> ScopedState, - action fromScopedAction: @escaping (ScopedAction) -> RootAction, + action fromScopedAction: @escaping (ScopedState, ScopedAction) -> RootAction?, parentStores: [Any] ) { self.rootStore = rootStore @@ -637,7 +650,7 @@ public typealias StoreOf = Store state = self.toScopedState(self.rootStore.state) self.isSending = false } - if let task = self.rootStore.send(self.fromScopedAction(action)) { + if let action = self.fromScopedAction(state, action), let task = self.rootStore.send(action) { return .fireAndForget { await task.cancellableValue } } else { return .none @@ -649,7 +662,7 @@ public typealias StoreOf = Store func rescope( _ store: Store, state toRescopedState: @escaping (ScopedState) -> RescopedState, - action fromRescopedAction: @escaping (RescopedAction) -> ScopedAction + action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction? ) -> Store } @@ -658,13 +671,13 @@ public typealias StoreOf = Store func rescope( _ store: Store, state toRescopedState: @escaping (ScopedState) -> RescopedState, - action fromRescopedAction: @escaping (RescopedAction) -> ScopedAction + action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction? ) -> Store { - let fromScopedAction = self.fromScopedAction as! (ScopedAction) -> RootAction + let fromScopedAction = self.fromScopedAction as! (ScopedState, ScopedAction) -> RootAction? let reducer = ScopedReducer( rootStore: self.rootStore, state: { _ in toRescopedState(store.state) }, - action: { fromScopedAction(fromRescopedAction($0)) }, + action: { fromRescopedAction($0, $1).flatMap { fromScopedAction(store.state, $0) } }, parentStores: self.parentStores + [store] ) let childStore = Store( @@ -685,7 +698,7 @@ public typealias StoreOf = Store func rescope( _ store: Store, state toRescopedState: @escaping (ScopedState) -> RescopedState, - action fromRescopedAction: @escaping (RescopedAction) -> ScopedAction + action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction? ) -> Store } @@ -694,12 +707,15 @@ public typealias StoreOf = Store let fromScopedAction: Any init(root: Store) { - self.init(root: root, fromScopedAction: { $0 }) + self.init( + root: root, + fromScopedAction: { (state: RootState, action: RootAction) -> RootAction? in action } + ) } - private init( + private init( root: Store, - fromScopedAction: @escaping (ScopedAction) -> RootAction + fromScopedAction: @escaping (ScopedState, ScopedAction) -> RootAction? ) { self.root = root self.fromScopedAction = fromScopedAction @@ -708,9 +724,9 @@ public typealias StoreOf = Store func rescope( _ scopedStore: Store, state toRescopedState: @escaping (ScopedState) -> RescopedState, - action fromRescopedAction: @escaping (RescopedAction) -> ScopedAction + action fromRescopedAction: @escaping (RescopedState, RescopedAction) -> ScopedAction? ) -> Store { - let fromScopedAction = self.fromScopedAction as! (ScopedAction) -> RootAction + let fromScopedAction = self.fromScopedAction as! (ScopedState, ScopedAction) -> RootAction? var isSending = false let rescopedStore = Store( @@ -718,7 +734,11 @@ public typealias StoreOf = Store reducer: .init { rescopedState, rescopedAction, _ in isSending = true defer { isSending = false } - let task = self.root.send(fromScopedAction(fromRescopedAction(rescopedAction))) + guard + let scopedAction = fromRescopedAction(rescopedState, rescopedAction), + let rootAction = fromScopedAction(scopedStore.state.value, scopedAction) + else { return .none } + let task = self.root.send(rootAction) rescopedState = toRescopedState(scopedStore.state) if let task = task { return .fireAndForget { await task.cancellableValue } @@ -736,7 +756,9 @@ public typealias StoreOf = Store } rescopedStore.scope = StoreScope( root: self.root, - fromScopedAction: { fromScopedAction(fromRescopedAction($0)) } + fromScopedAction: { + fromRescopedAction($0, $1).flatMap { fromScopedAction(scopedStore.state.value, $0) } + } ) return rescopedStore } diff --git a/Sources/ComposableArchitecture/SwiftUI/Binding.swift b/Sources/ComposableArchitecture/SwiftUI/Binding.swift index 78b1e63b3..d51860245 100644 --- a/Sources/ComposableArchitecture/SwiftUI/Binding.swift +++ b/Sources/ComposableArchitecture/SwiftUI/Binding.swift @@ -171,9 +171,32 @@ public struct BindingAction: Equatable { @usableFromInline let set: (inout Root) -> Void - let value: Any + // NB: swift(<5.8) has an enum existential layout bug that can cause crashes when extracting + // payloads. We can box the existential to work around the bug. + #if swift(<5.8) + private let _value: [Any] + var value: Any { self._value[0] } + #else + let value: Any + #endif let valueIsEqualTo: (Any) -> Bool + init( + keyPath: PartialKeyPath, + set: @escaping (inout Root) -> Void, + value: Any, + valueIsEqualTo: @escaping (Any) -> Bool + ) { + self.keyPath = keyPath + self.set = set + #if swift(<5.8) + self._value = [value] + #else + self.value = value + #endif + self.valueIsEqualTo = valueIsEqualTo + } + public static func == (lhs: Self, rhs: Self) -> Bool { lhs.keyPath == rhs.keyPath && lhs.valueIsEqualTo(rhs.value) } diff --git a/Sources/ComposableArchitecture/SwiftUI/IfLetStore.swift b/Sources/ComposableArchitecture/SwiftUI/IfLetStore.swift index 9cf298246..d7a085e55 100644 --- a/Sources/ComposableArchitecture/SwiftUI/IfLetStore.swift +++ b/Sources/ComposableArchitecture/SwiftUI/IfLetStore.swift @@ -58,10 +58,12 @@ if var state = viewStore.state { return ViewBuilder.buildEither( first: ifContent( - store.scope { - state = $0 ?? state - return state - } + store + .filter { state, _ in state == nil ? !BindingLocal.isActive : true } + .scope { + state = $0 ?? state + return state + } ) ) } else { @@ -85,10 +87,12 @@ self.content = { viewStore in if var state = viewStore.state { return ifContent( - store.scope { - state = $0 ?? state - return state - } + store + .filter { state, _ in state == nil ? !BindingLocal.isActive : true } + .scope { + state = $0 ?? state + return state + } ) } else { return nil diff --git a/Sources/ComposableArchitecture/ViewStore.swift b/Sources/ComposableArchitecture/ViewStore.swift index 50fd6c79b..9e3e06e17 100644 --- a/Sources/ComposableArchitecture/ViewStore.swift +++ b/Sources/ComposableArchitecture/ViewStore.swift @@ -470,6 +470,7 @@ public final class ViewStore { #endif #if canImport(SwiftUI) + /// Derives a binding from the store that prevents direct writes to state and instead sends /// actions to the store. /// @@ -600,7 +601,11 @@ public final class ViewStore { send action: HashableWrapper<(Value) -> ViewAction> ) -> Value { get { state.rawValue(self.state) } - set { self.send(action.rawValue(newValue)) } + set { + BindingLocal.$isActive.withValue(true) { + self.send(action.rawValue(newValue)) + } + } } } @@ -790,3 +795,7 @@ private struct HashableWrapper: Hashable { static func == (lhs: Self, rhs: Self) -> Bool { false } func hash(into hasher: inout Hasher) {} } + +enum BindingLocal { + @TaskLocal static var isActive = false +} diff --git a/Tests/ComposableArchitectureTests/BindingLocalTests.swift b/Tests/ComposableArchitectureTests/BindingLocalTests.swift new file mode 100644 index 000000000..059de1c84 --- /dev/null +++ b/Tests/ComposableArchitectureTests/BindingLocalTests.swift @@ -0,0 +1,40 @@ +#if DEBUG && !os(Linux) + import XCTest + + @testable import ComposableArchitecture + + @MainActor + final class BindingLocalTests: XCTestCase { + public func testBindingLocalIsActive() { + XCTAssertFalse(BindingLocal.isActive) + + struct MyReducer: ReducerProtocol { + struct State: Equatable { + var text = "" + } + + enum Action: Equatable { + case textChanged(String) + } + + func reduce(into state: inout State, action: Action) -> EffectTask { + switch action { + case let .textChanged(text): + state.text = text + return .none + } + } + } + + let store = Store(initialState: MyReducer.State(), reducer: MyReducer()) + let viewStore = ViewStore(store, observe: { $0 }) + + let binding = viewStore.binding(get: \.text) { text in + XCTAssertTrue(BindingLocal.isActive) + return .textChanged(text) + } + binding.wrappedValue = "Hello!" + XCTAssertEqual(viewStore.text, "Hello!") + } + } +#endif diff --git a/Tests/ComposableArchitectureTests/BindingTests.swift b/Tests/ComposableArchitectureTests/BindingTests.swift index f1a290603..7af4c6f4f 100644 --- a/Tests/ComposableArchitectureTests/BindingTests.swift +++ b/Tests/ComposableArchitectureTests/BindingTests.swift @@ -42,5 +42,18 @@ XCTAssertEqual(viewStore.state, .init(nested: .init(field: "Hello!"))) } #endif + + // NB: This crashes in Swift(<5.8) RELEASE when `BindingAction` holds directly onto an unboxed + // `value: Any` existential + func testLayoutBug() { + enum Foo { + case bar(Baz) + } + enum Baz { + case fizz(BindingAction) + case buzz(Bool) + } + _ = (/Foo.bar).extract(from: .bar(.buzz(true))) + } } #endif diff --git a/Tests/ComposableArchitectureTests/EffectCancellationTests.swift b/Tests/ComposableArchitectureTests/EffectCancellationTests.swift index a2c8d536d..f680ffc6c 100644 --- a/Tests/ComposableArchitectureTests/EffectCancellationTests.swift +++ b/Tests/ComposableArchitectureTests/EffectCancellationTests.swift @@ -68,15 +68,17 @@ final class EffectCancellationTests: XCTestCase { func testCancellationAfterDelay() { var value: Int? + let scheduler = QueueScheduler() + Effect(value: 1) - .deferred(for: 0.5, scheduler: QueueScheduler.main) + .deferred(for: 0.5, scheduler: scheduler) .cancellable(id: CancelID()) .producer .startWithValues { value = $0 } XCTAssertEqual(value, nil) - DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) { + scheduler.queue.asyncAfter(deadline: .now() + 0.05) { _ = EffectTask.cancel(id: CancelID()) .producer .start() diff --git a/Tests/ComposableArchitectureTests/RuntimeWarningTests.swift b/Tests/ComposableArchitectureTests/RuntimeWarningTests.swift index 4e69f2912..234367534 100644 --- a/Tests/ComposableArchitectureTests/RuntimeWarningTests.swift +++ b/Tests/ComposableArchitectureTests/RuntimeWarningTests.swift @@ -206,8 +206,6 @@ reducer: EmptyReducer() ) - let viewStore = ViewStore(store) - var line: UInt = 0 XCTExpectFailure { line = #line diff --git a/Tests/ComposableArchitectureTests/StoreFilterTests.swift b/Tests/ComposableArchitectureTests/StoreFilterTests.swift new file mode 100644 index 000000000..a64eb760b --- /dev/null +++ b/Tests/ComposableArchitectureTests/StoreFilterTests.swift @@ -0,0 +1,27 @@ +// `@MainActor` introduces issues gathering tests on Linux +#if DEBUG && !os(Linux) + import ReactiveSwift + import XCTest + + @testable import ComposableArchitecture + + @MainActor + final class StoreFilterTests: XCTestCase { + + func testFilter() { + let store = Store(initialState: nil, reducer: EmptyReducer()) + .filter { state, _ in state != nil } + + let viewStore = ViewStore(store) + var count = 0 + viewStore.produced.producer + .startWithValues { _ in count += 1 } + + XCTAssertEqual(count, 1) + viewStore.send(()) + XCTAssertEqual(count, 1) + viewStore.send(()) + XCTAssertEqual(count, 1) + } + } +#endif diff --git a/Tests/ComposableArchitectureTests/TestStoreNonExhaustiveTests.swift b/Tests/ComposableArchitectureTests/TestStoreNonExhaustiveTests.swift index 97d8059bc..c01c7763a 100644 --- a/Tests/ComposableArchitectureTests/TestStoreNonExhaustiveTests.swift +++ b/Tests/ComposableArchitectureTests/TestStoreNonExhaustiveTests.swift @@ -460,38 +460,40 @@ // Confirms that when you send an action the test store skips any unreceived actions // automatically. func testSendWithUnreceivedActions_SkipsActions() async { - struct Feature: ReducerProtocol { - enum Action: Equatable { - case tap - case response(Int) - } - func reduce(into state: inout Int, action: Action) -> EffectTask { - switch action { - case .tap: - state += 1 - return .task { [state] in .response(state + 42) } - case let .response(number): - state = number - return .none + await _withMainSerialExecutor { + struct Feature: ReducerProtocol { + enum Action: Equatable { + case tap + case response(Int) + } + func reduce(into state: inout Int, action: Action) -> EffectTask { + switch action { + case .tap: + state += 1 + return .task { [state] in .response(state + 42) } + case let .response(number): + state = number + return .none + } } } - } - let store = TestStore( - initialState: 0, - reducer: Feature() - ) - store.exhaustivity = .off + let store = TestStore( + initialState: 0, + reducer: Feature() + ) + store.exhaustivity = .off - await store.send(.tap) - XCTAssertEqual(store.state, 1) + await store.send(.tap) + XCTAssertEqual(store.state, 1) - // Ignored received action: .response(43) - await store.send(.tap) - XCTAssertEqual(store.state, 44) + // Ignored received action: .response(43) + await store.send(.tap) + XCTAssertEqual(store.state, 44) - await store.skipReceivedActions() - XCTAssertEqual(store.state, 86) + await store.skipReceivedActions() + XCTAssertEqual(store.state, 86) + } } func testPartialExhaustivityPrefix() async {