본문 바로가기
RxJS(Reactive X)

[스트림 결합] 실전 사용 모음

by 찬찬2 2024. 3. 13.

CASE #1

두 개의 스트림(A, B)이 있다. A 스트림에서 값을 발행하고 B는 발행하지 않을 경우 또는 A 스트림에서 값을 발행하지 않고 B에서 값을 발행하는 경우 두 스트림이 가지고 있는 현재, 마지막 값이 필요할 때.

 

※ reactive form을 사용 중

 

this.form = this._fb.group({
    dateRange: [''],
    activeSensorType: [''],
    outerData: this._fb.group({}),
    outerCheckedStates: this._fb.group({}),
    innerData: this._fb.group({}),
    innerCheckedStates: this._fb.group({})
  });

const A$ = this.form.get('dateRange').valueChanges.pipe(startWith(this.form.get('dateRange').value));
const B$ = this.form.get('activeSensorType').valueChanges.pipe(startWith(this.form.get('activeSensorType').value));
  
combineLatest([ A$, B$ ]).subscribe(([A, B]) => {
    console.log("++ combineLatest ++");
    console.log('Date range value:', A);
    console.log('Active sensor type value:', B);
});

 

핵심 연산자: Join creation operator: combineLatest, Join operator: startWith

댓글