Skip to content

Commit 2bbebc0

Browse files
committed
Move shouldReallyTerminate into Signal.init.
1 parent dfcd33a commit 2bbebc0

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

Sources/Signal.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ public final class Signal<Value, Error: Swift.Error> {
101101
// sent multiple times, since `shouldReallyTerminate` would not
102102
// respond to false positives.
103103

104+
/// Return the terminating state if the signal is terminating , or `nil`
105+
/// otherwise.
106+
///
107+
/// Calling this method as a result of a false positive `terminating` check
108+
/// is permitted. `nil` would be returned in this case.
109+
///
110+
/// - note: The `updateLock` would be acquired.
111+
///
112+
/// - returns: The terminating state or `nil`.
113+
@inline(__always)
114+
func shouldReallyTerminate() -> TerminatingState<Value, Error>? {
115+
// Acquire `updateLock`. If the termination has still not yet been
116+
// handled, take it over and bump the status to `terminated`.
117+
signal.updateLock.lock()
118+
119+
if case let .terminating(state) = signal.state {
120+
signal.state = .terminated
121+
signal.updateLock.unlock()
122+
return state
123+
}
124+
125+
signal.updateLock.unlock()
126+
return nil
127+
}
128+
104129
if event.isTerminating {
105130
// Recursive events are disallowed for `value` events, but are permitted
106131
// for termination events. Specifically:
@@ -131,7 +156,7 @@ public final class Signal<Value, Error: Swift.Error> {
131156
if signal.sendLock.try() {
132157
// Check whether the terminating state has been handled by a
133158
// concurrent sender. If not, handle it.
134-
if let terminatingState = signal.shouldReallyTerminate() {
159+
if let terminatingState = shouldReallyTerminate() {
135160
for observer in terminatingState.observers {
136161
observer.action(terminatingState.event)
137162
}
@@ -174,7 +199,7 @@ public final class Signal<Value, Error: Swift.Error> {
174199

175200
// Check if the status has been bumped to `terminating` due to a
176201
// concurrent or a recursive termination event.
177-
if case .terminating = signal.state, let terminatingState = signal.shouldReallyTerminate() {
202+
if case .terminating = signal.state, let terminatingState = shouldReallyTerminate() {
178203
for observer in terminatingState.observers {
179204
observer.action(terminatingState.event)
180205
}
@@ -192,7 +217,7 @@ public final class Signal<Value, Error: Swift.Error> {
192217
if !shouldDispose, case .terminating = signal.state {
193218
signal.sendLock.lock()
194219

195-
if let terminatingState = signal.shouldReallyTerminate() {
220+
if let terminatingState = shouldReallyTerminate() {
196221
for observer in terminatingState.observers {
197222
observer.action(terminatingState.event)
198223
}
@@ -214,31 +239,6 @@ public final class Signal<Value, Error: Swift.Error> {
214239
generatorDisposable = generator(observer)
215240
}
216241

217-
/// Return the state snapshot if the signal is in the `terminating` state, or
218-
/// `nil` otherwise.
219-
///
220-
/// Calling this method as a result of a false positive `terminating` check
221-
/// is permitted. `nil` would be returned in this case.
222-
///
223-
/// - note: The `updateLock` would be acquired.
224-
///
225-
/// - returns:
226-
/// The state snapshot associated with a termination event, or `nil`.
227-
private func shouldReallyTerminate() -> SignalStateSnapshot<Value, Error, Event<Value, Error>>? {
228-
// Acquire `updateLock`. If the termination has still not yet been
229-
// handled, take it over and bump the status to `terminated`.
230-
updateLock.lock()
231-
232-
if case let .terminating(snapshot) = state {
233-
state = .terminated
234-
updateLock.unlock()
235-
return snapshot
236-
}
237-
238-
updateLock.unlock()
239-
return nil
240-
}
241-
242242
/// Swap the generator disposable with `nil`.
243243
///
244244
/// - returns:

0 commit comments

Comments
 (0)