-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathBroadcastStateMachine.swift
241 lines (211 loc) · 8.32 KB
/
BroadcastStateMachine.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
struct BroadcastStateMachine<Base: AsyncSequence>: Sendable
where Base: Sendable, Base.Element: Sendable {
typealias Channel = UnicastStateMachine<Base.Element>
enum State {
case initial(base: Base, channels: [Int: Channel])
case broadcasting(
task: Task<Void, Never>,
suspendedProducer: UnsafeContinuation<Void, Never>?,
channels: [Int: Channel],
isBusy: Bool,
demands: Set<Int>
)
case finished(channels: [Int: Channel])
}
private var state: State
init(base: Base) {
self.state = .initial(base: base, channels: [:])
}
func task() -> Task<Void, Never>? {
switch self.state {
case .broadcasting(let task, _, _, _, _):
return task
default:
return nil
}
}
mutating func taskIsStarted(
id: Int,
task: Task<Void, Never>,
continuation: UnsafeContinuation<Result<Base.Element?, Error>, Never>
) -> Channel.NextIsSuspendedAction {
switch self.state {
case .initial(_, var channels):
precondition(channels[id] != nil, "Invalid state.")
var channel = channels[id]!
let action = channel.nextIsSuspended(continuation: continuation)
channels[id] = channel
self.state = .broadcasting(task: task, suspendedProducer: nil, channels: channels, isBusy: true, demands: [id])
return action
case .broadcasting:
preconditionFailure("Invalid state.")
case .finished:
preconditionFailure("Invalid state.")
}
}
enum ProducerIsSuspendedAction {
case resume
case suspend
}
mutating func producerIsSuspended(
continuation: UnsafeContinuation<Void, Never>
) -> ProducerIsSuspendedAction {
switch self.state {
case .initial:
preconditionFailure("Invalid state.")
case .broadcasting(let task, let suspendedProducer, let channels, _, var demands):
precondition(suspendedProducer == nil, "Invalid state.")
if !demands.isEmpty {
demands.removeAll()
self.state = .broadcasting(task: task, suspendedProducer: continuation, channels: channels, isBusy: true, demands: demands)
return .resume
}
self.state = .broadcasting(task: task, suspendedProducer: continuation, channels: channels, isBusy: false, demands: demands)
return .suspend
case .finished:
preconditionFailure("Invalid state.")
}
}
mutating func element(element: Base.Element) -> [Channel.SendAction] {
switch state {
case .initial:
preconditionFailure("Invalid state.")
case .broadcasting(let task, _, var channels, _, let demands):
var actions = [Channel.SendAction]()
for entry in channels {
let id = entry.key
var channel = entry.value
actions.append(channel.send(element))
channels[id] = channel
}
self.state = .broadcasting(task: task, suspendedProducer: nil, channels: channels, isBusy: false, demands: demands)
return actions
case .finished:
preconditionFailure("Invalid state.")
}
}
mutating func finish(error: Error? = nil) -> [Channel.FinishAction] {
switch state {
case .initial:
preconditionFailure("Invalid state.")
case .broadcasting(_, _, var channels, _, _):
var actions = [Channel.FinishAction]()
for entry in channels {
let id = entry.key
var channel = entry.value
actions.append(channel.finish(error: error))
channels[id] = channel
}
self.state = .finished(channels: channels)
return actions
case .finished:
preconditionFailure("Invalid state.")
}
}
mutating func next(id: Int) -> Channel.NextAction {
switch self.state {
case .initial(let base, var channels):
var channel = Channel()
let action = channel.next()
channels[id] = channel
self.state = .initial(base: base, channels: channels)
return action
case .broadcasting(let task, let suspendedProducer, var channels, let isBusy, let demands):
if var channel = channels[id] {
let action = channel.next()
channels[id] = channel
self.state = .broadcasting(task: task, suspendedProducer: suspendedProducer, channels: channels, isBusy: isBusy, demands: demands)
return action
}
var channel = Channel()
let action = channel.next()
channels[id] = channel
self.state = .broadcasting(task: task, suspendedProducer: suspendedProducer, channels: channels, isBusy: isBusy, demands: demands)
return action
case .finished(var channels):
if var channel = channels[id] {
let action = channel.next()
channels[id] = channel
self.state = .finished(channels: channels)
return action
}
var channel = Channel()
let action = channel.next()
channels[id] = channel
self.state = .finished(channels: channels)
return action
}
}
enum NextIsSuspendedAction {
case nextIsSuspendedAction(action: Channel.NextIsSuspendedAction)
case resumeProducerAndNextIsSuspendedAction(continuation: UnsafeContinuation<Void, Never>?, action: Channel.NextIsSuspendedAction)
case startTask(base: Base)
}
mutating func nextIsSuspended(
id: Int,
continuation: UnsafeContinuation<Result<Base.Element?, Error>, Never>
) -> NextIsSuspendedAction {
switch self.state {
case .initial(let base, _):
return .startTask(base: base)
case .broadcasting(let task, let suspendedProducer, var channels, let isBusy, var demands):
guard channels[id] != nil else { return .nextIsSuspendedAction(action: .resume(element: .success(nil))) }
if isBusy {
demands.update(with: id)
var channel = channels[id]!
let action = channel.nextIsSuspended(continuation: continuation)
channels[id] = channel
self.state = .broadcasting(task: task, suspendedProducer: suspendedProducer, channels: channels, isBusy: isBusy, demands: demands)
return .nextIsSuspendedAction(action: action)
}
demands.removeAll()
var channel = channels[id]!
let action = channel.nextIsSuspended(continuation: continuation)
channels[id] = channel
self.state = .broadcasting(task: task, suspendedProducer: suspendedProducer, channels: channels, isBusy: true, demands: demands)
return .resumeProducerAndNextIsSuspendedAction(continuation: suspendedProducer, action: action)
case .finished(var channels):
precondition(channels[id] != nil, "Invalid state.")
var channel = channels[id]!
let action = channel.nextIsSuspended(continuation: continuation)
channels[id] = channel
self.state = .finished(channels: channels)
return .nextIsSuspendedAction(action: action)
}
}
enum NextIsCancelledAction {
case nextIsCancelledAction(continuation: UnsafeContinuation<Result<Base.Element?, Error>, Never>?)
}
mutating func nextIsCancelled(
id: Int
) -> NextIsCancelledAction {
switch self.state {
case .initial:
preconditionFailure("Invalid state.")
case .broadcasting(let task, let suspendedProducer, var channels, let isBusy, var demands):
precondition(channels[id] != nil, "Invalid state.")
var channel = channels[id]!
demands.remove(id)
let continuation = channel.nextIsCancelled()
channels[id] = nil
self.state = .broadcasting(task: task, suspendedProducer: suspendedProducer, channels: channels, isBusy: isBusy, demands: demands)
return .nextIsCancelledAction(continuation: continuation)
case .finished(var channels):
var channel = channels[id]!
let continuation = channel.nextIsCancelled()
channels[id] = nil
self.state = .finished(channels: channels)
return .nextIsCancelledAction(continuation: continuation)
}
}
}