BACK
PracticeReal-life: sample() a snapshot on signal
PRACTICE · 58 · 練

Real-life: sample() a snapshot on signal

Take the latest sensor value only when a sampler event arrives.

  1. Solve the task in the editor — no long theory.
  2. Run the check and compare your output with the expected result.
  3. Stuck? Open the solution and carry the approach into your own code.
Solution spoiler · click to reveal
const { Subject, sample } = Rx;

const sensor$ = new Subject();
const sampler$ = new Subject();

const result$ = sensor$.pipe(
  sample(sampler$)
);

result$.subscribe(value => console.log(value));

sensor$.next('A');
sensor$.next('B');
sampler$.next('tick');
sensor$.next('C');
sampler$.next('tick');
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...