View Source Reactive (Reactive State v0.1.1)
Module to manage reactive state by using GenServer processes ("reactive process" from here on) to manage each piece of state and its relationships to other reactive processes
Installation
The package can be installed by adding reactive_state
to your list of dependencies in mix.exs
:
def deps do
[
{:reactive_state, "~> 0.1.1"}
]
end
Examples
Working with data directly with Reactive.Ref
iex> use Reactive
iex> ref = Ref.new(0) #PID<0.204.0>
iex> Ref.get(ref) # or Ref.get(ref)
0
iex> Ref.set(ref, 1)
:ok
iex> Ref.get(ref)
1
Reactive Block
iex> use Reactive
iex> ref = Ref.new(2)
iex> ref_squared = reactive do
...> get(ref) ** 2
...> end
iex> Reactive.get(ref_squared)
4
iex> Ref.set(ref, 3)
iex> Reactive.get(ref_squared)
9
Conditional Branches
iex> use Reactive
iex> if_false = Ref.new(1)
iex> if_true = Ref.new(2)
iex> toggle = Ref.new(false)
iex> computed = reactive do
...> if get(toggle) do
...> get(if_true)
...> else
...> get(if_false)
...> end
...> end
iex> Reactive.get(computed)
1
iex> Ref.set(toggle, true)
:ok
iex> Reactive.get(computed)
2
iex> # Now, updating `if_false` will not require a recomputation
iex> Ref.set(if_false, 0)
:ok
iex> Reactive.get_cached(computed)
2
iex> # Updating `if_true` will require a recomputation
iex> Ref.set(if_true, 3)
:ok
iex> Reactive.get_cached(computed)
:stale
iex> Reactive.get(computed)
3
Summary
Functions
Returns a specification to start this module under a supervisor.
Retrieve the state of a reactive process
Retrieve the state of a reactive process, and register the current process as dependent of that process, with the call ID of the current process.
You should use the Reactive.reactive
macro to manage reactive relationships instead
Retrieve the cached state of a reactive process, or :stale if it has not been computed or is stale
Create a reactive process using a method
Syntatic sugar for creating reactive blocks
Replace a reactive process's computation method
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Retrieve the state of a reactive process
Example
iex> ref = Reactive.new(fn _ -> 0 end)
iex> Reactive.get(ref)
0
Retrieve the state of a reactive process, and register the current process as dependent of that process, with the call ID of the current process.
You should use the Reactive.reactive
macro to manage reactive relationships instead
Example
iex> use Reactive
iex> ref = reactive do
...> 0
...> end
iex> Reactive.get(ref)
0
Retrieve the cached state of a reactive process, or :stale if it has not been computed or is stale
Example
iex> use Reactive
iex> ref = reactive do
...> 0
...> end
iex> Reactive.get_cached(ref)
:stale
iex> Reactive.get(ref)
0
iex> Reactive.get_cached(ref)
0
Create a reactive process using a method
iex> use Reactive
iex> ref = Ref.new(2)
iex> ref_squared = Reactive.new(fn call_id ->
...> Reactive.get(ref, call_id) ** 2
...> end)
iex> Reactive.get(ref_squared)
4
Syntatic sugar for creating reactive blocks
iex> use Reactive
iex> ref = Ref.new(2)
iex> ref_squared = reactive do
...> get(ref) ** 2
...> end #PID<0.204.0>
iex> Reactive.get(ref_squared)
4
iex> Ref.set(ref, 3)
iex> Reactive.get(ref_squared)
9
Using the reactive
macro in this way is roughly equivalent to:
iex> use Reactive
iex> ref = Ref.new(2)
iex> ref_squared = Reactive.new(fn call_id ->
...> Reactive.get(ref, call_id) ** 2
...> end)
iex> Reactive.get(ref_squared)
4
iex> Ref.set(ref, 3)
iex> Reactive.get(ref_squared)
9
Replace a reactive process's computation method
iex> use Reactive
iex> ref = reactive do
...> 0
...> end
iex> Reactive.get(ref)
0
iex> Reactive.set(ref, fn _ -> 1 end)
:ok
iex> Reactive.get(ref)
1