subscribe with object for Primitive Sequences

This commit is contained in:
freak4pc 2021-01-30 23:03:08 +02:00 committed by Shai Mishali
parent 9bc9726eeb
commit 83f32e3d7e
3 changed files with 112 additions and 0 deletions

View File

@ -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<Object: AnyObject>(
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.

View File

@ -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<Object: AnyObject>(
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.

View File

@ -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<Object: AnyObject>(
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.