Deprecated.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //
  2. // Deprecated.swift
  3. // RxCocoa
  4. //
  5. // Created by Krunoslav Zaher on 3/19/17.
  6. // Copyright © 2017 Krunoslav Zaher. All rights reserved.
  7. //
  8. import RxSwift
  9. import Dispatch
  10. import Foundation
  11. extension ObservableType {
  12. /**
  13. Creates new subscription and sends elements to observer.
  14. In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables
  15. writing more consistent binding code.
  16. - parameter observer: Observer that receives events.
  17. - returns: Disposable object that can be used to unsubscribe the observer.
  18. */
  19. @available(*, deprecated, renamed: "bind(to:)")
  20. public func bindTo<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
  21. return self.subscribe(observer)
  22. }
  23. /**
  24. Creates new subscription and sends elements to observer.
  25. In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables
  26. writing more consistent binding code.
  27. - parameter observer: Observer that receives events.
  28. - returns: Disposable object that can be used to unsubscribe the observer.
  29. */
  30. @available(*, deprecated, renamed: "bind(to:)")
  31. public func bindTo<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element? {
  32. return self.map { $0 }.subscribe(observer)
  33. }
  34. /**
  35. Creates new subscription and sends elements to variable.
  36. In case error occurs in debug mode, `fatalError` will be raised.
  37. In case error occurs in release mode, `error` will be logged.
  38. - parameter variable: Target variable for sequence elements.
  39. - returns: Disposable object that can be used to unsubscribe the observer.
  40. */
  41. @available(*, deprecated, renamed: "bind(to:)")
  42. public func bindTo(_ variable: Variable<Element>) -> Disposable {
  43. return self.subscribe { e in
  44. switch e {
  45. case let .next(element):
  46. variable.value = element
  47. case let .error(error):
  48. let error = "Binding error to variable: \(error)"
  49. #if DEBUG
  50. rxFatalError(error)
  51. #else
  52. print(error)
  53. #endif
  54. case .completed:
  55. break
  56. }
  57. }
  58. }
  59. /**
  60. Creates new subscription and sends elements to variable.
  61. In case error occurs in debug mode, `fatalError` will be raised.
  62. In case error occurs in release mode, `error` will be logged.
  63. - parameter variable: Target variable for sequence elements.
  64. - returns: Disposable object that can be used to unsubscribe the observer.
  65. */
  66. @available(*, deprecated, renamed: "bind(to:)")
  67. public func bindTo(_ variable: Variable<Element?>) -> Disposable {
  68. return self.map { $0 as Element? }.bindTo(variable)
  69. }
  70. /**
  71. Subscribes to observable sequence using custom binder function.
  72. - parameter binder: Function used to bind elements from `self`.
  73. - returns: Object representing subscription.
  74. */
  75. @available(*, deprecated, renamed: "bind(to:)")
  76. public func bindTo<Result>(_ binder: (Self) -> Result) -> Result {
  77. return binder(self)
  78. }
  79. /**
  80. Subscribes to observable sequence using custom binder function and final parameter passed to binder function
  81. after `self` is passed.
  82. public func bindTo<R1, R2>(binder: Self -> R1 -> R2, curriedArgument: R1) -> R2 {
  83. return binder(self)(curriedArgument)
  84. }
  85. - parameter binder: Function used to bind elements from `self`.
  86. - parameter curriedArgument: Final argument passed to `binder` to finish binding process.
  87. - returns: Object representing subscription.
  88. */
  89. @available(*, deprecated, renamed: "bind(to:)")
  90. public func bindTo<R1, R2>(_ binder: (Self) -> (R1) -> R2, curriedArgument: R1) -> R2 {
  91. return binder(self)(curriedArgument)
  92. }
  93. /**
  94. Subscribes an element handler to an observable sequence.
  95. In case error occurs in debug mode, `fatalError` will be raised.
  96. In case error occurs in release mode, `error` will be logged.
  97. - parameter onNext: Action to invoke for each element in the observable sequence.
  98. - returns: Subscription object used to unsubscribe from the observable sequence.
  99. */
  100. @available(*, deprecated, renamed: "bind(onNext:)")
  101. public func bindNext(_ onNext: @escaping (Element) -> Void) -> Disposable {
  102. return self.subscribe(onNext: onNext, onError: { error in
  103. let error = "Binding error: \(error)"
  104. #if DEBUG
  105. rxFatalError(error)
  106. #else
  107. print(error)
  108. #endif
  109. })
  110. }
  111. }
  112. #if os(iOS) || os(tvOS)
  113. import UIKit
  114. extension NSTextStorage {
  115. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  116. public func createRxDelegateProxy() -> RxTextStorageDelegateProxy {
  117. fatalError()
  118. }
  119. }
  120. extension UIScrollView {
  121. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  122. public func createRxDelegateProxy() -> RxScrollViewDelegateProxy {
  123. fatalError()
  124. }
  125. }
  126. extension UICollectionView {
  127. @available(*, unavailable, message: "createRxDataSourceProxy is now unavailable, check DelegateProxyFactory")
  128. public func createRxDataSourceProxy() -> RxCollectionViewDataSourceProxy {
  129. fatalError()
  130. }
  131. }
  132. extension UITableView {
  133. @available(*, unavailable, message: "createRxDataSourceProxy is now unavailable, check DelegateProxyFactory")
  134. public func createRxDataSourceProxy() -> RxTableViewDataSourceProxy {
  135. fatalError()
  136. }
  137. }
  138. extension UINavigationBar {
  139. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  140. public func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy {
  141. fatalError()
  142. }
  143. }
  144. extension UINavigationController {
  145. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  146. public func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy {
  147. fatalError()
  148. }
  149. }
  150. extension UITabBar {
  151. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  152. public func createRxDelegateProxy() -> RxTabBarDelegateProxy {
  153. fatalError()
  154. }
  155. }
  156. extension UITabBarController {
  157. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  158. public func createRxDelegateProxy() -> RxTabBarControllerDelegateProxy {
  159. fatalError()
  160. }
  161. }
  162. extension UISearchBar {
  163. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  164. public func createRxDelegateProxy() -> RxSearchBarDelegateProxy {
  165. fatalError()
  166. }
  167. }
  168. #endif
  169. #if os(iOS)
  170. extension UISearchController {
  171. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  172. public func createRxDelegateProxy() -> RxSearchControllerDelegateProxy {
  173. fatalError()
  174. }
  175. }
  176. extension UIPickerView {
  177. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  178. public func createRxDelegateProxy() -> RxPickerViewDelegateProxy {
  179. fatalError()
  180. }
  181. @available(*, unavailable, message: "createRxDataSourceProxy is now unavailable, check DelegateProxyFactory")
  182. public func createRxDataSourceProxy() -> RxPickerViewDataSourceProxy {
  183. fatalError()
  184. }
  185. }
  186. #endif
  187. #if os(macOS)
  188. import Cocoa
  189. extension NSTextField {
  190. @available(*, unavailable, message: "createRxDelegateProxy is now unavailable, check DelegateProxyFactory")
  191. public func createRxDelegateProxy() -> RxTextFieldDelegateProxy {
  192. fatalError()
  193. }
  194. }
  195. #endif
  196. /**
  197. This method can be used in unit tests to ensure that driver is using mock schedulers instead of
  198. main schedulers.
  199. **This shouldn't be used in normal release builds.**
  200. */
  201. @available(*, deprecated, renamed: "SharingScheduler.mock(scheduler:action:)")
  202. public func driveOnScheduler(_ scheduler: SchedulerType, action: () -> Void) {
  203. SharingScheduler.mock(scheduler: scheduler, action: action)
  204. }
  205. @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
  206. extension Variable {
  207. /// Converts `Variable` to `SharedSequence` unit.
  208. ///
  209. /// - returns: Observable sequence.
  210. @available(*, deprecated, renamed: "asDriver()")
  211. public func asSharedSequence<SharingStrategy: SharingStrategyProtocol>(strategy: SharingStrategy.Type = SharingStrategy.self) -> SharedSequence<SharingStrategy, Element> {
  212. let source = self.asObservable()
  213. .observeOn(SharingStrategy.scheduler)
  214. return SharedSequence(source)
  215. }
  216. }
  217. #if !os(Linux)
  218. extension DelegateProxy {
  219. @available(*, unavailable, renamed: "assignedProxy(for:)")
  220. public static func assignedProxyFor(_ object: ParentObject) -> Delegate? {
  221. fatalError()
  222. }
  223. @available(*, unavailable, renamed: "currentDelegate(for:)")
  224. public static func currentDelegateFor(_ object: ParentObject) -> Delegate? {
  225. fatalError()
  226. }
  227. }
  228. #endif
  229. /**
  230. Observer that enforces interface binding rules:
  231. * can't bind errors (in debug builds binding of errors causes `fatalError` in release builds errors are being logged)
  232. * ensures binding is performed on main thread
  233. `UIBindingObserver` doesn't retain target interface and in case owned interface element is released, element isn't bound.
  234. In case event binding is attempted from non main dispatch queue, event binding will be dispatched async to main dispatch
  235. queue.
  236. */
  237. @available(*, deprecated, renamed: "Binder")
  238. public final class UIBindingObserver<UIElement, Value> : ObserverType where UIElement: AnyObject {
  239. public typealias Element = Value
  240. weak var UIElement: UIElement?
  241. let binding: (UIElement, Value) -> Void
  242. /// Initializes `ViewBindingObserver` using
  243. @available(*, deprecated, renamed: "UIBinder.init(_:scheduler:binding:)")
  244. public init(UIElement: UIElement, binding: @escaping (UIElement, Value) -> Void) {
  245. self.UIElement = UIElement
  246. self.binding = binding
  247. }
  248. /// Binds next element to owner view as described in `binding`.
  249. public func on(_ event: Event<Value>) {
  250. if !DispatchQueue.isMain {
  251. DispatchQueue.main.async {
  252. self.on(event)
  253. }
  254. return
  255. }
  256. switch event {
  257. case .next(let element):
  258. if let view = self.UIElement {
  259. self.binding(view, element)
  260. }
  261. case .error(let error):
  262. bindingError(error)
  263. case .completed:
  264. break
  265. }
  266. }
  267. /// Erases type of observer.
  268. ///
  269. /// - returns: type erased observer.
  270. public func asObserver() -> AnyObserver<Value> {
  271. return AnyObserver(eventHandler: self.on)
  272. }
  273. }
  274. #if os(iOS)
  275. extension Reactive where Base: UIRefreshControl {
  276. /// Bindable sink for `beginRefreshing()`, `endRefreshing()` methods.
  277. @available(*, deprecated, renamed: "isRefreshing")
  278. public var refreshing: Binder<Bool> {
  279. return self.isRefreshing
  280. }
  281. }
  282. #endif
  283. #if os(iOS) || os(tvOS)
  284. extension Reactive where Base: UIImageView {
  285. /// Bindable sink for `image` property.
  286. /// - parameter transitionType: Optional transition type while setting the image (kCATransitionFade, kCATransitionMoveIn, ...)
  287. @available(*, deprecated, renamed: "image")
  288. public func image(transitionType: String? = nil) -> Binder<UIImage?> {
  289. return Binder(base) { imageView, image in
  290. if let transitionType = transitionType {
  291. if image != nil {
  292. let transition = CATransition()
  293. transition.duration = 0.25
  294. transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  295. transition.type = CATransitionType(rawValue: transitionType)
  296. imageView.layer.add(transition, forKey: kCATransition)
  297. }
  298. }
  299. else {
  300. imageView.layer.removeAllAnimations()
  301. }
  302. imageView.image = image
  303. }
  304. }
  305. }
  306. extension Reactive where Base: UISegmentedControl {
  307. @available(*, deprecated, renamed: "enabledForSegment(at:)")
  308. public func enabled(forSegmentAt segmentAt: Int) -> Binder<Bool> {
  309. return enabledForSegment(at: segmentAt)
  310. }
  311. }
  312. #endif
  313. #if os(macOS)
  314. extension Reactive where Base: NSImageView {
  315. /// Bindable sink for `image` property.
  316. ///
  317. /// - parameter transitionType: Optional transition type while setting the image (kCATransitionFade, kCATransitionMoveIn, ...)
  318. @available(*, deprecated, renamed: "image")
  319. public func image(transitionType: String? = nil) -> Binder<NSImage?> {
  320. return Binder(self.base) { control, value in
  321. if let transitionType = transitionType {
  322. if value != nil {
  323. let transition = CATransition()
  324. transition.duration = 0.25
  325. transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
  326. transition.type = CATransitionType(rawValue: transitionType)
  327. control.layer?.add(transition, forKey: kCATransition)
  328. }
  329. }
  330. else {
  331. control.layer?.removeAllAnimations()
  332. }
  333. control.image = value
  334. }
  335. }
  336. }
  337. #endif
  338. @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
  339. extension Variable {
  340. /// Converts `Variable` to `Driver` trait.
  341. ///
  342. /// - returns: Driving observable sequence.
  343. public func asDriver() -> Driver<Element> {
  344. let source = self.asObservable()
  345. .observeOn(DriverSharingStrategy.scheduler)
  346. return Driver(source)
  347. }
  348. }
  349. private let errorMessage = "`drive*` family of methods can be only called from `MainThread`.\n" +
  350. "This is required to ensure that the last replayed `Driver` element is delivered on `MainThread`.\n"
  351. extension SharedSequenceConvertibleType where SharingStrategy == DriverSharingStrategy {
  352. /**
  353. Creates new subscription and sends elements to variable.
  354. This method can be only called from `MainThread`.
  355. - parameter variable: Target variable for sequence elements.
  356. - returns: Disposable object that can be used to unsubscribe the observer from the variable.
  357. */
  358. @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
  359. public func drive(_ variable: Variable<Element>) -> Disposable {
  360. MainScheduler.ensureRunningOnMainThread(errorMessage: errorMessage)
  361. return self.drive(onNext: { e in
  362. variable.value = e
  363. })
  364. }
  365. /**
  366. Creates new subscription and sends elements to variable.
  367. This method can be only called from `MainThread`.
  368. - parameter variable: Target variable for sequence elements.
  369. - returns: Disposable object that can be used to unsubscribe the observer from the variable.
  370. */
  371. @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
  372. public func drive(_ variable: Variable<Element?>) -> Disposable {
  373. MainScheduler.ensureRunningOnMainThread(errorMessage: errorMessage)
  374. return self.drive(onNext: { e in
  375. variable.value = e
  376. })
  377. }
  378. }
  379. @available(*, deprecated, message: "Variable is deprecated. Please use `BehaviorRelay` as a replacement.")
  380. extension ObservableType {
  381. /**
  382. Creates new subscription and sends elements to variable.
  383. In case error occurs in debug mode, `fatalError` will be raised.
  384. In case error occurs in release mode, `error` will be logged.
  385. - parameter to: Target variable for sequence elements.
  386. - returns: Disposable object that can be used to unsubscribe the observer.
  387. */
  388. public func bind(to variable: Variable<Element>) -> Disposable {
  389. return self.subscribe { e in
  390. switch e {
  391. case let .next(element):
  392. variable.value = element
  393. case let .error(error):
  394. let error = "Binding error to variable: \(error)"
  395. #if DEBUG
  396. rxFatalError(error)
  397. #else
  398. print(error)
  399. #endif
  400. case .completed:
  401. break
  402. }
  403. }
  404. }
  405. /**
  406. Creates new subscription and sends elements to variable.
  407. In case error occurs in debug mode, `fatalError` will be raised.
  408. In case error occurs in release mode, `error` will be logged.
  409. - parameter to: Target variable for sequence elements.
  410. - returns: Disposable object that can be used to unsubscribe the observer.
  411. */
  412. public func bind(to variable: Variable<Element?>) -> Disposable {
  413. return self.map { $0 as Element? }.bind(to: variable)
  414. }
  415. }
  416. // MARK: throttle
  417. extension SharedSequenceConvertibleType {
  418. /**
  419. Returns an Observable that emits the first and the latest item emitted by the source Observable during sequential time windows of a specified duration.
  420. This operator makes sure that no two elements are emitted in less then dueTime.
  421. - seealso: [debounce operator on reactivex.io](http://reactivex.io/documentation/operators/debounce.html)
  422. - parameter dueTime: Throttling duration for each element.
  423. - parameter latest: Should latest element received in a dueTime wide time window since last element emission be emitted.
  424. - returns: The throttled sequence.
  425. */
  426. @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "timeout(_:latest:)")
  427. public func throttle(_ dueTime: Foundation.TimeInterval, latest: Bool = true)
  428. -> SharedSequence<SharingStrategy, Element> {
  429. return throttle(.milliseconds(Int(dueTime * 1000.0)), latest: latest)
  430. }
  431. /**
  432. 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.
  433. - parameter dueTime: Throttling duration for each element.
  434. - returns: The throttled sequence.
  435. */
  436. @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "debounce(_:)")
  437. public func debounce(_ dueTime: Foundation.TimeInterval)
  438. -> SharedSequence<SharingStrategy, Element> {
  439. return debounce(.milliseconds(Int(dueTime * 1000.0)))
  440. }
  441. }
  442. // MARK: delay
  443. extension SharedSequenceConvertibleType {
  444. /**
  445. 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.
  446. - seealso: [delay operator on reactivex.io](http://reactivex.io/documentation/operators/delay.html)
  447. - parameter dueTime: Relative time shift of the source by.
  448. - parameter scheduler: Scheduler to run the subscription delay timer on.
  449. - returns: the source Observable shifted in time by the specified delay.
  450. */
  451. @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "delay(_:)")
  452. public func delay(_ dueTime: Foundation.TimeInterval)
  453. -> SharedSequence<SharingStrategy, Element> {
  454. return delay(.milliseconds(Int(dueTime * 1000.0)))
  455. }
  456. }
  457. extension SharedSequence where Element : RxAbstractInteger {
  458. /**
  459. Returns an observable sequence that produces a value after each period, using the specified scheduler to run timers and to send out observer messages.
  460. - seealso: [interval operator on reactivex.io](http://reactivex.io/documentation/operators/interval.html)
  461. - parameter period: Period for producing the values in the resulting sequence.
  462. - returns: An observable sequence that produces a value after each period.
  463. */
  464. @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "interval(_:)")
  465. public static func interval(_ period: Foundation.TimeInterval)
  466. -> SharedSequence<SharingStrategy, Element> {
  467. return interval(.milliseconds(Int(period * 1000.0)))
  468. }
  469. }
  470. // MARK: timer
  471. extension SharedSequence where Element: RxAbstractInteger {
  472. /**
  473. 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.
  474. - seealso: [timer operator on reactivex.io](http://reactivex.io/documentation/operators/timer.html)
  475. - parameter dueTime: Relative time at which to produce the first value.
  476. - parameter period: Period to produce subsequent values.
  477. - returns: An observable sequence that produces a value after due time has elapsed and then each period.
  478. */
  479. @available(*, deprecated, message: "Use DispatchTimeInterval overload instead.", renamed: "timer(_:)")
  480. public static func timer(_ dueTime: Foundation.TimeInterval, period: Foundation.TimeInterval)
  481. -> SharedSequence<SharingStrategy, Element> {
  482. return timer(.milliseconds(Int(dueTime * 1000.0)), period: .milliseconds(Int(period * 1000.0)))
  483. }
  484. }