Skip to content

Commit

Permalink
subscribe with object for Driver/Signal
Browse files Browse the repository at this point in the history
  • Loading branch information
freak4pc committed Feb 10, 2021
1 parent 67f74cb commit 9bc9726
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
34 changes: 33 additions & 1 deletion RxCocoa/Traits/Driver/Driver+Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingSt
return with(self.asObservable())(curriedArgument)
}

/**
Subscribes an element handler, a completion handler and disposed handler to an observable sequence.
This method can be only called from `MainThread`.
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.
Error callback is not exposed because `Driver` can't error out.
- 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 onNext: Action to invoke for each element in the observable sequence.
- parameter onCompleted: Action to invoke upon graceful termination of the observable sequence.
gracefully completed, errored, or if the generation is canceled by disposing subscription)
- 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 drive<Object: AnyObject>(
with object: Object,
onNext: ((Object, Element) -> Void)? = nil,
onCompleted: ((Object) -> Void)? = nil,
onDisposed: ((Object) -> Void)? = nil
) -> Disposable {
MainScheduler.ensureRunningOnMainThread(errorMessage: errorMessage)
return self.asObservable().subscribe(with: object, onNext: onNext, onCompleted: onCompleted, onDisposed: onDisposed)
}

/**
Subscribes an element handler, a completion handler and disposed handler to an observable sequence.
This method can be only called from `MainThread`.
Expand All @@ -150,7 +178,11 @@ extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingSt
gracefully completed, errored, or if the generation is canceled by disposing subscription)
- returns: Subscription object used to unsubscribe from the observable sequence.
*/
public func drive(onNext: ((Element) -> Void)? = nil, onCompleted: (() -> Void)? = nil, onDisposed: (() -> Void)? = nil) -> Disposable {
public func drive(
onNext: ((Element) -> Void)? = nil,
onCompleted: (() -> Void)? = nil,
onDisposed: (() -> Void)? = nil
) -> Disposable {
MainScheduler.ensureRunningOnMainThread(errorMessage: errorMessage)
return self.asObservable().subscribe(onNext: onNext, onCompleted: onCompleted, onDisposed: onDisposed)
}
Expand Down
39 changes: 37 additions & 2 deletions RxCocoa/Traits/Signal/Signal+Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,55 @@ extension SharedSequenceConvertibleType where SharingStrategy == SignalSharingSt
relays.forEach { $0.accept(e) }
})
}

/**
Subscribes an element handler, a completion handler and disposed handler to an observable 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.
Error callback is not exposed because `Signal` can't error out.
- 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 onNext: Action to invoke for each element in the observable sequence.
- parameter onCompleted: Action to invoke upon graceful termination of the observable sequence.
gracefully completed, errored, or if the generation is canceled by disposing subscription)
- 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 emit<Object: AnyObject>(
with object: Object,
onNext: ((Object, Element) -> Void)? = nil,
onCompleted: ((Object) -> Void)? = nil,
onDisposed: ((Object) -> Void)? = nil
) -> Disposable {
self.asObservable().subscribe(
with: object,
onNext: onNext,
onCompleted: onCompleted,
onDisposed: onDisposed
)
}

/**
Subscribes an element handler, a completion handler and disposed handler to an observable sequence.
Error callback is not exposed because `Signal` can't error out.
- parameter onNext: Action to invoke for each element in the observable sequence.
- parameter onCompleted: Action to invoke upon graceful termination of the observable sequence.
gracefully completed, errored, or if the generation is canceled by disposing subscription)
- 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 emit(onNext: ((Element) -> Void)? = nil, onCompleted: (() -> Void)? = nil, onDisposed: (() -> Void)? = nil) -> Disposable {
public func emit(
onNext: ((Element) -> Void)? = nil,
onCompleted: (() -> Void)? = nil,
onDisposed: (() -> Void)? = nil
) -> Disposable {
self.asObservable().subscribe(onNext: onNext, onCompleted: onCompleted, onDisposed: onDisposed)
}

Expand Down

0 comments on commit 9bc9726

Please sign in to comment.