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

Observables Intro

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

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

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

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

Observables Intro ကို ရိုးရိုးလေးနားလည်မယ်

Observables များသည် RxJS library မှ လာသော asynchronous data streams များဖြစ်သည်။ Angular တွင် HTTP requests, events, နှင့် အခြား async operations များအတွက် အသုံးပြုသည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Subscribe လုပ်ပြီး cleanup မရှိခြင်းက long-lived streams တွင် memory leak နဲ့ duplicate effects ဖြစ်စေနိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Observable က အချိန်အလိုက် zero သို့ many values, error သို့ completion ပို့နိုင်သော lazy stream contract ဖြစ်ပါတယ်။ Producer, subscription, next/error/complete channels နဲ့ teardown ကိုနားလည်ရပြီး Promise တစ်ခုနှင့်ကွာခြားချက်မှာ multiple values နဲ့ cancellation/composition ဖြစ်ပါတယ်။ Angular event/HTTP streams ကို operators ဖြင့် declarative pipeline ဆောက်ကာ nested subscriptions ရှောင်ပါ။

Mental Model
  1. Event / request
  2. Stream + service
  3. State / UI update

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

အဓိက Syntax / Pattern

typescript
import { Observable } from 'rxjs';

// Creating an Observable
const observable = new Observable(observer => {
  observer.next('First value');
  observer.next('Second value');
  
  setTimeout(() => {
    observer.next('Third value (after 2s)');
    observer.complete();
  }, 2000);
});

// Subscribing to the Observable
const subscription = observable.subscribe({
  next: (value) => console.log('Received:', value),
  error: (err) => console.error('Error:', err),
  complete: () => console.log('Stream completed!')
});

// Unsubscribe when done
// subscription.unsubscribe();

// Real example: HTTP request
this.http.get('/api/users').subscribe({
  next: (data) => this.users = data,
  error: (err) => console.error(err),
  complete: () => console.log('Request completed')
});

Observable က အချိန်အလိုက် zero သို့ many values, error သို့ completion ပို့နိုင်သော lazy stream contract ဖြစ်ပါတယ်။ Producer, subscription, next/error/complete channels နဲ့ teardown ကိုနားလည်ရပြီး Promise တစ်ခုနှင့်ကွာခြားချက်မှာ multiple values နဲ့ cancellation/composition ဖြစ်ပါတယ်။ Angular event/HTTP streams ကို operators ဖြင့် declarative pipeline ဆောက်ကာ nested subscriptions ရှောင်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Event / requestimport { Observable } from 'rxjs';

    Observables များသည် RxJS library မှ လာသော asynchronous data streams များဖြစ်သည်။ Angular တွင် HTTP requests, events, နှင့် အခြား async operations များအတွက် အသုံးပြုသည်။

  2. Stream + servicenext: (value) => console.log('Received:', value),

    Observable က အချိန်အလိုက် zero သို့ many values, error သို့ completion ပို့နိုင်သော lazy stream contract ဖြစ်ပါတယ်။ Producer, subscription, next/error/complete channels နဲ့ teardown ကိုနားလည်ရပြီး Promise တစ်ခုနှင့်ကွာခြားချက်မှာ multiple values နဲ့ cancellation/composition ဖြစ်ပါတယ်။ Angular event/HTTP streams ကို operators ဖြင့် declarative pipeline ဆောက်ကာ nested subscriptions ရှောင်ပါ။

  3. State / UI update});

    Observable တစ်ခုကို values သုံးခု emit နဲ့ teardown log ပါအောင်ရေးပြီး normal completion, error နဲ့ early unsubscribe cases စမ်းပါ။

Execution Flow
  1. Event / request
  2. Stream + service
  3. State / UI update

Success

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

Failure

Subscribe လုပ်ပြီး cleanup မရှိခြင်းက long-lived streams တွင် memory leak နဲ့ duplicate effects ဖြစ်စေနိုင်ပါတယ်။

Observables များသည် RxJS library မှ လာသော asynchronous data streams များဖြစ်သည်။ Angular တွင် HTTP requests, events, နှင့် အခြား async operations များအတွက် အသုံးပြုသည်။

typescript
import { Observable } from 'rxjs';

// Creating an Observable
const observable = new Observable(observer => {
  observer.next('First value');
  observer.next('Second value');
  
  setTimeout(() => {
    observer.next('Third value (after 2s)');
    observer.complete();
  }, 2000);
});

// Subscribing to the Observable
const subscription = observable.subscribe({
  next: (value) => console.log('Received:', value),
  error: (err) => console.error('Error:', err),
  complete: () => console.log('Stream completed!')
});

// Unsubscribe when done
// subscription.unsubscribe();

// Real example: HTTP request
this.http.get('/api/users').subscribe({
  next: (data) => this.users = data,
  error: (err) => console.error(err),
  complete: () => console.log('Request completed')
});
You should see
(Observable သည် data stream တစ်ခုကို emit လုပ်ပြီး subscribers များက receive လုပ်မည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Observable တစ်ခုကို values သုံးခု emit နဲ့ teardown log ပါအောင်ရေးပြီး normal completion, error နဲ့ early unsubscribe cases စမ်းပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

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

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
import { Observable } from 'rxjs';

// Creating an Observable
const observable = new Observable(observer => {
  observer.next('First value');
  observer.next('Second value');
  
  setTimeout(() => {
    observer.next('Third value (after 2s)');
    observer.complete();
  }, 2000);
});

// Subscribing to the Observable
const subscription = observable.subscribe({
  next: (value) => console.log('Received:', value),
  error: (err) => console.error('Error:', err),
  complete: () => console.log('Stream completed!')
});

// Unsubscribe when done
// subscription.unsubscribe();

// Real example: HTTP request
this.http.get('/api/users').subscribe({
  next: (data) => this.users = data,
  error: (err) => console.error(err),
  complete: () => console.log('Request completed')
});

ရလဒ်: Observables Intro ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Observable တစ်ခုကို values သုံးခု emit နဲ့ teardown log ပါအောင်ရေးပြီး normal completion, error နဲ့ early unsubscribe cases စမ်းပါ။

Quick Quiz 1 / 3

Observables Intro ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

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

Quick Quiz 3 / 3

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

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

  • Event / request → Stream + service → State / UI update
  • Observable က အချိန်အလိုက် zero သို့ many values, error သို့ completion ပို့နိုင်သော lazy stream contract ဖြစ်ပါတယ်။ Producer, subscription, next/error/complete channels နဲ့ teardown ကိုနားလည်ရပြီး Promise တစ်ခုနှင့်ကွာခြားချက်မှာ multiple values နဲ့ cancellation/composition ဖြစ်ပါတယ်။ Angular event/HTTP streams ကို operators ဖြင့် declarative pipeline ဆောက်ကာ nested subscriptions ရှောင်ပါ။
  • Subscribe လုပ်ပြီး cleanup မရှိခြင်းက long-lived streams တွင် memory leak နဲ့ duplicate effects ဖြစ်စေနိုင်ပါတယ်။
  • Observable တစ်ခုကို values သုံးခု emit နဲ့ teardown log ပါအောင်ရေးပြီး normal completion, error နဲ့ early unsubscribe cases စမ်းပါ။

RxJS — Guide — RxJS

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

  • Subscribe လုပ်ပြီး cleanup မရှိခြင်းက long-lived streams တွင် memory leak နဲ့ duplicate effects ဖြစ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

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

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

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Observable သည် data stream တစ်ခုကို emit လုပ်ပြီး subscribers များက receive လုပ်မည်)

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

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

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

Observables Intro | Thuta Learning