skip to content

shareReplay

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const { rxObserver, palette } = require('api/v0.3');
const { timer, from } = require('rxjs');
const { zip, shareReplay, take } = require('rxjs/operators');


// our source will be a basic timer
// making 5 ticks, every 5ms
const source$ = timer(0, 5).pipe(
  take(5),
  zip(from(palette), Marble) // add color to items
);

const shared$ = source$.pipe(
  shareReplay()
);


// subscriptions and visualisations:
// creating observers for source$
const a = rxObserver('source$');
const b = rxObserver('source$ — delayed subscription');
source$.subscribe(a);

// creating observers for shared$
const c = rxObserver('shared$');
const d = rxObserver('shared$ — delayed subscription');
shared$.subscribe(c);

// delayed subscriptions
setTimeout(()=>{
  source$.subscribe(b);
  shared$.subscribe(d);
}, 10);



// helpers
function Marble(value,color) {
  return {
    valueOf: ()=>value
    , color
  };
}

0mssource$startcomplete00 11 22 33 44 source$ — delayed subscriptionstartcomplete00 11 22 33 44 shared$startcomplete00 11 22 33 44 shared$ — delayed subscriptionstartcomplete00 11 22 33 44