This repository was archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAutomaton.swift
165 lines (136 loc) · 5.72 KB
/
Automaton.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
import ReactiveSwift
/// Deterministic finite state machine that receives "input"
/// and with "current state" transform to "next state" & "output (additional effect)".
public final class Automaton<State, Input>
{
/// Basic state-transition function type.
public typealias Mapping = (State, Input) -> State?
/// Transducer (input & output) mapping with `Effect<Input>` (additional effect) as output,
/// which may emit next input values for continuous state-transitions.
public typealias EffectMapping<Queue> = (State, Input) -> (State, Effect<Input, State, Queue>?)?
where Queue: EffectQueueProtocol
/// `Reply` signal that notifies either `.success` or `.failure` of state-transition on every input.
public let replies: Signal<Reply<State, Input>, Never>
/// Current state.
public let state: Property<State>
fileprivate let _repliesObserver: Signal<Reply<State, Input>, Never>.Observer
fileprivate let _disposable: Disposable
/// Initializer using `Mapping`.
///
/// - Parameters:
/// - state: Initial state.
/// - input: `Signal<Input, Never>` that automaton receives.
/// - mapping: Simple `Mapping` that designates next state only (no additional effect).
public convenience init(
state initialState: State,
inputs inputSignal: Signal<Input, Never>,
mapping: @escaping Mapping
)
{
self.init(
state: initialState,
inputs: inputSignal,
mapping: { mapping($0, $1).map { ($0, Effect<Input, State, Never>?.none) } }
)
}
/// Initializer using `EffectMapping`.
///
/// - Parameters:
/// - state: Initial state.
/// - effect: Initial effect.
/// - input: `Signal<Input, Never>` that automaton receives.
/// - mapping: `EffectMapping` that designates next state and also generates additional effect.
public convenience init<Queue>(
state initialState: State,
effect initialEffect: Effect<Input, State, Queue>? = nil,
inputs inputSignal: Signal<Input, Never>,
mapping: @escaping EffectMapping<Queue>
) where Queue: EffectQueueProtocol
{
self.init(
state: initialState,
inputs: inputSignal,
makeSignals: { from -> MakeSignals in
let mapped = from
.map { input, fromState in
return (input, fromState, mapping(fromState, input))
}
let replies = mapped
.map { input, fromState, mapped -> Reply<State, Input> in
if let (toState, _) = mapped {
return .success((input, fromState, toState))
}
else {
return .failure((input, fromState))
}
}
var effects = mapped
.filterMap { _, _, mapped -> Effect<Input, State, Queue>? in
guard case let .some(_, effect) = mapped else { return nil }
return effect
}
.producer
if let initialEffect = initialEffect {
effects = effects.prefix(value: initialEffect)
}
let effectInputs = SignalProducer.merge(
EffectQueue<Queue>.allCases.map { queue in
effects
.filter { $0.queue == queue }
.flatMap(queue.flattenStrategy) { effect -> SignalProducer<Input, Never> in
/// - Note: Cancellation will be triggered regardless of state-transition success or failure.
let until = from.filterMap { effect.until($0, $1) ? () : nil }
return effect.producer.take(until: until)
}
}
)
return (replies, effectInputs)
}
)
}
internal init(
state initialState: State,
inputs inputSignal: Signal<Input, Never>,
makeSignals: (Signal<(Input, State), Never>) -> MakeSignals
)
{
let stateProperty = MutableProperty(initialState)
self.state = Property(capturing: stateProperty)
(self.replies, self._repliesObserver) = Signal<Reply<State, Input>, Never>.pipe()
let effectInputs = Signal<Input, Never>.pipe()
let mergedInputs = Signal.merge(inputSignal, effectInputs.output)
let mapped = mergedInputs
.withLatest(from: stateProperty.producer)
let (replies, effects) = makeSignals(mapped)
let d = CompositeDisposable()
d += stateProperty <~ replies.filterMap { $0.toState }
d += replies.observeValues(self._repliesObserver.send(value:))
let effectDisposable = effects.start(effectInputs.input)
d += effectDisposable
d += inputSignal
.observeCompleted { [_repliesObserver] in
effectDisposable.dispose()
_repliesObserver.sendCompleted()
effectInputs.input.sendCompleted()
}
d += inputSignal
.observeInterrupted { [_repliesObserver] in
effectDisposable.dispose()
_repliesObserver.sendInterrupted()
effectInputs.input.sendInterrupted()
}
self._disposable = d
}
deinit
{
self._repliesObserver.sendCompleted()
self._disposable.dispose()
}
}
extension Automaton
{
internal typealias MakeSignals = (
replies: Signal<Reply<State, Input>, Never>,
effects: SignalProducer<Input, Never>
)
}