Thuta Learning
Angular
AdvancedWeb Developmentအလယ်အလတ်

RxJS Operators

Angularသင်ခန်းစာ 2925 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • RxJS Operators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 နဲ့ရွေးပါ။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

RxJS Operators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
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 ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input + stateimport { map, filter, debounceTime, switchMap } from ...

    RxJS Operators များသည် Observable streams များကို transform, filter, နှင့် combine လုပ်ရန် အသုံးပြုသည်။

  2. 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 နဲ့ရွေးပါ။

  3. Rendered Angular UI).subscribe(results => this.results = results);

    Search box ကို debounce + distinctUntilChanged + switchMap ဖြင့် API ခေါ်ပြီး rapid typing, empty query, request error နဲ့ stale response cases စမ်းပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. 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 လုပ်ရန် အသုံးပြုသည်။

typescript
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);
You should see
(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 ကိုခန့်မှန်းပါ
typescript
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

RxJS Operators ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Nested subscribe သုံးခြင်းက cancellation, errors နဲ့ teardown ကိုခွဲကျစေပြီး race condition ဖြစ်လွယ်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

RxJS Operators နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Operators များသည် Observable streams များကို powerful ways များဖြင့် manipulate လုပ်နိုင်သည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

RxJS Operators | Thuta Learning