Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/ComposableArchitecture/Internal/Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import Combine
import Darwin

private class DemandBuffer<S: Subscriber> {
final class DemandBuffer<S: Subscriber> {
private var buffer = [S.Input]()
private let subscriber: S
private var completion: Subscribers.Completion<S.Failure>?
Expand Down
58 changes: 58 additions & 0 deletions Sources/ComposableArchitecture/Internal/CurrentValueRelay.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Combine
import Foundation

class CurrentValueRelay<Output>: Publisher {
typealias Failure = Never

private var currentValue: Output
private var subscriptions: [Subscription<AnySubscriber<Output, Failure>>] = []

var value: Output {
get { self.currentValue }
set { self.send(newValue) }
}

init(_ value: Output) {
self.currentValue = value
}

func receive<S>(subscriber: S)
where S: Subscriber, Never == S.Failure, Output == S.Input
{
let subscription = Subscription(downstream: AnySubscriber(subscriber))
self.subscriptions.append(subscription)
subscriber.receive(subscription: subscription)
subscription.forwardValueToBuffer(self.currentValue)
}

func send(_ value: Output) {
self.currentValue = value
for subscription in subscriptions {
subscription.forwardValueToBuffer(value)
}
}
}

extension CurrentValueRelay {
class Subscription<Downstream: Subscriber>: Combine.Subscription
where Output == Downstream.Input, Failure == Downstream.Failure
{
private var demandBuffer: DemandBuffer<Downstream>?

init(downstream: Downstream) {
self.demandBuffer = DemandBuffer(subscriber: downstream)
}

func forwardValueToBuffer(_ value: Output) {
_ = demandBuffer?.buffer(value: value)
}

func request(_ demand: Subscribers.Demand) {
_ = demandBuffer?.demand(demand)
}

func cancel() {
demandBuffer = nil
}
}
}
4 changes: 2 additions & 2 deletions Sources/ComposableArchitecture/ViewStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class ViewStore<State, Action>: ObservableObject {
public private(set) lazy var objectWillChange = ObservableObjectPublisher()

private let _send: (Action) -> Void
fileprivate let _state: CurrentValueSubject<State, Never>
fileprivate let _state: CurrentValueRelay<State>
private var viewCancellable: AnyCancellable?

/// Initializes a view store from a store.
Expand All @@ -72,7 +72,7 @@ public final class ViewStore<State, Action>: ObservableObject {
removeDuplicates isDuplicate: @escaping (State, State) -> Bool
) {
self._send = { store.send($0) }
self._state = CurrentValueSubject(store.state.value)
self._state = CurrentValueRelay(store.state.value)

self.viewCancellable = store.state
.removeDuplicates(by: isDuplicate)
Expand Down
31 changes: 31 additions & 0 deletions Tests/ComposableArchitectureTests/ViewStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ final class ViewStoreTests: XCTestCase {
XCTAssertNoDifference(results, [0, 1])
}

func testStorePublisherSubscriptionOrder() {
let reducer = Reducer<Int, Void, Void> { count, _, _ in
count += 1
return .none
}
let store = Store(initialState: 0, reducer: reducer, environment: ())
let viewStore = ViewStore(store)

var results: [Int] = []

viewStore.publisher
.sink { _ in results.append(0) }
.store(in: &self.cancellables)

viewStore.publisher
.sink { _ in results.append(1) }
.store(in: &self.cancellables)

viewStore.publisher
.sink { _ in results.append(2) }
.store(in: &self.cancellables)

XCTAssertNoDifference(results, [0, 1, 2])

for _ in 0 ..< 9 {
viewStore.send(())
}

XCTAssertNoDifference(results, Array(repeating: [0, 1, 2], count: 10).flatMap { $0 })
}

#if compiler(>=5.5) && canImport(_Concurrency)
func testSendWhile() {
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else { return }
Expand Down