ObservableType.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // ObservableType.swift
  3. // RxSwift
  4. //
  5. // Created by Krunoslav Zaher on 8/8/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. /// Represents a push style sequence.
  9. public protocol ObservableType : ObservableConvertibleType {
  10. /**
  11. Subscribes `observer` to receive events for this sequence.
  12. ### Grammar
  13. **Next\* (Error | Completed)?**
  14. * sequences can produce zero or more elements so zero or more `Next` events can be sent to `observer`
  15. * once an `Error` or `Completed` event is sent, the sequence terminates and can't produce any other elements
  16. It is possible that events are sent from different threads, but no two events can be sent concurrently to
  17. `observer`.
  18. ### Resource Management
  19. When sequence sends `Complete` or `Error` event all internal resources that compute sequence elements
  20. will be freed.
  21. To cancel production of sequence elements and free resources immediately, call `dispose` on returned
  22. subscription.
  23. - returns: Subscription for `observer` that can be used to cancel production of sequence elements and free resources.
  24. */
  25. func subscribe<O: ObserverType>(_ observer: O) -> Disposable where O.E == E
  26. }
  27. extension ObservableType {
  28. /// Default implementation of converting `ObservableType` to `Observable`.
  29. public func asObservable() -> Observable<E> {
  30. // temporary workaround
  31. //return Observable.create(subscribe: self.subscribe)
  32. return Observable.create { o in
  33. return self.subscribe(o)
  34. }
  35. }
  36. }