Skip to content

Commit

Permalink
Fix builds, tests, warnings and run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
p4checo committed Jan 31, 2023
1 parent a1eddf5 commit 396ae72
Show file tree
Hide file tree
Showing 10 changed files with 868 additions and 869 deletions.
6 changes: 3 additions & 3 deletions Sources/ComposableArchitecture/Effects/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -348,7 +348,7 @@ extension Effect {
public func fireAndForget<NewValue, NewError>(
outputType: NewValue.Type = NewValue.self,
failureType: NewError.Type = NewError.self
) -> Effect<NewValue, NewError> {
) -> EffectProducer<NewValue, NewError> {
self
.producer
.flatMapError { _ in .empty }
Expand Down
14 changes: 7 additions & 7 deletions Sources/ComposableArchitecture/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,24 +399,24 @@ public final class Store<State, Action> {
}
},
completed: { [weak self] in
self?.threadCheck(status: .effectCompletion(action))
boxedTask.wrappedValue?.cancel()
didComplete = true
self?.threadCheck(status: .effectCompletion(action))
boxedTask.wrappedValue?.cancel()
didComplete = true
self?.effectDisposables.removeValue(forKey: uuid)?.dispose()
},
},
interrupted: { [weak self] in
boxedTask.wrappedValue?.cancel()
didComplete = true
self?.effectDisposables.removeValue(forKey: uuid)?.dispose()
}
}
)

let effectDisposable = CompositeDisposable()
effectDisposable += producer.start(observer)
effectDisposable += AnyDisposable { [weak self] in
self?.threadCheck(status: .effectCompletion(action))
self?.effectDisposables.removeValue(forKey: uuid)?.dispose()
}
}

