Daniel T. Daniel T. - 3 months ago 54
Swift Question

How to use ReplaySubject in ReactiveKit

I have this in RxSwift:

func foo() -> Observable<Int> {
let subject = RxSwift.ReplaySubject<Int>.create(bufferSize: 1)
return subject.asObservable()
}


How do I implement the same concept in ReactiveKit?

Answer

With ReactiveKit 3 that is currently in beta (rk3 branch) you can do:

func foo() -> Signal<Int, NoError> {
  let subject = ReplaySubject<Int, NoError>(bufferSize: 1)
  return subject.toSignal()
}

In ReactiveKit 2 ReplaySubject is generalised over events:

func foo() -> Stream<Int> {
  let subject = ReplaySubject<StreamEvent<Int>>(bufferSize: 1)
  return Stream(rawStream: subject.toRawStream())
}

or

func foo() -> Operation<Int, Error> {
  let subject = ReplaySubject<OperationEvent<Int>>(bufferSize: 1)
  return Operation(rawStream: subject.toRawStream())
}