17

In RxJava, there is Observable.OnSubscribe which will call your action when something subscribes to the observable.

Is there an equivalent in RxJs?


It seems as though they've taken the page down since asking this question. Google's cached version lives here.

3
  • I've never seen/needed it and a quick search didn't find anything, though I don't know for sure - so I'll just post this as a comment Jan 26, 2017 at 22:01
  • I searched as well, but came up with nothing. I just find it odd that it exists in RxJava and not RxJs. I kept my specific scenario out of the question as I feel this is a stronger question by itself. I think the reason I need something like it is just due to bad design, unfortunately. Jan 26, 2017 at 22:08
  • 1
    It is definitely useful for debug and logging purposes Dec 24, 2019 at 13:17

3 Answers 3

14

Really like Mark's answer, but if you're using rxjs version 5 or later, I'd prefer using pure functions instead of patching the prototype.

Update:

Implemented without empty concat hack using defer as suggested by Ray

import {defer} from 'rxjs/observable/defer';
import {Observable} from 'rxjs/Observable';

/** Example
import {from} from 'rxjs/observable/from';

from([1, 2, 3])
    .pipe(doOnSubscribe(() => console.log('subscribed to stream')))
    .subscribe(x => console.log(x), null, () => console.log('completed'));
*/

export function doOnSubscribe<T>(onSubscribe: () => void): (source: Observable<T>) =>  Observable<T> {
    return function inner(source: Observable<T>): Observable<T> {
        return defer(() => {
          onSubscribe();
          return source;
        });
    };
}

https://gist.github.com/evxn/750702f7c8e8d5a32c7b53167fe14d8d

Original Answer:

import {empty} from 'rxjs/observable/empty';
import {concat, tap} from 'rxjs/operators';
import {Observable} from 'rxjs/Observable';

/** Example
import {from} from 'rxjs/observable/from';
from([1, 2, 3])
    .pipe(doOnSubscribe(() => console.log('subscribed to stream')))
    .subscribe(x => console.log(x), null, () => console.log('completed'));
*/

export function doOnSubscribe<T>(callback: () => void): (source: Observable<T>) =>  Observable<T> {
    return function inner(source: Observable<T>): Observable<T> {
        return empty().pipe(
            tap(null, null, callback),
            concat<T>(source)
        );
    };
}
0
11

This function is easily implemented by composition of the existing functions of RxJs5.

Rx.Observable.prototype.doOnSubscribe = function (onSubscribe) {
  return Rx.Observable.empty()
    .do(null,null, onSubscribe)
    .concat(this);
};

Rx.Observable.from([1,2,3])
  .doOnSubscribe(() => console.log('subscribed to stream'))
  .subscribe(console.log, null, () => console.log('completed'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.3/Rx.js"></script>

When downstream subscribes to the doOnSubscribe we first emit an empty observable and use the .do() onComplete callback as doOnSubscribe. Then we .concat() the upstream to this and return this modified stream.

9

Another example that builds on Mark's answer is to use the factory functions:

Rx.Observable.prototype.doOnSubscribe = function(onSubscribe) {
  const source = this; 
  return Rx.Observable.defer(() => {
      onSubscribe();
      return source;
  });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.