diff --git a/RxSwift/Traits/PrimitiveSequence/Completable.swift b/RxSwift/Traits/PrimitiveSequence/Completable.swift index f0a8cbbd..ff8fae21 100644 --- a/RxSwift/Traits/PrimitiveSequence/Completable.swift +++ b/RxSwift/Traits/PrimitiveSequence/Completable.swift @@ -71,6 +71,40 @@ extension PrimitiveSequenceType where Trait == CompletableTrait, Element == Swif } } + /** + Subscribes a completion handler and an error handler for this sequence. + + Also, take in an object and provide an unretained, safe to use (i.e. not implicitly unwrapped), reference to it along with the events emitted by the sequence. + + - Note: If `object` can't be retained, none of the other closures will be invoked. + + - parameter object: The object to provide an unretained reference on. + - parameter onCompleted: Action to invoke upon graceful termination of the observable sequence. + - parameter onError: Action to invoke upon errored termination of the observable sequence. + - parameter onDisposed: Action to invoke upon any type of termination of sequence (if the sequence has + gracefully completed, errored, or if the generation is canceled by disposing subscription). + - returns: Subscription object used to unsubscribe from the observable sequence. + */ + public func subscribe( + with object: Object, + onCompleted: ((Object) -> Void)? = nil, + onError: ((Object, Swift.Error) -> Void)? = nil, + onDisposed: ((Object) -> Void)? = nil + ) -> Disposable { + subscribe( + onCompleted: { [weak object] in + guard let object = object else { return } + onCompleted?(object) + }, onError: { [weak object] in + guard let object = object else { return } + onError?(object, $0) + }, onDisposed: { [weak object] in + guard let object = object else { return } + onDisposed?(object) + } + ) + } + /** Subscribes a completion handler and an error handler for this sequence. diff --git a/RxSwift/Traits/PrimitiveSequence/Maybe.swift b/RxSwift/Traits/PrimitiveSequence/Maybe.swift index 376c0c03..3692eb16 100644 --- a/RxSwift/Traits/PrimitiveSequence/Maybe.swift +++ b/RxSwift/Traits/PrimitiveSequence/Maybe.swift @@ -77,6 +77,48 @@ extension PrimitiveSequenceType where Trait == MaybeTrait { } } + /** + Subscribes a success handler, an error handler, and a completion handler for this sequence. + + Also, take in an object and provide an unretained, safe to use (i.e. not implicitly unwrapped), reference to it along with the events emitted by the sequence. + + - Note: If `object` can't be retained, none of the other closures will be invoked. + + - parameter object: The object to provide an unretained reference on. + - parameter onSuccess: Action to invoke for each element in the observable sequence. + - parameter onError: Action to invoke upon errored termination of the observable sequence. + - parameter onCompleted: Action to invoke upon graceful termination of the observable sequence. + - parameter onDisposed: Action to invoke upon any type of termination of sequence (if the sequence has + gracefully completed, errored, or if the generation is canceled by disposing subscription). + - returns: Subscription object used to unsubscribe from the observable sequence. + */ + public func subscribe( + with object: Object, + onSuccess: ((Object, Element) -> Void)? = nil, + onError: ((Object, Swift.Error) -> Void)? = nil, + onCompleted: ((Object) -> Void)? = nil, + onDisposed: ((Object) -> Void)? = nil + ) -> Disposable { + subscribe( + onSuccess: { [weak object] in + guard let object = object else { return } + onSuccess?(object, $0) + }, + onError: { [weak object] in + guard let object = object else { return } + onError?(object, $0) + }, + onCompleted: { [weak object] in + guard let object = object else { return } + onCompleted?(object) + }, + onDisposed: { [weak object] in + guard let object = object else { return } + onDisposed?(object) + } + ) + } + /** Subscribes a success handler, an error handler, and a completion handler for this sequence. diff --git a/RxSwift/Traits/PrimitiveSequence/Single.swift b/RxSwift/Traits/PrimitiveSequence/Single.swift index 434ed730..ffb8aa91 100644 --- a/RxSwift/Traits/PrimitiveSequence/Single.swift +++ b/RxSwift/Traits/PrimitiveSequence/Single.swift @@ -81,6 +81,42 @@ extension PrimitiveSequenceType where Trait == SingleTrait { subscribe(onSuccess: onSuccess, onFailure: onError, onDisposed: onDisposed) } + /** + Subscribes a success handler, and an error handler for this sequence. + + Also, take in an object and provide an unretained, safe to use (i.e. not implicitly unwrapped), reference to it along with the events emitted by the sequence. + + - Note: If `object` can't be retained, none of the other closures will be invoked. + + - parameter object: The object to provide an unretained reference on. + - parameter onSuccess: Action to invoke for each element in the observable sequence. + - parameter onFailure: Action to invoke upon errored termination of the observable sequence. + - parameter onDisposed: Action to invoke upon any type of termination of sequence (if the sequence has + gracefully completed, errored, or if the generation is canceled by disposing subscription). + - returns: Subscription object used to unsubscribe from the observable sequence. + */ + public func subscribe( + with object: Object, + onSuccess: ((Object, Element) -> Void)? = nil, + onFailure: ((Object, Swift.Error) -> Void)? = nil, + onDisposed: ((Object) -> Void)? = nil + ) -> Disposable { + subscribe( + onSuccess: { [weak object] in + guard let object = object else { return } + onSuccess?(object, $0) + }, + onFailure: { [weak object] in + guard let object = object else { return } + onFailure?(object, $0) + }, + onDisposed: { [weak object] in + guard let object = object else { return } + onDisposed?(object) + } + ) + } + /** Subscribes a success handler, and an error handler for this sequence.