1
0

PublishRelay.swift 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // PublishRelay.swift
  3. // RxRelay
  4. //
  5. // Created by Krunoslav Zaher on 3/28/15.
  6. // Copyright © 2017 Krunoslav Zaher. All rights reserved.
  7. //
  8. import RxSwift
  9. /// PublishRelay is a wrapper for `PublishSubject`.
  10. ///
  11. /// Unlike `PublishSubject` it can't terminate with error or completed.
  12. public final class PublishRelay<Element>: ObservableType {
  13. private let subject: PublishSubject<Element>
  14. // Accepts `event` and emits it to subscribers
  15. public func accept(_ event: Element) {
  16. self.subject.onNext(event)
  17. }
  18. /// Initializes with internal empty subject.
  19. public init() {
  20. self.subject = PublishSubject()
  21. }
  22. /// Subscribes observer
  23. public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
  24. self.subject.subscribe(observer)
  25. }
  26. /// - returns: Canonical interface for push style sequence
  27. public func asObservable() -> Observable<Element> {
  28. self.subject.asObservable()
  29. }
  30. }