RxJS (ย่อมาจาก Reactive Extensions for JavaScript) คือ ไลบรารีสำหรับจัดการ asynchronous data stream อย่างมีประสิทธิภาพและยืดหยุ่นมาก โดยใช้แนวคิดของ Reactive Programming
🧠 แนวคิดหลักของ RxJS
🔄 "Everything is a stream"
ข้อมูลต่าง ๆ ไม่ว่าจะเป็น click, typing, HTTP response, หรือ timer — ล้วนเป็น stream ได้
Stream คือ ลำดับของค่าที่เกิดขึ้นตามเวลา
🔧 RxJS ใช้ทำอะไร?
การใช้งานทั่วไป | ตัวอย่าง |
---|---|
จัดการ event อย่างซับซ้อน | พวก click หลายครั้ง, debounce |
ควบคุม API call | cancel request, retry |
สร้าง pipeline ที่สามารถเปลี่ยนแปลง/กรองข้อมูล | map, filter, merge, etc. |
ผสานหลาย stream เข้าด้วยกัน | combineLatest, merge, zip |
Reactive UI / Form | ทำงานร่วมกับ Angular Reactive Forms ได้ดี |
🧱 ส่วนประกอบหลักของ RxJS
ส่วนประกอบ | ความหมาย |
---|---|
Observable | ตัวแทนของ stream (เหมือน Promise ที่ส่งข้อมูลได้หลายค่า) |
Observer | ผู้ที่ subscribe เพื่อรับข้อมูลจาก Observable |
Operators | ฟังก์ชันที่เปลี่ยนแปลง stream เช่น map , filter , debounceTime , mergeMap
|
Subject | เป็นทั้ง Observable และ Observer (ใช้ส่งข้อมูลจาก component ไปยังอื่นๆ) |
Subscription | ตัวแทนของการเชื่อมต่อ (subscribe) ที่สามารถ unsubscribe ได้ |
📦 ตัวอย่างใช้งานง่ายๆ
🔔 สร้าง Observable และ subscribe
import { Observable } from 'rxjs'
const observable = new Observable((subscriber) => {
subscriber.next('Hello')
subscriber.next('World')
subscriber.complete()
})
observable.subscribe({
next: (val) => console.log(val),
complete: () => console.log('Done'),
})
// 👉 Output:
// Hello
// World
// Done
⌨️ ใช้กับ DOM event
import { fromEvent } from 'rxjs'
const clicks$ = fromEvent(document, 'click')
clicks$.subscribe(event => console.log(event))
🔄 ใช้ Operator เช่น map และ filter
import { of } from 'rxjs'
import { map, filter } from 'rxjs/operators'
of(1, 2, 3, 4, 5).pipe(
filter(x => x % 2 === 1),
map(x => x * 10)
).subscribe(val => console.log(val))
// 👉 Output: 10, 30, 50
📍 RxJS นิยมใช้ในไหน?
- ✅ Angular: ใช้ RxJS เป็นแกนหลักของ HTTP, Form, Router
- ✅ ใช้ใน React ได้ด้วย โดยใช้ร่วมกับ rxjs-hooks หรือ state management แบบ reactive
- ✅ ใช้ใน Node.js / Backend ที่ต้องการจัดการ async หลายแหล่ง
🔚 สรุป
คำถาม | คำตอบสั้น |
---|---|
RxJS คืออะไร | ไลบรารีสำหรับจัดการ asynchronous data ด้วยแนวคิด reactive programming |
ใช้ทำอะไร | จัดการ event, async stream, UI updates, API call ฯลฯ |
เหมาะกับใคร | คนที่เจอ async/stream หลายอย่างซ้อนกัน และต้องการเขียนโค้ดให้สะอาด |
RxJS กับ Redux Thunk มีเป้าหมายและแนวคิดต่างกัน แม้ทั้งคู่จะใช้ในการจัดการ asynchronous logic ได้เหมือนกันก็ตาม
🧠 เปรียบเทียบโดยสรุป: RxJS vs Redux Thunk
คุณสมบัติ | Redux Thunk | RxJS |
---|---|---|
แนวคิดหลัก | Imperative + async/await | Reactive Programming (ทุกอย่างคือ stream) |
ใช้งาน | จัดการ async logic ใน Redux (เช่น fetch API) | จัดการ stream ที่ต่อเนื่อง เช่น event, input, timer |
เหมาะกับ | งานที่ async ง่ายๆ เช่น API call ใน Redux | งานที่ซับซ้อน เช่น auto-complete, live search, cancel request, UI ที่เปลี่ยนตามเวลา |
syntax | ใช้ async/await, if/else, try/catch ง่าย | ใช้ operators เช่น map , filter , switchMap , ต้องเรียนรู้เพิ่มเติม |
integration | ทำงานเฉพาะใน Redux (กับ store และ dispatch) | ใช้ได้ทุกที่: React, Angular, Node.js, ไม่ต้องใช้ Redux |
learning curve | ง่ายกว่า | ยากกว่า |
cancel async ได้ไหม | ยากหรือทำไม่ได้ | ทำได้ง่าย (ผ่าน switchMap , takeUntil , etc.) |
ใช้ร่วมกับ framework | React + Redux | Angular (native), React (ได้ แต่ต้องวางแผนดี) |
📦 ตัวอย่างงานที่เหมาะกับแต่ละตัว
✅ เหมาะกับ Redux Thunk
งานที่เหมาะสม | เหตุผล |
---|---|
โหลดข้อมูลจาก API | ใช้ async/await ได้ง่าย |
จัดการ auth เช่น login/logout | ง่ายต่อการจัด flow และ dispatch หลาย action |
UI สถานะ loading/error/success ที่ไม่ซับซ้อน | เขียน reducer ได้ตรงไปตรงมา |
Form submit ที่ทำงานทีละรอบ | ไม่ต้อง cancel หรือ debounce |
✅ เหมาะกับ RxJS
งานที่เหมาะสม | เหตุผล |
---|---|
Auto-complete หรือ search ที่ debounce และ cancel previous request | ใช้ debounceTime + switchMap ง่ายมาก |
Live data / real-time UI | ใช้ interval , websocket , merge ได้ดี |
ดักจับ input หลายอย่างพร้อมกัน เช่น keydown + mouse + API + timer | ผสาน stream ด้วย combineLatest , merge , zip
|
Cancel async ได้ง่าย | ทำได้ผ่าน stream control |
ต้อง reuse pipeline หลายจุด | เขียน operator chain ไว้ใช้งานซ้ำได้ |
💡 ถ้า logic มีลักษณะ "หลาย async ซ้อนกัน", "ต้อง cancel บางอัน", หรือ "ควบคุม event หลายแหล่งพร้อมกัน", RxJS เหมาะกว่า
ลักษณะโปรเจกต์ของคุณ | แนะนำใช้ |
---|---|
API call ธรรมดา, login/logout, loading/error ง่ายๆ | Redux Thunk |
มี live search, debounce, cancel async, stream ซับซ้อน | RxJS |
ใช้ Angular | RxJS โดยธรรมชาติ |
ใช้ React + Redux | เริ่มจาก Thunk, ถ้า logic ซับซ้อนค่อยพิจารณา RxJS |
Top comments (0)