ရိုးရိုးလေး ရှင်းပြမယ်
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 ရှောင်ပါ။
- Event / request
- Stream + service
- State / UI update
Observables Intro ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
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 ကို အဆင့်လိုက်ဖတ်မယ်
- Event / request
import { Observable } from 'rxjs';Observables များသည် RxJS library မှ လာသော asynchronous data streams များဖြစ်သည်။ Angular တွင် HTTP requests, events, နှင့် အခြား async operations များအတွက် အသုံးပြုသည်။
- Stream + service
next: (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 ရှောင်ပါ။
- State / UI update
});Observable တစ်ခုကို values သုံးခု emit နဲ့ teardown log ပါအောင်ရေးပြီး normal completion, error နဲ့ early unsubscribe cases စမ်းပါ။
- Event / request
- Stream + service
- 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 များအတွက် အသုံးပြုသည်။
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 သည် 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 ကိုခန့်မှန်းပါ
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
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- 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