ရိုးရိုးလေး ရှင်းပြမယ်
RxJS Operators ကို ရိုးရိုးလေးနားလည်မယ်
RxJS Operators များသည် Observable streams များကို transform, filter, နှင့် combine လုပ်ရန် အသုံးပြုသည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Nested subscribe သုံးခြင်းက cancellation, errors နဲ့ teardown ကိုခွဲကျစေပြီး race condition ဖြစ်လွယ်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
RxJS operators က source stream ကိုပြောင်းလဲသည့် composable functions ဖြစ်ပါတယ်။ `map` က value transform, `filter` က selection, `switchMap` က latest async work ဆီပြောင်းပြီး old work cancel, `concatMap` က order ထိန်း၊ `mergeMap` က concurrency ပေးပါတယ်။ Error handling နဲ့ retry placement က pipeline behavior ကိုပြောင်းတာကြောင့် marble/timeline mental model နဲ့ရွေးပါ။
- Input + state
- Component + template
- Rendered Angular UI
RxJS Operators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
import { map, filter, debounceTime, switchMap } from 'rxjs/operators';
import { fromEvent } from 'rxjs';
// map - transform each value
this.http.get<User[]>('/api/users').pipe(
map(users => users.map(u => u.name))
).subscribe(names => console.log(names));
// filter - only emit values that pass condition
this.numbers$.pipe(
filter(num => num % 2 === 0) // only even numbers
).subscribe(even => console.log(even));
// debounceTime - wait before emitting (good for search)
const searchBox = document.getElementById('search');
fromEvent(searchBox, 'input').pipe(
debounceTime(300), // wait 300ms after typing stops
map((event: any) => event.target.value)
).subscribe(searchTerm => this.search(searchTerm));
// switchMap - switch to new observable, cancel previous
this.searchControl.valueChanges.pipe(
debounceTime(300),
switchMap(term => this.searchService.search(term))
).subscribe(results => this.results = results);RxJS operators က source stream ကိုပြောင်းလဲသည့် composable functions ဖြစ်ပါတယ်။ `map` က value transform, `filter` က selection, `switchMap` က latest async work ဆီပြောင်းပြီး old work cancel, `concatMap` က order ထိန်း၊ `mergeMap` က concurrency ပေးပါတယ်။ Error handling နဲ့ retry placement က pipeline behavior ကိုပြောင်းတာကြောင့် marble/timeline mental model နဲ့ရွေးပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
import { map, filter, debounceTime, switchMap } from ...RxJS Operators များသည် Observable streams များကို transform, filter, နှင့် combine လုပ်ရန် အသုံးပြုသည်။
- Component + template
// debounceTime - wait before emitting (good for search)RxJS operators က source stream ကိုပြောင်းလဲသည့် composable functions ဖြစ်ပါတယ်။ `map` က value transform, `filter` က selection, `switchMap` က latest async work ဆီပြောင်းပြီး old work cancel, `concatMap` က order ထိန်း၊ `mergeMap` က concurrency ပေးပါတယ်။ Error handling နဲ့ retry placement က pipeline behavior ကိုပြောင်းတာကြောင့် marble/timeline mental model နဲ့ရွေးပါ။
- Rendered Angular UI
).subscribe(results => this.results = results);Search box ကို debounce + distinctUntilChanged + switchMap ဖြင့် API ခေါ်ပြီး rapid typing, empty query, request error နဲ့ stale response cases စမ်းပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
RxJS Operators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Nested subscribe သုံးခြင်းက cancellation, errors နဲ့ teardown ကိုခွဲကျစေပြီး race condition ဖြစ်လွယ်ပါတယ်။
RxJS Operators များသည် Observable streams များကို transform, filter, နှင့် combine လုပ်ရန် အသုံးပြုသည်။
import { map, filter, debounceTime, switchMap } from 'rxjs/operators';
import { fromEvent } from 'rxjs';
// map - transform each value
this.http.get<User[]>('/api/users').pipe(
map(users => users.map(u => u.name))
).subscribe(names => console.log(names));
// filter - only emit values that pass condition
this.numbers$.pipe(
filter(num => num % 2 === 0) // only even numbers
).subscribe(even => console.log(even));
// debounceTime - wait before emitting (good for search)
const searchBox = document.getElementById('search');
fromEvent(searchBox, 'input').pipe(
debounceTime(300), // wait 300ms after typing stops
map((event: any) => event.target.value)
).subscribe(searchTerm => this.search(searchTerm));
// switchMap - switch to new observable, cancel previous
this.searchControl.valueChanges.pipe(
debounceTime(300),
switchMap(term => this.searchService.search(term))
).subscribe(results => this.results = results);(Operators များသည် Observable streams များကို powerful ways များဖြင့် manipulate လုပ်နိုင်သည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Search box ကို debounce + distinctUntilChanged + switchMap ဖြင့် API ခေါ်ပြီး rapid typing, empty query, request error နဲ့ stale response cases စမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
RxJS Operators နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
import { map, filter, debounceTime, switchMap } from 'rxjs/operators';
import { fromEvent } from 'rxjs';
// map - transform each value
this.http.get<User[]>('/api/users').pipe(
map(users => users.map(u => u.name))
).subscribe(names => console.log(names));
// filter - only emit values that pass condition
this.numbers$.pipe(
filter(num => num % 2 === 0) // only even numbers
).subscribe(even => console.log(even));
// debounceTime - wait before emitting (good for search)
const searchBox = document.getElementById('search');
fromEvent(searchBox, 'input').pipe(
debounceTime(300), // wait 300ms after typing stops
map((event: any) => event.target.value)
).subscribe(searchTerm => this.search(searchTerm));
// switchMap - switch to new observable, cancel previous
this.searchControl.valueChanges.pipe(
debounceTime(300),
switchMap(term => this.searchService.search(term))
).subscribe(results => this.results = results);ရလဒ်: RxJS Operators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Search box ကို debounce + distinctUntilChanged + switchMap ဖြင့် API ခေါ်ပြီး rapid typing, empty query, request error နဲ့ stale response cases စမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- RxJS operators က source stream ကိုပြောင်းလဲသည့် composable functions ဖြစ်ပါတယ်။ `map` က value transform, `filter` က selection, `switchMap` က latest async work ဆီပြောင်းပြီး old work cancel, `concatMap` က order ထိန်း၊ `mergeMap` က concurrency ပေးပါတယ်။ Error handling နဲ့ retry placement က pipeline behavior ကိုပြောင်းတာကြောင့် marble/timeline mental model နဲ့ရွေးပါ။
- Nested subscribe သုံးခြင်းက cancellation, errors နဲ့ teardown ကိုခွဲကျစေပြီး race condition ဖြစ်လွယ်ပါတယ်။
- Search box ကို debounce + distinctUntilChanged + switchMap ဖြင့် API ခေါ်ပြီး rapid typing, empty query, request error နဲ့ stale response cases စမ်းပါ။
RxJS — Guide — RxJS