SynchronizedRef<A> serves as a mutable reference to a value of type A.
With it, we can store immutable data and perform updates atomically and effectfully.
The distinctive function in SynchronizedRef is updateEffect.
This function takes an effectful operation and executes it to modify the shared state.
This is the key feature setting SynchronizedRef apart from Ref.
In real-world applications, SynchronizedRef is useful when you need to execute effects, such as querying a database, and then update shared state based on the result. It ensures that updates happen sequentially, preserving consistency in concurrent environments.
Example (Concurrent Updates with SynchronizedRef)
In this example, we simulate fetching user ages concurrently and updating a shared state that stores the ages: