Angular Signals और RxJS दोनों Angular में reactivity handle करने के core tools हैं, लेकिन उनका purpose, mental model और usage patterns fundamentally अलग हैं। Modern Angular में यह समझना बहुत जरूरी है कि Signals कब use करें और RxJS कब, ताकि application simple भी रहे और scalable भी।
इस chapter में हम Angular Signals और RxJS का deep, practical और comparative study करेंगे, real-world scenarios के साथ।
Reactivity का Concept
Reactivity का मतलब है कि data change होते ही dependent UI या logic automatically update हो जाए।
Angular में historically RxJS इस reactivity का foundation रहा है। Signals Angular का newer reactive primitive है जो fine-grained और synchronous reactivity provide करता है।
Angular Signals क्या हैं
Signals एक reactive value होती है।
count = signal(0);
Value read करने के लिए function call किया जाता है:
count();
Update करने के लिए:
count.set(5);
count.update(v => v + 1);
Signals synchronous और deterministic होते हैं।
RxJS क्या है
RxJS asynchronous data streams के लिए library है।
count$ = new BehaviorSubject<number>(0);
count$.next(5);
Consume करने के लिए:
count$.subscribe(v => console.log(v));
RxJS powerful है लेकिन mental overhead ज्यादा है।
Core Difference Summary
Signals:
- Synchronous
- Pull-based reactivity
- Simple API
- Fine-grained updates
RxJS:
- Asynchronous
- Push-based streams
- Powerful operators
- Complex workflows
Mental Model Difference
Signals:
- Value + dependencies automatically tracked
- No manual subscription management
- Read triggers dependency tracking
RxJS:
- Stream of values over time
- Manual subscription lifecycle
- Operator-based transformations
Signals thinking is closer to variables, RxJS closer to events.
Change Detection Behavior
Signals:
- Only dependent views update
- Fine-grained change detection
- Less unnecessary re-render
RxJS:
- Works with async pipe or manual subscribe
- Change detection triggered per emission
Signals performance advantage UI-heavy apps में noticeable होता है।
Derived State Comparison
Signals:
total = computed(() => this.count() * 2);
RxJS:
total$ = this.count$.pipe(map(v => v * 2));
Signals syntax simple है, RxJS more expressive है।
Side Effects Handling
Signals use effect().
effect(() => {
console.log(this.count());
});
RxJS use tap().
this.count$.pipe(
tap(v => console.log(v))
).subscribe();
RxJS side effects async flows में ज्यादा controlled होते हैं।
Async Operations
Signals:
- Async support indirect
- Promises + manual updates
fetchData() {
fetch('/api').then(r => r.json()).then(data => {
this.data.set(data);
});
}
RxJS:
- Async operations native
- cancelation support
- retry, debounce, switchMap
this.http.get('/api').pipe(
retry(3)
);
Async-heavy scenarios में RxJS clearly better है।
Error Handling
Signals:
- No built-in error channel
- Error state manually store करनी पड़ती है
RxJS:
- Dedicated error channel
- catchError, retry
Complex error flows RxJS में easy हैं।
State Management Use Case
UI state:
- Signals preferred
loading = signal(false);
Data streams:
- RxJS preferred
users$ = this.http.get<User[]>('/users');
Hybrid approach most common है।
Template Consumption
Signals:
<p>{{ count() }}</p>
RxJS:
<p>{{ count$ | async }}</p>
Signals templates cleaner बनाते हैं।
Memory Management
Signals:
- No subscriptions
- Auto-cleanup
RxJS:
- Manual unsubscribe या async pipe
- Memory leaks risk
Signals beginner-friendly हैं।
Testing Comparison
Signals:
service.increment();
expect(service.count()).toBe(1);
RxJS:
service.count$.subscribe(v => expect(v).toBe(1));
Signals tests simpler होते हैं।
When to Use Signals
Signals use करें जब:
- UI state manage करनी हो
- Component-level state हो
- Simple derived values हों
- Performance critical UI हो
When to Use RxJS
RxJS use करें जब:
- HTTP calls
- WebSocket streams
- Complex async flows
- Cancellation, retries, debouncing चाहिए
Signals + RxJS Together
Best Angular apps दोनों use करती हैं।
data = signal<User[]>([]);
this.http.get<User[]>('/users').subscribe(users => {
this.data.set(users);
});
RxJS async data fetch करता है, Signals UI state manage करते हैं।
Migration Strategy
Existing RxJS code को replace करना जरूरी नहीं है।
Approach:
- New UI logic Signals से लिखें
- Existing streams RxJS में रहने दें
- Gradual adoption करें
Common Mistakes
- Signals से async workflows बनाना
- RxJS overuse for simple state
- Mixing paradigms without clarity
Real-World Recommendation
Angular Signals vs RxJS कोई competition नहीं है।
Signals:
- UI reactivity tool
RxJS:
- Async data flow engine
Angular का future hybrid model पर based है जहाँ Signals और RxJS दोनों अपनी-अपनी strength में use होते हैं। अगले chapter में हम Angular Signals को deep dive में cover करेंगे, जहाँ reactive rendering और advanced signal patterns discuss होंगे।
