multicast - 2

RxJS 5 multicast

by Brian Troncone

HTML

<script src="https://npmcdn.com/@reactivex/[email protected]/dist/global/Rx.umd.js"></script>

JavaScript

//emit every 2 seconds, take 5
const source = Rx.Observable.interval(2000).take(5);

//example with ReplaySubject
const example = source
  //since we are multicasting below, side effects will be executed once
  .do(() => console.log('Side Effect #2'))
  .mapTo('Result Two!')
//can use any type of subject
const multi = example.multicast(() => new Rx.ReplaySubject(5));
//subscribe subject to source
multi.connect();

setTimeout(() => { 
  /*
   subscriber will receieve all previous values on subscription because
   of ReplaySubject
   */
  const subscriber = multi
    .subscribe(val => console.group(val));
}, 5000);