1

Currently I'm building a project in Kotlin using RxKotlin. My background with Rx is primarily grounded in RxJS.

A pattern I would regularly use for creating hot observables in Typescript would look something along the lines of this:

  private dataStore: IFoo;
  private dataStoreSubject: BehaviorSubject<IFoo> = new BehaviorSubject(this.dataStore);
  public dataStoreObservable: Observable<IFoo> = Observable.from(this.dataStoreSubject);

  public getNetworkData(): Observable<IFoo[]> {
      return this.http.get()
         .map((response: IResponse) => {
             this.dataStore = <IFoo[]>response;
             this.dataStoreSubject.next(this.dataStore);
             return this.dataStore;
          });
   }

This would allow me to expose an Observable, without exposing the Subject and subsequently the subject.next(); method.

My question is: What would be the most idiomatic way to establish similar logic in RxKotlin or RxJava?

Devon Germano
  • 171
  • 12
  • 1
    So you just want to [`hide()`](http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observable.html#hide--) the `Subject`'s `Observer` methods? _(See what I did there?)_ – Gergely Kőrössy Feb 01 '18 at 17:13
  • That is helpful @GergelyKőrössy ! Moreso I'm trying to find a relationship between `BehaviorSubject` and `Observable`, as in Typescript it's fairly simple to create an `Observable` from a `BehaviorSubject` in RxJava it seems as if it works inversely where a `BehaviorSubject` requires a source `Observable`. Is it considered kosher to expose a `BehaviorSubject` to my observers? – Devon Germano Feb 01 '18 at 17:30
  • `BehaviorSubject` can be created using its static `create()` method without requiring anything "external". – Gergely Kőrössy Feb 01 '18 at 17:33
  • I'm aware of `BehaviorSubject.create()`. Like I said before, I'm curious as to the relationship between `BehaviorSubject` and `Observable`. I'm aware that I could just go around creating random `BehaviorSubjects`, but that doesn't necessarily explain the correlation between `BehaviorSubject` and `Observable`, or if exposing a `BehaviorSubject` is an idiomatic design decision. – Devon Germano Feb 01 '18 at 17:38
  • I'm not sure I understand your question. `Subject`s in general are `Observable`s and `Observer`s at the same time. You can use a `Subject` for internal purposes since it has the "push" methods (`on***`) and you can return an `Observable` using the `hide` operator so the caller cannot access those methods. – Gergely Kőrössy Feb 01 '18 at 19:50

1 Answers1

0

You need to use compose and transform. You can add "link" between your subject and observable into the transform.

Vova
  • 956
  • 8
  • 22