SubscriptionRef<A> is a SynchronizedRef that can be subscribed to in order to receive the current value as well as all changes to that value. It combines mutable state management with reactive streaming.
Overview
SubscriptionRef is ideal for reactive state that multiple consumers need to observe. It provides:
- Mutable state with atomic updates
- Change notification via streams
- Current value access for new subscribers
- Type-safe updates with Effect integration
Creating a SubscriptionRef
Reading Values
Current Value
Subscribe to Changes
Updating Values
Direct Updates
Get-and-Update
Effectful Updates
Conditional Updates
Update Some
Get and Update Some
Modify Operations
Transform and Return
Modify with Effects
Modify Some
Multiple Subscribers
Practical Examples
Counter with Live Updates
State Machine
Configuration Updates
Live Dashboard
Comparison with Ref
| Feature | Ref | SubscriptionRef |
|---|---|---|
| Atomic updates | ✅ | ✅ |
| Get current value | ✅ | ✅ |
| Subscribe to changes | ❌ | ✅ |
| Stream of values | ❌ | ✅ |
| Multiple observers | Manual | Built-in |
| Overhead | Lower | Higher (pubsub) |
Performance Considerations
- Use for observed state - If nothing subscribes to changes, use regular
Ref - Backpressure aware - The
changesstream respects backpressure - Multiple subscribers - Each subscriber gets independent stream
- Memory - Subscribers hold references; interrupt fibers when done
See Also
- Ref - Simple mutable references
- SynchronizedRef - Refs with effectful updates
- Stream - Reactive streams
- PubSub - Underlying publish-subscribe mechanism