@@ -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
0 commit comments