@@ -40,68 +40,56 @@ extension UnsafeAtomicState where State == DisposableState {
4040
4141/// A type-erased disposable that forwards operations to an underlying disposable.
4242public final class AnyDisposable : Disposable {
43- private let disposable : Disposable
43+ private final class ActionDisposable : Disposable {
44+ let state : UnsafeAtomicState < DisposableState >
45+ var action : ( ( ) -> Void ) ?
4446
45- public var isDisposed : Bool {
46- return disposable . isDisposed
47- }
47+ var isDisposed : Bool {
48+ return state . is ( . disposed )
49+ }
4850
49- public init ( _ disposable: Disposable ) {
50- self . disposable = disposable
51- }
51+ init ( _ action: ( ( ) -> Void ) ? ) {
52+ self . state = UnsafeAtomicState ( . active)
53+ self . action = action
54+ }
5255
53- public func dispose( ) {
54- disposable. dispose ( )
56+ func dispose( ) {
57+ if state. tryDispose ( ) {
58+ action ? ( )
59+ action = nil
60+ }
61+ }
5562 }
56- }
5763
58- /// A disposable that only flips `isDisposed` upon disposal, and performs no other
59- /// work.
60- public final class SimpleDisposable : Disposable {
61- private var state = UnsafeAtomicState ( DisposableState . active)
64+ private let base : Disposable
6265
6366 public var isDisposed : Bool {
64- return state . is ( . disposed )
67+ return base . isDisposed
6568 }
6669
67- public init ( ) { }
68-
69- public func dispose( ) {
70- _ = state. tryDispose ( )
71- }
72-
73- deinit {
74- state. deinitialize ( )
70+ /// Create a disposable which runs the given action upon disposal.
71+ ///
72+ /// - parameters:
73+ /// - action: A closure to run when calling `dispose()`.
74+ public init ( _ action: @escaping ( ) -> Void ) {
75+ base = ActionDisposable ( action)
7576 }
76- }
77-
78- /// A disposable that will run an action upon disposal.
79- public final class ActionDisposable : Disposable {
80- private var action : ( ( ) -> Void ) ?
81- private var state : UnsafeAtomicState < DisposableState >
8277
83- public var isDisposed : Bool {
84- return state. is ( . disposed)
78+ /// Create a disposable.
79+ public init ( ) {
80+ base = ActionDisposable ( nil )
8581 }
8682
87- /// Initialize the disposable to run the given action upon disposal .
83+ /// Create a disposable which wraps the given disposable .
8884 ///
8985 /// - parameters:
90- /// - action: A closure to run when calling `dispose()`.
91- public init ( action: @escaping ( ) -> Void ) {
92- self . action = action
93- self . state = UnsafeAtomicState ( DisposableState . active)
86+ /// - disposable: The disposable to be wrapped.
87+ public init ( _ disposable: Disposable ) {
88+ base = disposable
9489 }
9590
9691 public func dispose( ) {
97- if state. tryDispose ( ) {
98- action ? ( )
99- action = nil
100- }
101- }
102-
103- deinit {
104- state. deinitialize ( )
92+ base. dispose ( )
10593 }
10694}
10795
@@ -179,7 +167,7 @@ public final class CompositeDisposable: Disposable {
179167 guard disposables != nil else { return nil }
180168
181169 let token = disposables!. insert ( d)
182- return ActionDisposable { [ weak self] in
170+ return AnyDisposable { [ weak self] in
183171 self ? . disposables. modify {
184172 $0? . remove ( using: token)
185173 }
@@ -197,12 +185,51 @@ public final class CompositeDisposable: Disposable {
197185 /// `disposable` is `nil`.
198186 @discardableResult
199187 public func add( _ action: @escaping ( ) -> Void ) -> Disposable ? {
200- return add ( ActionDisposable ( action : action) )
188+ return add ( AnyDisposable ( action) )
201189 }
202190
203191 deinit {
204192 state. deinitialize ( )
205193 }
194+
195+ /// Adds the right-hand-side disposable to the left-hand-side
196+ /// `CompositeDisposable`.
197+ ///
198+ /// ````
199+ /// disposable += producer
200+ /// .filter { ... }
201+ /// .map { ... }
202+ /// .start(observer)
203+ /// ````
204+ ///
205+ /// - parameters:
206+ /// - lhs: Disposable to add to.
207+ /// - rhs: Disposable to add.
208+ ///
209+ /// - returns: An instance of `DisposableHandle` that can be used to opaquely
210+ /// remove the disposable later (if desired).
211+ @discardableResult
212+ public static func += ( lhs: CompositeDisposable , rhs: Disposable ? ) -> Disposable ? {
213+ return lhs. add ( rhs)
214+ }
215+
216+ /// Adds the right-hand-side `ActionDisposable` to the left-hand-side
217+ /// `CompositeDisposable`.
218+ ///
219+ /// ````
220+ /// disposable += { ... }
221+ /// ````
222+ ///
223+ /// - parameters:
224+ /// - lhs: Disposable to add to.
225+ /// - rhs: Closure to add as a disposable.
226+ ///
227+ /// - returns: An instance of `DisposableHandle` that can be used to opaquely
228+ /// remove the disposable later (if desired).
229+ @discardableResult
230+ public static func += ( lhs: CompositeDisposable , rhs: @escaping ( ) -> ( ) ) -> Disposable ? {
231+ return lhs. add ( rhs)
232+ }
206233}
207234
208235/// A disposable that, upon deinitialization, will automatically dispose of
@@ -246,6 +273,44 @@ extension ScopedDisposable where Inner == AnyDisposable {
246273 }
247274}
248275
276+ extension ScopedDisposable where Inner == CompositeDisposable {
277+ /// Adds the right-hand-side disposable to the left-hand-side
278+ /// `ScopedDisposable<CompositeDisposable>`.
279+ ///
280+ /// ````
281+ /// disposable += { ... }
282+ /// ````
283+ ///
284+ /// - parameters:
285+ /// - lhs: Disposable to add to.
286+ /// - rhs: Disposable to add.
287+ ///
288+ /// - returns: An instance of `DisposableHandle` that can be used to opaquely
289+ /// remove the disposable later (if desired).
290+ @discardableResult
291+ public static func += ( lhs: ScopedDisposable < CompositeDisposable > , rhs: Disposable ? ) -> Disposable ? {
292+ return lhs. inner. add ( rhs)
293+ }
294+
295+ /// Adds the right-hand-side disposable to the left-hand-side
296+ /// `ScopedDisposable<CompositeDisposable>`.
297+ ///
298+ /// ````
299+ /// disposable += { ... }
300+ /// ````
301+ ///
302+ /// - parameters:
303+ /// - lhs: Disposable to add to.
304+ /// - rhs: Closure to add as a disposable.
305+ ///
306+ /// - returns: An instance of `DisposableHandle` that can be used to opaquely
307+ /// remove the disposable later (if desired).
308+ @discardableResult
309+ public static func += ( lhs: ScopedDisposable < CompositeDisposable > , rhs: @escaping ( ) -> ( ) ) -> Disposable ? {
310+ return lhs. inner. add ( rhs)
311+ }
312+ }
313+
249314/// A disposable that disposes of its wrapped disposable, and allows its
250315/// wrapped disposable to be replaced.
251316public final class SerialDisposable : Disposable {
@@ -293,78 +358,3 @@ public final class SerialDisposable: Disposable {
293358 state. deinitialize ( )
294359 }
295360}
296-
297- /// Adds the right-hand-side disposable to the left-hand-side
298- /// `CompositeDisposable`.
299- ///
300- /// ````
301- /// disposable += producer
302- /// .filter { ... }
303- /// .map { ... }
304- /// .start(observer)
305- /// ````
306- ///
307- /// - parameters:
308- /// - lhs: Disposable to add to.
309- /// - rhs: Disposable to add.
310- ///
311- /// - returns: An instance of `DisposableHandle` that can be used to opaquely
312- /// remove the disposable later (if desired).
313- @discardableResult
314- public func += ( lhs: CompositeDisposable , rhs: Disposable ? ) -> Disposable ? {
315- return lhs. add ( rhs)
316- }
317-
318- /// Adds the right-hand-side `ActionDisposable` to the left-hand-side
319- /// `CompositeDisposable`.
320- ///
321- /// ````
322- /// disposable += { ... }
323- /// ````
324- ///
325- /// - parameters:
326- /// - lhs: Disposable to add to.
327- /// - rhs: Closure to add as a disposable.
328- ///
329- /// - returns: An instance of `DisposableHandle` that can be used to opaquely
330- /// remove the disposable later (if desired).
331- @discardableResult
332- public func += ( lhs: CompositeDisposable , rhs: @escaping ( ) -> ( ) ) -> Disposable ? {
333- return lhs. add ( rhs)
334- }
335-
336- /// Adds the right-hand-side disposable to the left-hand-side
337- /// `ScopedDisposable<CompositeDisposable>`.
338- ///
339- /// ````
340- /// disposable += { ... }
341- /// ````
342- ///
343- /// - parameters:
344- /// - lhs: Disposable to add to.
345- /// - rhs: Disposable to add.
346- ///
347- /// - returns: An instance of `DisposableHandle` that can be used to opaquely
348- /// remove the disposable later (if desired).
349- @discardableResult
350- public func += ( lhs: ScopedDisposable < CompositeDisposable > , rhs: Disposable ? ) -> Disposable ? {
351- return lhs. inner. add ( rhs)
352- }
353-
354- /// Adds the right-hand-side disposable to the left-hand-side
355- /// `ScopedDisposable<CompositeDisposable>`.
356- ///
357- /// ````
358- /// disposable += { ... }
359- /// ````
360- ///
361- /// - parameters:
362- /// - lhs: Disposable to add to.
363- /// - rhs: Closure to add as a disposable.
364- ///
365- /// - returns: An instance of `DisposableHandle` that can be used to opaquely
366- /// remove the disposable later (if desired).
367- @discardableResult
368- public func += ( lhs: ScopedDisposable < CompositeDisposable > , rhs: @escaping ( ) -> ( ) ) -> Disposable ? {
369- return lhs. inner. add ( rhs)
370- }
0 commit comments