Skip to content

Commit 6a5b8f2

Browse files
stephencelisactions-user
authored andcommitted
Run swift-format
1 parent 2bf1be6 commit 6a5b8f2

File tree

2 files changed

+62
-61
lines changed

2 files changed

+62
-61
lines changed

Sources/ComposableArchitecture/Effects/Cancellation.swift

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,57 +29,58 @@ extension Effect {
2929
public func cancellable(id: AnyHashable, cancelInFlight: Bool = false) -> Effect {
3030
// NB: This check intends to work around bugs over different versions of Combine
3131
#if swift(>=5.3) && !os(macOS)
32-
let effect = Deferred<
33-
Publishers.PrefixUntilOutput<Publishers.HandleEvents<Self>, PassthroughSubject<Void, Never>>
34-
> {
35-
let subject = PassthroughSubject<Void, Never>()
36-
lock.sync { subjects[id, default: []].append(subject) }
37-
let cleanup = {
38-
lock.sync {
39-
subjects[id]?.removeAll(where: { $0 === subject })
40-
if subjects[id]?.isEmpty == true {
41-
subjects[id] = nil
32+
let effect = Deferred<
33+
Publishers.PrefixUntilOutput<Publishers.HandleEvents<Self>, PassthroughSubject<Void, Never>>
34+
> {
35+
let subject = PassthroughSubject<Void, Never>()
36+
lock.sync { subjects[id, default: []].append(subject) }
37+
let cleanup = {
38+
lock.sync {
39+
subjects[id]?.removeAll(where: { $0 === subject })
40+
if subjects[id]?.isEmpty == true {
41+
subjects[id] = nil
42+
}
4243
}
4344
}
45+
return
46+
self
47+
.handleEvents(
48+
receiveCompletion: { _ in cleanup() },
49+
receiveCancel: cleanup
50+
)
51+
.prefix(untilOutputFrom: subject)
4452
}
45-
return self
46-
.handleEvents(
47-
receiveCompletion: { _ in cleanup() },
48-
receiveCancel: cleanup
49-
)
50-
.prefix(untilOutputFrom: subject)
51-
}
52-
.eraseToEffect()
53+
.eraseToEffect()
5354
#else
54-
let effect = Deferred { () -> Publishers.HandleEvents<PassthroughSubject<Output, Failure>> in
55-
cancellablesLock.lock()
56-
defer { cancellablesLock.unlock() }
55+
let effect = Deferred { () -> Publishers.HandleEvents<PassthroughSubject<Output, Failure>> in
56+
cancellablesLock.lock()
57+
defer { cancellablesLock.unlock() }
5758

58-
let subject = PassthroughSubject<Output, Failure>()
59-
let cancellable = self.subscribe(subject)
59+
let subject = PassthroughSubject<Output, Failure>()
60+
let cancellable = self.subscribe(subject)
6061

61-
var cancellationCancellable: AnyCancellable!
62-
cancellationCancellable = AnyCancellable {
63-
cancellablesLock.sync {
64-
subject.send(completion: .finished)
65-
cancellable.cancel()
66-
cancellationCancellables[id]?.remove(cancellationCancellable)
67-
if cancellationCancellables[id]?.isEmpty == .some(true) {
68-
cancellationCancellables[id] = nil
62+
var cancellationCancellable: AnyCancellable!
63+
cancellationCancellable = AnyCancellable {
64+
cancellablesLock.sync {
65+
subject.send(completion: .finished)
66+
cancellable.cancel()
67+
cancellationCancellables[id]?.remove(cancellationCancellable)
68+
if cancellationCancellables[id]?.isEmpty == .some(true) {
69+
cancellationCancellables[id] = nil
70+
}
6971
}
7072
}
71-
}
7273

73-
cancellationCancellables[id, default: []].insert(
74-
cancellationCancellable
75-
)
74+
cancellationCancellables[id, default: []].insert(
75+
cancellationCancellable
76+
)
7677

77-
return subject.handleEvents(
78-
receiveCompletion: { _ in cancellationCancellable.cancel() },
79-
receiveCancel: cancellationCancellable.cancel
80-
)
81-
}
82-
.eraseToEffect()
78+
return subject.handleEvents(
79+
receiveCompletion: { _ in cancellationCancellable.cancel() },
80+
receiveCancel: cancellationCancellable.cancel
81+
)
82+
}
83+
.eraseToEffect()
8384
#endif
8485

8586
return cancelInFlight ? .concatenate(.cancel(id: id), effect) : effect
@@ -92,25 +93,25 @@ extension Effect {
9293
/// identifier.
9394
public static func cancel(id: AnyHashable) -> Effect {
9495
#if swift(>=5.3) && !os(macOS)
95-
return .fireAndForget {
96-
lock.sync {
97-
subjects[id]?.forEach { $0.send(()) }
96+
return .fireAndForget {
97+
lock.sync {
98+
subjects[id]?.forEach { $0.send(()) }
99+
}
98100
}
99-
}
100101
#else
101-
return .fireAndForget {
102-
cancellablesLock.sync {
103-
cancellationCancellables[id]?.forEach { $0.cancel() }
102+
return .fireAndForget {
103+
cancellablesLock.sync {
104+
cancellationCancellables[id]?.forEach { $0.cancel() }
105+
}
104106
}
105-
}
106107
#endif
107108
}
108109
}
109110

110111
#if swift(>=5.3) && !os(macOS)
111-
var subjects: [AnyHashable: [PassthroughSubject<Void, Never>]] = [:]
112-
let lock = NSRecursiveLock()
112+
var subjects: [AnyHashable: [PassthroughSubject<Void, Never>]] = [:]
113+
let lock = NSRecursiveLock()
113114
#else
114-
var cancellationCancellables: [AnyHashable: Set<AnyCancellable>] = [:]
115-
let cancellablesLock = NSRecursiveLock()
115+
var cancellationCancellables: [AnyHashable: Set<AnyCancellable>] = [:]
116+
let cancellablesLock = NSRecursiveLock()
116117
#endif

Tests/ComposableArchitectureTests/EffectCancellationTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ final class EffectCancellationTests: XCTestCase {
117117
.store(in: &self.cancellables)
118118

119119
#if swift(>=5.3) && !os(macOS)
120-
XCTAssertTrue(subjects.isEmpty)
120+
XCTAssertTrue(subjects.isEmpty)
121121
#else
122-
XCTAssertTrue(cancellationCancellables.isEmpty)
122+
XCTAssertTrue(cancellationCancellables.isEmpty)
123123
#endif
124124
}
125125

@@ -137,9 +137,9 @@ final class EffectCancellationTests: XCTestCase {
137137
.store(in: &self.cancellables)
138138

139139
#if swift(>=5.3) && !os(macOS)
140-
XCTAssertTrue(subjects.isEmpty)
140+
XCTAssertTrue(subjects.isEmpty)
141141
#else
142-
XCTAssertTrue(cancellationCancellables.isEmpty)
142+
XCTAssertTrue(cancellationCancellables.isEmpty)
143143
#endif
144144
}
145145

@@ -231,9 +231,9 @@ final class EffectCancellationTests: XCTestCase {
231231
self.wait(for: [expectation], timeout: 999)
232232

233233
#if swift(>=5.3) && !os(macOS)
234-
XCTAssertTrue(subjects.isEmpty)
234+
XCTAssertTrue(subjects.isEmpty)
235235
#else
236-
XCTAssertTrue(cancellationCancellables.isEmpty)
236+
XCTAssertTrue(cancellationCancellables.isEmpty)
237237
#endif
238238
}
239239

@@ -253,9 +253,9 @@ final class EffectCancellationTests: XCTestCase {
253253
cancellables.removeAll()
254254

255255
#if swift(>=5.3) && !os(macOS)
256-
XCTAssertTrue(subjects.isEmpty)
256+
XCTAssertTrue(subjects.isEmpty)
257257
#else
258-
XCTAssertTrue(cancellationCancellables.isEmpty)
258+
XCTAssertTrue(cancellationCancellables.isEmpty)
259259
#endif
260260
}
261261

0 commit comments

Comments
 (0)