Gentle Introduction to Reactive Programming

Gentle Introduction to Reactive Programming

Reactive programming paradigm ဆိုပြီးနောက်ပိုင်းမှာ Rx.java, Rx.js Spring Reactor WebFlux စတာတွေကေနသုံးလာတာရှိတယ်။
ဒါပေသိ နားမလည်ကြလို့ တိုင်ပတ် နေတာမြင်ကြလို့။
Reactive Programming က သူ့ချည်းပဲမနေဘူး Functional Reactive Programming ဆိုပြီးတွဲသုံးတယ် အဲ့တော့ FP ကိုသိမှနားလည်မယ်။
မဟုတ်ရင် operator တွေဖြစ်တဲ့ map, flatMap, zip, merge, concat စတာတွေကိုနားလည်ဖို့မလွယ်ဘူးဖြစ်မယ်။

ဘာလို့ Reactive Programming ကိုသုံးလဲဆိုတော့ Asynchronous programming ကြောင့် ပေါ့။
အောက်က code ကိုကြည့်ပါ။

JS code ဆိုပါစို. Asynchronous ကို လုပ်ထားတာ ေပါ့ setTimeout ကိုသုံးပြီးတော့ data return ပြန်ထားတာ။
function getDataAfter3Sec()
{
setTimeout(()=>{
return “data”
},3000);
}
ခုနက function ကိုခေါ်လိုက်ရင် data က ချက်ချင်းမရဘူး ေခါ်တဲ့ကောင်စီလဲ ပြန်မလာနိုင်ဘူး။ Asynchronous မို့လို့ပါပဲ။
သေချာနားလည်ချင်ရင်တော့ဒီသွားဖတ်
https://www.facebook.com/thet.khine.587/posts/pfbid032nC2cbJxatpQJdqjsEusKkCAYrJH236CSjvkxFt85hVTwjqM37bAPdj9PyXz3Xtql
အဲ့တော့ခုနက code ကို Callback နဲ့ရေးရင်ဒီလိုရမယ်
function getDataAfter3Sec(callback)
{
setTimeout(()=>{
callback(“data”)
},3000);
}
function callback(data)
{
console.log(“Callback “,data);
}
console.log(“Start”);
getDataAfter3Sec(callback);
console.log(“After call”);
ဒါဆိုရင် ခုနက code ကို callback သုံးပြီး ဖြေရှင်းလို့ရတယ်။ Callback မှာလဲပြဿနာက ဘာလဲဆိုတော့ nested လုပ်တာများရင် callback hell ဖြစ်လာတယ်။
အဲ့တော့ Promise ကိုသုံးပေါ့။
function getDataAfter3Sec()
{
return new Promise((resolve,reject)=>{
resolve(“data”);
})
}
getDataAfter3Sec().then(data=>console.log(“Data “,data))
Promise ကဘာကောင်းသွားလဲဆိုတော့ then တွေကို sequential programming style နဲ့ရေးလို့ရသွားတာ
.then()
.then()
ပုံစံနဲ့ အဲ့တော့အရင်က async မှာ control flow လိုက်ကြည့်ရတာ callback ထက်စာရင်သက်သာတယ်။
Promise ကလဲ functional programming က Monad လိုကောင်် တွေကိုယူသုံး railway oriented programming နဲ့ ထဲ့ပြီး error handling ဘာညာလုပ်ထားတာ။
အဲ့တော့နောက်ဆုံးကဘာလဲဆိုတော့ async await ပေါ်လာတယ်။ imperative style ရေးလို့ရသွားတယ်ပေါ့။
နောက်တော့ Reactive Programming ပေါ်လာတာ။

Reactive Programming သည် underlying မှာ Observabl Stream ကိုသုံးတယ်။ Observer Observalbe pattern ကိုနားလည်ထားရင်ကောင်းတယ်။

Async မှာ data သည် operation ဥပမာ fileRead လုပ်တာနဲ့ တန်းမရဘူး ဒါမှ data ရတဲ့အချိန်မှာ notify လုပ်ပေးရတယ်၊
ဒါကို reactive programming မှာ subscribe စတာတွေသုံးပြီးဖမ်းတယ်။
နောက် Parallel REST call 2 ခုပေ့ါ ၂ခုလုံးကို Parallel ခေါ်မယ် ဒါဆို callback လိုဆို

first call callback ပြီးမှ နောက်တခုဟာ ေခါ်လို့ရမယ် အဆင်မပြေဘူး async ပုံစံ မသုံးနိုင်ဘူး။
Reactive မှာကျတော့ operator တွေသုံးပြီး တပြိုင်တည်းခေါ်လို့ရတယ် ပြီးမှ ၂ ခုပြီးမှ ပြန်ပေါင်းတာမျိုးရတယ်။
Reactive အဓိပ္ပာယ်က ခုနက stream တွေသည် excel က formula တွေလိုပဲ တခုခု input field ပြောင်းရင် output လိုက်ပြောင်းသလိုမျိုး reactive ဖြစ်တယ်။အဲ့တော့ async မှာ event တခုခုဖြစ်တာနဲ့ chain of reaction လေးတွေလိုက်လုပ်ထားလို့ရတယ်။

ပြောချင်တာက Reactive Programming ကိုနားမလည်တာက
1 Asynchronous programming
2 Functional Programming
အဲ့၂ခုကိုနားမလည်လို့ နားမလည်တာ။

Original link

crd=>(https://m.facebook.com/story.php?story_fbid=pfbid0HYYS5UTGtRbsQ1yyd4Y736LST4RihvqMdcAuCByc6tTVh5LmjiF4ksZeRc4gfh7Kl&id=1819241055&mibextid=Nif5oz)

Link 1=>https://supyaem.home.blog/2023/10/21/non-blocking-io-vs-blocking-io/

Leave a comment