123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- //
- // Deprecated.swift
- // RxCocoa
- //
- // Created by Krunoslav Zaher on 3/19/17.
- // Copyright © 2017 Krunoslav Zaher. All rights reserved.
- //
- import RxSwift
- import Dispatch
- import Foundation
- extension ObservableType {
- /**
- Creates new subscription and sends elements to observer.
- In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables
- writing more consistent binding code.
- - parameter observer: Observer that receives events.
- - returns: Disposable object that can be used to unsubscribe the observer.
- */
- @available(*, deprecated, renamed: "bind(to:)")
- public func bindTo<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
- return self.subscribe(observer)
- }
- /**
- Creates new subscription and sends elements to observer.
- In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables
- writing more consistent binding code.
- - parameter observer: Observer that receives events.
- - returns: Disposable object that can be used to unsubscribe the observer.
- */
- @available(*, deprecated, renamed: "bind(to:)")
- public func bindTo<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element? {
- return self.map { $0 }.subscribe(observer)
- }
- /**
- Creates new subscription and sends elements to variable.
- In case error occurs in debug mode, `fatalError` will be raised.
- In case error occurs in release mode, `error` will be logged.
- - parameter variable: Target variable for sequence elements.
- - returns: Disposable object that can be used to unsubscribe the observer.
- */
- @available(*, deprecated, renamed: "bind(to:)")
- public func bindTo(_ variable: Variable<Element>) -> Disposable {
- return self.subscribe { e in
- switch e {
- case let .next(element):
- variable.value = element
- case let .error(error):
- let error = "Binding error to variable: \(error)"
- #if DEBUG
- rxFatalError(error)
- #else
- print(error)
- #endif
- case .completed:
- break
- }
- }
- }
- /**
- Creates new subscription and sends elements to variable.
- In case error occurs in debug mode, `fatalError` will be raised.
- In case error occurs in release mode, `error` will be logged.
- - parameter variable: Target variable for sequence elements.
- - returns: Disposable object that can be used to unsubscribe the observer.
- */
- @available(*, deprecated, renamed: "bind(to:)")
- public func bindTo(_ variable: Variable<Element?>) -> Disposable {
- return self.map { $0 as Element? }.bindTo(variable)
- }
- /**
- Subscribes to observable sequence using custom binder function.
- - parameter binder: Function used to bind elements from `self`.
- - returns: Object representing subscription.
- */
- @available(*, deprecated, renamed: "bind(to:)")
- public func bindTo<Result>(_ binder: (Self) -> Result) -> Result {
- return binder(self)
- }
- /**
- Subscribes to observable sequence using custom binder function and final parameter passed to binder function
- after `self` is passed.
- public func bindTo<R1, R2>(binder: Self -> R1 -> R2, curriedArgument: R1) -> R2 {
- return binder(self)(curriedArgument)
- }
- - parameter binder: Function used to bind elements from `self`.
- - parameter curriedArgument: Final argument passed to `binder` to finish binding process.
- - returns: Object representing subscription.
- */
- @available(*, deprecated, renamed: "bind(to:)")
- public func bindTo<R1, R2>(_ binder: (Self) -> (R1) -> R2, curriedArgument: R1) -> R2 {
- return binder(self)(curriedArgument)
- }
- /**
- Subscribes an element handler to an observable sequence.
- In case error occurs in debug mode, `fatalError` will be raised.
- In case error occurs in release mode, `error` will be logged.
- - parameter onNext: Action to invoke for each element in the observable sequence.
- - returns: Subscription object used to unsubscribe from the observable sequence.
- */
- @available(*, deprecated, renamed: "bind(onNext:)")
- public func bindNext(_ onNext: @escaping (Element) -> Void) -> Disposable {
- return self.subscribe(onNext: onNext, onError: { error in
- let error = "Binding error: \(error)"
- #if DEBUG
- rxFatalError(error)
- #else
- print(error)
- #endif
- })
- }
- }
- #if os(iOS) || os(tvOS)
- import UIKit
- extension NSTextStorage {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxTextStorageDelegateProxy {
- fatalError()
- }
- }
- extension UIScrollView {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxScrollViewDelegateProxy {
- fatalError()
- }
- }
- extension UICollectionView {
- @available(*, unavailable, message: "createRxDataSourceProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDataSourceProxy() -> RxCollectionViewDataSourceProxy {
- fatalError()
- }
- }
- extension UITableView {
- @available(*, unavailable, message: "createRxDataSourceProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDataSourceProxy() -> RxTableViewDataSourceProxy {
- fatalError()
- }
- }
- extension UINavigationBar {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy {
- fatalError()
- }
- }
- extension UINavigationController {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy {
- fatalError()
- }
- }
- extension UITabBar {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxTabBarDelegateProxy {
- fatalError()
- }
- }
- extension UITabBarController {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxTabBarControllerDelegateProxy {
- fatalError()
- }
- }
- extension UISearchBar {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxSearchBarDelegateProxy {
- fatalError()
- }
- }
- #endif
- #if os(iOS)
- extension UISearchController {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxSearchControllerDelegateProxy {
- fatalError()
- }
- }
- extension UIPickerView {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxPickerViewDelegateProxy {
- fatalError()
- }
- @available(*, unavailable, message: "createRxDataSourceProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDataSourceProxy() -> RxPickerViewDataSourceProxy {
- fatalError()
- }
- }
- #endif
- #if os(macOS)
- import Cocoa
- extension NSTextField {
- @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
- public func createRxDelegateProxy() -> RxTextFieldDelegateProxy {
- fatalError()
- }
- }
- #endif
- /**
- This method can be used in unit tests to ensure that driver is using mock schedulers instead of
- main schedulers.
- **This shouldn't be used in normal release builds.**
- */
- @available(*, deprecated, renamed: "SharingScheduler.mock(scheduler:action:)")
- public func driveOnScheduler(_ scheduler: SchedulerType, action: () -> Void) {
- SharingScheduler.mock(scheduler: scheduler, action: action)
- }
- @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
- extension Variable {
- /// Converts `Variable` to `SharedSequence` unit.
- ///
- /// - returns: Observable sequence.
- @available(*, deprecated, renamed: "asDriver()")
- public func asSharedSequence<SharingStrategy: SharingStrategyProtocol>(strategy: SharingStrategy.Type = SharingStrategy.self) -> SharedSequence<SharingStrategy, Element> {
- let source = self.asObservable()
- .observeOn(SharingStrategy.scheduler)
- return SharedSequence(source)
- }
- }
- #if !os(Linux)
- extension DelegateProxy {
- @available(*, unavailable, renamed: "assignedProxy(for:)")
- public static func assignedProxyFor(_ object: ParentObject) -> Delegate? {
- fatalError()
- }
-
- @available(*, unavailable, renamed: "currentDelegate(for:)")
- public static func currentDelegateFor(_ object: ParentObject) -> Delegate? {
- fatalError()
- }
- }
- #endif
- /**
- Observer that enforces interface binding rules:
- * can't bind errors (in debug builds binding of errors causes `fatalError` in release builds errors are being logged)
- * ensures binding is performed on main thread
-
- `UIBindingObserver` doesn't retain target interface and in case owned interface element is released, element isn't bound.
-
- In case event binding is attempted from non main dispatch queue, event binding will be dispatched async to main dispatch
- queue.
- */
- @available(*, deprecated, renamed: "Binder")
- public final class UIBindingObserver<UIElement, Value> : ObserverType where UIElement: AnyObject {
- public typealias Element = Value
- weak var UIElement: UIElement?
- let binding: (UIElement, Value) -> Void
- /// Initializes `ViewBindingObserver` using
- @available(*, deprecated, renamed: "UIBinder.init(_:scheduler:binding:)")
- public init(UIElement: UIElement, binding: @escaping (UIElement, Value) -> Void) {
- self.UIElement = UIElement
- self.binding = binding
- }
- /// Binds next element to owner view as described in `binding`.
- public func on(_ event: Event<Value>) {
- if !DispatchQueue.isMain {
- DispatchQueue.main.async {
- self.on(event)
- }
- return
- }
- switch event {
- case .next(let element):
- if let view = self.UIElement {
- self.binding(view, element)
- }
- case .error(let error):
- bindingError(error)
- case .completed:
- break
- }
- }
- /// Erases type of observer.
- ///
- /// - returns: type erased observer.
- public func asObserver() -> AnyObserver<Value> {
- return AnyObserver(eventHandler: self.on)
- }
- }
- #if os(iOS)
- extension Reactive where Base: UIRefreshControl {
- /// Bindable sink for `beginRefreshing()`, `endRefreshing()` methods.
- @available(*, deprecated, renamed: "isRefreshing")
- public var refreshing: Binder<Bool> {
- return self.isRefreshing
- }
- }
- #endif
- #if os(iOS) || os(tvOS)
- extension Reactive where Base: UIImageView {
- /// Bindable sink for `image` property.
- /// - parameter transitionType: Optional transition type while setting the image (kCATransitionFade, kCATransitionMoveIn, ...)
- @available(*, deprecated, renamed: "image")
- public func image(transitionType: String? = nil) -> Binder<UIImage?> {
- return Binder(base) { imageView, image in
- if let transitionType = transitionType {
- if image != nil {
- let transition = CATransition()
- transition.duration = 0.25
- transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
- transition.type = CATransitionType(rawValue: transitionType)
- imageView.layer.add(transition, forKey: kCATransition)
- }
- }
- else {
- imageView.layer.removeAllAnimations()
- }
- imageView.image = image
- }
- }
- }
-
- extension Reactive where Base: UISegmentedControl {
- @available(*, deprecated, renamed: "enabledForSegment(at:)")
- public func enabled(forSegmentAt segmentAt: Int) -> Binder<Bool> {
- return enabledForSegment(at: segmentAt)
- }
- }
- #endif
- #if os(macOS)
- extension Reactive where Base: NSImageView {
- /// Bindable sink for `image` property.
- ///
- /// - parameter transitionType: Optional transition type while setting the image (kCATransitionFade, kCATransitionMoveIn, ...)
- @available(*, deprecated, renamed: "image")
- public func image(transitionType: String? = nil) -> Binder<NSImage?> {
- return Binder(self.base) { control, value in
- if let transitionType = transitionType {
- if value != nil {
- let transition = CATransition()
- transition.duration = 0.25
- transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
- transition.type = CATransitionType(rawValue: transitionType)
- control.layer?.add(transition, forKey: kCATransition)
- }
- }
- else {
- control.layer?.removeAllAnimations()
- }
- control.image = value
- }
- }
- }
- #endif
- @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
- extension Variable {
- /// Converts `Variable` to `Driver` trait.
- ///
- /// - returns: Driving observable sequence.
- public func asDriver() -> Driver<Element> {
- let source = self.asObservable()
- .observeOn(DriverSharingStrategy.scheduler)
- return Driver(source)
- }
- }
- private let errorMessage = "`drive*` family of methods can be only called from `MainThread`.\n" +
- "This is required to ensure that the last replayed `Driver` element is delivered on `MainThread`.\n"
- extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingStrategy {
- /**
- Creates new subscription and sends elements to variable.
- This method can be only called from `MainThread`.
- - parameter variable: Target variable for sequence elements.
- - returns: Disposable object that can be used to unsubscribe the observer from the variable.
- */
- @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
- public func drive(_ variable: Variable<Element>) -> Disposable {
- MainScheduler.ensureRunningOnMainThread(errorMessage: errorMessage)
- return self.drive(onNext: { e in
- variable.value = e
- })
- }
- /**
- Creates new subscription and sends elements to variable.
- This method can be only called from `MainThread`.
- - parameter variable: Target variable for sequence elements.
- - returns: Disposable object that can be used to unsubscribe the observer from the variable.
- */
- @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
- public func drive(_ variable: Variable<Element?>) -> Disposable {
- MainScheduler.ensureRunningOnMainThread(errorMessage: errorMessage)
- return self.drive(onNext: { e in
- variable.value = e
- })
- }
- }
- @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
- extension ObservableType {
- /**
- Creates new subscription and sends elements to variable.
- In case error occurs in debug mode, `fatalError` will be raised.
- In case error occurs in release mode, `error` will be logged.
- - parameter to: Target variable for sequence elements.
- - returns: Disposable object that can be used to unsubscribe the observer.
- */
- public func bind(to variable: Variable<Element>) -> Disposable {
- return self.subscribe { e in
- switch e {
- case let .next(element):
- variable.value = element
- case let .error(error):
- let error = "Binding error to variable: \(error)"
- #if DEBUG
- rxFatalError(error)
- #else
- print(error)
- #endif
- case .completed:
- break
- }
- }
- }
- /**
- Creates new subscription and sends elements to variable.
- In case error occurs in debug mode, `fatalError` will be raised.
- In case error occurs in release mode, `error` will be logged.
- - parameter to: Target variable for sequence elements.
- - returns: Disposable object that can be used to unsubscribe the observer.
- */
- public func bind(to variable: Variable<Element?>) -> Disposable {
- return self.map { $0 as Element? }.bind(to: variable)
- }
- }
- // MARK: throttle
- extension SharedSequenceConvertibleType {
- /**
- Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration.
- This operator makes sure that no two elements are emitted in less then dueTime.
- - seealso: [debounce operator on reactivex.io](http://reactivex.io/documentation/operators/debounce.html)
- - parameter dueTime: Throttling duration for each element.
- - parameter latest: Should latest element received in a dueTime wide time window since last element emission be emitted.
- - returns: The throttled sequence.
- */
- @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "timeout(_:latest:)")
- public func throttle(_ dueTime: Foundation.TimeInterval, latest: Bool = true)
- -> SharedSequence<SharingStrategy, Element> {
- return throttle(.milliseconds(Int(dueTime * 1000.0)), latest: latest)
- }
- /**
- Ignores elements from an observable sequence which are followed by another element within a specified relative time duration, using the specified scheduler to run throttling timers.
- - parameter dueTime: Throttling duration for each element.
- - returns: The throttled sequence.
- */
- @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "debounce(_:)")
- public func debounce(_ dueTime: Foundation.TimeInterval)
- -> SharedSequence<SharingStrategy, Element> {
- return debounce(.milliseconds(Int(dueTime * 1000.0)))
- }
- }
- // MARK: delay
- extension SharedSequenceConvertibleType {
-
- /**
- Returns an observable sequence by the source observable sequence shifted forward in time by a specified delay. Error events from the source observable sequence are not delayed.
-
- - seealso: [delay operator on reactivex.io](http://reactivex.io/documentation/operators/delay.html)
-
- - parameter dueTime: Relative time shift of the source by.
- - parameter scheduler: Scheduler to run the subscription delay timer on.
- - returns: the source Observable shifted in time by the specified delay.
- */
- @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "delay(_:)")
- public func delay(_ dueTime: Foundation.TimeInterval)
- -> SharedSequence<SharingStrategy, Element> {
- return delay(.milliseconds(Int(dueTime * 1000.0)))
- }
- }
- extension SharedSequence where Element : RxAbstractInteger {
- /**
- Returns an observable sequence that produces a value after each period, using the specified scheduler to run timers and to send out observer messages.
-
- - seealso: [interval operator on reactivex.io](http://reactivex.io/documentation/operators/interval.html)
-
- - parameter period: Period for producing the values in the resulting sequence.
- - returns: An observable sequence that produces a value after each period.
- */
- @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "interval(_:)")
- public static func interval(_ period: Foundation.TimeInterval)
- -> SharedSequence<SharingStrategy, Element> {
- return interval(.milliseconds(Int(period * 1000.0)))
- }
- }
- // MARK: timer
- extension SharedSequence where Element: RxAbstractInteger {
- /**
- Returns an observable sequence that periodically produces a value after the specified initial relative due time has elapsed, using the specified scheduler to run timers.
-
- - seealso: [timer operator on reactivex.io](http://reactivex.io/documentation/operators/timer.html)
-
- - parameter dueTime: Relative time at which to produce the first value.
- - parameter period: Period to produce subsequent values.
- - returns: An observable sequence that produces a value after due time has elapsed and then each period.
- */
- @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "timer(_:)")
- public static func timer(_ dueTime: Foundation.TimeInterval, period: Foundation.TimeInterval)
- -> SharedSequence<SharingStrategy, Element> {
- return timer(.milliseconds(Int(dueTime * 1000.0)), period: .milliseconds(Int(period * 1000.0)))
- }
- }
|