if !didComplete {
let task = Task<Void, Never> { @MainActor in
Expand Down Expand Up @@ -677,7 +677,7 @@ public typealias StoreOf<R: ReducerProtocol> = Store<R.State, R.Action>
let reducer = ScopedReducer<RootState, RootAction, RescopedState, RescopedAction>(
rootStore: self.rootStore,
state: { _ in toRescopedState(store.state) },
action: { fromRescopedAction($0, $1).flatMap { fromScopedAction(store.state.value, $0) } },
action: { fromRescopedAction($0, $1).flatMap { fromScopedAction(store.state, $0) } },
parentStores: self.parentStores + [store]
)
let childStore = Store<RescopedState, RescopedAction>(
Expand Down
196 changes: 98 additions & 98 deletions Sources/ComposableArchitecture/SwiftUI/IfLetStore.swift
Original file line number Diff line number Diff line change
@@ -1,112 +1,112 @@
#if canImport(SwiftUI)
import SwiftUI
import SwiftUI

/// A view that safely unwraps a store of optional state in order to show one of two views.
///
/// When the underlying state is non-`nil`, the `then` closure will be performed with a ``Store``
/// that holds onto non-optional state, and otherwise the `else` closure will be performed.
///
/// This is useful for deciding between two views to show depending on an optional piece of state:
///
/// ```swift
/// IfLetStore(
/// store.scope(state: \SearchState.results, action: SearchAction.results),
/// ) {
/// SearchResultsView(store: $0)
/// } else: {
/// Text("Loading search results...")
/// }
/// ```
///
/// And for showing a sheet when a piece of state becomes non-`nil`:
///
/// ```swift
/// .sheet(
/// isPresented: viewStore.binding(
/// get: \.isGameActive,
/// send: { $0 ? .startButtonTapped : .detailDismissed }
/// )
/// ) {
/// IfLetStore(
/// self.store.scope(state: \.detail, action: AppAction.detail)
/// ) {
/// DetailView(store: $0)
/// }
/// }
/// ```
///
/// A view that safely unwraps a store of optional state in order to show one of two views.
///
/// When the underlying state is non-`nil`, the `then` closure will be performed with a ``Store``
/// that holds onto non-optional state, and otherwise the `else` closure will be performed.
///
/// This is useful for deciding between two views to show depending on an optional piece of state:
///
/// ```swift
/// IfLetStore(
/// store.scope(state: \SearchState.results, action: SearchAction.results),
/// ) {
/// SearchResultsView(store: $0)
/// } else: {
/// Text("Loading search results...")
/// }
/// ```
///
/// And for showing a sheet when a piece of state becomes non-`nil`:
///
/// ```swift
/// .sheet(
/// isPresented: viewStore.binding(
/// get: \.isGameActive,
/// send: { $0 ? .startButtonTapped : .detailDismissed }
/// )
/// ) {
/// IfLetStore(
/// self.store.scope(state: \.detail, action: AppAction.detail)
/// ) {
/// DetailView(store: $0)
/// }
/// }
/// ```
///
public struct IfLetStore<State, Action, Content>: View where Content: View {
private let content: (ViewStore<State?, Action>) -> Content
private let store: Store<State?, Action>
private let content: (ViewStore<State?, Action>) -> Content
private let store: Store<State?, Action>

/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional
/// state is `nil` or non-`nil`.
///
/// - Parameters:
/// - store: A store of optional state.
/// - ifContent: A function that is given a store of non-optional state and returns a view that
/// is visible only when the optional state is non-`nil`.
/// - elseContent: A view that is only visible when the optional state is `nil`.
public init<IfContent, ElseContent>(
_ store: Store<State?, Action>,
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent,
@ViewBuilder else elseContent: () -> ElseContent
) where Content == _ConditionalContent<IfContent, ElseContent> {
self.store = store
let elseContent = elseContent()
self.content = { viewStore in
if var state = viewStore.state {
return ViewBuilder.buildEither(
first: ifContent(
/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional
/// state is `nil` or non-`nil`.
///
/// - Parameters:
/// - store: A store of optional state.
/// - ifContent: A function that is given a store of non-optional state and returns a view that
/// is visible only when the optional state is non-`nil`.
/// - elseContent: A view that is only visible when the optional state is `nil`.
public init<IfContent, ElseContent>(
_ store: Store<State?, Action>,
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent,
@ViewBuilder else elseContent: () -> ElseContent
) where Content == _ConditionalContent<IfContent, ElseContent> {
self.store = store
let elseContent = elseContent()
self.content = { viewStore in
if var state = viewStore.state {
return ViewBuilder.buildEither(
first: ifContent(
store
.filter { state, _ in state == nil ? !BindingLocal.isActive : true }
.scope {
state = $0 ?? state
return state
}
)
)
} else {
return ViewBuilder.buildEither(second: elseContent)
}
}
}

/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional
/// state is `nil` or non-`nil`.
///
/// - Parameters:
/// - store: A store of optional state.
/// - ifContent: A function that is given a store of non-optional state and returns a view that
/// is visible only when the optional state is non-`nil`.
public init<IfContent>(
_ store: Store<State?, Action>,
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent
) where Content == IfContent? {
self.store = store
self.content = { viewStore in
if var state = viewStore.state {
return ifContent(
store
.filter { state, _ in state == nil ? !BindingLocal.isActive : true }
.scope {
state = $0 ?? state
return state
}
state = $0 ?? state
return state
}
)
)
} else {
return ViewBuilder.buildEither(second: elseContent)
} else {
return nil
}
}
}
}

/// Initializes an ``IfLetStore`` view that computes content depending on if a store of optional
/// state is `nil` or non-`nil`.
///
/// - Parameters:
/// - store: A store of optional state.
/// - ifContent: A function that is given a store of non-optional state and returns a view that
/// is visible only when the optional state is non-`nil`.
public init<IfContent>(
_ store: Store<State?, Action>,
@ViewBuilder then ifContent: @escaping (Store<State, Action>) -> IfContent
) where Content == IfContent? {
self.store = store
self.content = { viewStore in
if var state = viewStore.state {
return ifContent(
store
.filter { state, _ in state == nil ? !BindingLocal.isActive : true }
.scope {
state = $0 ?? state
return state
}
)
} else {
return nil
}
public var body: some View {
WithViewStore(
self.store,
observe: { $0 },
removeDuplicates: { ($0 != nil) == ($1 != nil) },
content: self.content
)
}
}

public var body: some View {
WithViewStore(
self.store,
observe: { $0 },
removeDuplicates: { ($0 != nil) == ($1 != nil) },
content: self.content
)
}
}
#endif
Loading

0 comments on commit 396ae72

Please sign in to comment.