forked from sideeffect-io/AsyncExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncThrowingCurrentValueSubject.swift
155 lines (135 loc) · 4.33 KB
/
AsyncThrowingCurrentValueSubject.swift
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// AsyncThrowingCurrentValueSubject.swift
//
//
// Created by Thibault Wittemberg on 07/01/2022.
//
/// An`AsyncThrowingCurrentValueSubject` is an async sequence in which one can send values over time.
/// The current value is always accessible as an instance variable.
/// The current value is replayed in any new async for in loops.
/// When the `AsyncThrowingCurrentValueSubject` is terminated, new consumers will
/// immediately resume with this termination, whether it is a finish or a failure.
///
/// ```
/// let currentValue = AsyncThrowingCurrentValueSubject<Int, Error>(1)
///
/// Task {
/// for try await element in currentValue {
/// print(element) // will print 1 2 and throw
/// }
/// }
///
/// Task {
/// for try await element in currentValue {
/// print(element) // will print 1 2 and throw
/// }
/// }
///
/// .. later in the application flow
///
/// await currentValue.send(2)
///
/// print(currentValue.element) // will print 2
/// await currentValue.send(.failure(error))
///
/// ```
public final class AsyncThrowingCurrentValueSubject<Element, Failure: Error>: AsyncSubject where Element: Sendable {
public typealias Element = Element
public typealias Failure = Failure
public typealias AsyncIterator = Iterator
struct State {
var terminalState: Termination<Failure>?
var current: Element
var channels: [Int: AsyncThrowingBufferedChannel<Element, Error>]
var ids: Int
}
let state: ManagedCriticalState<State>
public var value: Element {
get {
self.state.criticalState.current
}
set {
self.send(newValue)
}
}
public init(_ element: Element) {
self.state = ManagedCriticalState(
State(terminalState: nil, current: element, channels: [:], ids: 0)
)
}
/// Sends a value to all consumers
/// - Parameter element: the value to send
public func send(_ element: Element) {
self.state.withCriticalRegion { state in
state.current = element
for channel in state.channels.values {
channel.send(element)
}
}
}
/// Finishes the subject with either a normal ending or an error.
/// - Parameter termination: The termination to finish the subject.
public func send(_ termination: Termination<Failure>) {
self.state.withCriticalRegion { state in
state.terminalState = termination
let channels = Array(state.channels.values)
state.channels.removeAll()
for channel in channels {
switch termination {
case .finished:
channel.finish()
case .failure(let error):
channel.fail(error)
}
}
}
}
func handleNewConsumer(
) -> (iterator: AsyncThrowingBufferedChannel<Element, Error>.Iterator, unregister: @Sendable () -> Void) {
let asyncBufferedChannel = AsyncThrowingBufferedChannel<Element, Error>()
let (terminalState, current) = self.state.withCriticalRegion { state -> (Termination?, Element) in
(state.terminalState, state.current)
}
if let terminalState = terminalState {
switch terminalState {
case .finished:
asyncBufferedChannel.finish()
case .failure(let error):
asyncBufferedChannel.fail(error)
}
return (asyncBufferedChannel.makeAsyncIterator(), {})
}
asyncBufferedChannel.send(current)
let consumerId = self.state.withCriticalRegion { state -> Int in
state.ids += 1
state.channels[state.ids] = asyncBufferedChannel
return state.ids
}
let unregister = { @Sendable [state] in
state.withCriticalRegion { state in
state.channels[consumerId] = nil
}
}
return (asyncBufferedChannel.makeAsyncIterator(), unregister)
}
public func makeAsyncIterator() -> AsyncIterator {
Iterator(asyncSubject: self)
}
public struct Iterator: AsyncSubjectIterator {
var iterator: AsyncThrowingBufferedChannel<Element, Error>.Iterator
let unregister: @Sendable () -> Void
init(asyncSubject: AsyncThrowingCurrentValueSubject) {
(self.iterator, self.unregister) = asyncSubject.handleNewConsumer()
}
public var hasBufferedElements: Bool {
self.iterator.hasBufferedElements
}
public mutating func next() async throws -> Element? {
try await withTaskCancellationHandler {
try await self.iterator.next()
} onCancel: { [unregister] in
unregister()
}
}
}
}