RBox
RBox
is a reactive abstraction in F-Box. It encapsulates a single value, supports reactive updates, and provides a functional interface for transforming, combining, and chaining operations.
The RBox
abstraction extends the functionality of Box
by introducing reactivity. It allows you to subscribe to updates, propagate changes to derived RBox
instances, and manage dependencies.
Key Features
- Reactivity: Automatically updates derived
RBox
instances when the original value changes. - Encapsulation: Wraps a value to provide a controlled interface for interactions.
Creating an RBox
To create an RBox
, use the static method RBox.pack
. This method initializes an RBox
instance with a given value. Additionally, you can subscribe to value changes to observe updates in real-time.
Example
This demonstrates how to:
- Create an
RBox
. - Subscribe to value changes.
- Update the value reactively.
- Unsubscribe from updates when no longer needed.
Supported Operators
The RBox
abstraction provides the following operators for working with its encapsulated value. Each operator is an alias for a corresponding method.
<$>
: Alias formap
. Applies a transformation function to the value.<*>
: Alias forapply
. Combines a function in anRBox
with a value in anotherRBox
.>>=
: Alias forflatMap
. Chains computations that return newRBox
instances.
For detailed usage, see API Methods.
API Methods
RBox.pack
RBox.pack(value: T): RBox<T>
Creates a new RBox
instance containing the provided value.
RBox.isRBox
RBox.isRBox(value: any): boolean
Checks if a given value is an RBox
instance.
getValue
getValue(): T
Retrieves the value encapsulated in the RBox
.
setValue
setValue(fn: (value: T) => T): void
Updates the value inside the RBox
and notifies all subscribers.
map
map<U>(fn: (value: T) => U): RBox<U>
Applies a transformation function to the encapsulated value, returning a new derived RBox
.
Alias: <$>
apply
apply<A, B>(this: RBox<(a: A) => B>, boxValue: RBox<A>): RBox<B>
Applies a function wrapped in one RBox
to a value wrapped in another RBox
.
Alias: <*>
flatMap
flatMap<U>(fn: (value: T) => RBox<U>): RBox<U>
Applies a function returning an RBox
to the value inside this RBox
and flattens the result.
Alias: >>=
subscribe
subscribe(observer: Observer<T>): string
Subscribes to updates of the RBox
value.
unsubscribe
unsubscribe(key: string): void
Unsubscribes from updates using the subscription key.
unsubscribeAll
unsubscribeAll(): void
Unsubscribes all observers from the RBox
.
detach
detach(): void
Detaches the RBox
from all dependencies and stops updates to derived RBox
instances.
Use Cases
Reactive Transformations
Automatically update derived RBox
values when the original changes:
Learn more about how map
enables reactive transformations.
Combining RBoxes
Combine multiple RBox
instances using the <*>
operator:
Explore the apply
method for combining RBox
instances.
Chaining Reactive Updates
Chain multiple reactive updates into a pipeline:
Discover how flatMap
simplifies chaining reactive updates.
Why Use RBox?
The RBox
abstraction provides a robust way to manage reactive state with minimal boilerplate. Its reactivity and functional interface make it an ideal tool for scenarios requiring dynamic updates and efficient state propagation.
Next Steps
Explore other abstractions built on Box
and RBox
: