multicast

연산자(operator) 정의: multicast(selector: Function): Observable

Share source utilizing the provided Subject.

Examples

Example 1: multicast with standard Subject

( jsBin | jsFiddle )

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

const example = source
  //since we are multicasting below, side effects will be executed once
  .do(() => console.log('Side Effect #1'))
  .mapTo('Result!')

//subscribe subject to source upon connect()
const multi = example.multicast(() => new Rx.Subject());
/*
  subscribers will share source
  output:
  "Side Effect #1"
  "Result!"
  "Result!"
  ...
*/
const subscriberOne = multi.subscribe(val => console.log(val));
const subscriberTwo = multi.subscribe(val => console.log(val));
//subscribe subject to source
multi.connect();
Example 2: multicast with ReplaySubject

( jsBin | jsFiddle )

//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);

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/multicast.ts

results matching ""

    No results matching ""