Powered by AppSignal & Oban Pro

ReactiveStruct demo

content/reactive_struct.livemd

ReactiveStruct demo

Mix.install([
  {:kino, "~> 0.17.0"},
  {:reactive_struct, github: "chgeuer/reactive_struct"}
], force: true)

Section

defmodule Calculator do
  use ReactiveStruct, 
    allow_updating_computed_fields: false

  defstruct ~w(a0 a b sum product)a

  computed(:a, fn %{a0: a0} -> a0 + 1 end)
  
  computed :sum, fn %{a: a, b: b} -> a + b end
  
  computed :product, fn %{a: a, b: b} -> a * b end
end
Calculator.mermaid()
|> Kino.Mermaid.new()
Calculator.new(a0: 2, b: 3)
|> IO.inspect(label: :a)
|> Calculator.merge(a0: 5)
|> IO.inspect(label: :b)  
|> Calculator.merge(product: 5)
|> IO.inspect(label: :c)  
defmodule Chain do
  use ReactiveStruct, allow_setting_computed_fields: false

  defstruct ~w[base base2 step1 step2 final]a

  computed(:step1, fn %{base: a, base2: b} -> a * 2 + b end)
  computed(:step2, fn %{step1: x} -> x + 10 end)
  computed(:final, fn %{step2: x} -> x * x end)
end

Chain.new(base: 2, base2: 3)

chain = %{final: 289} = Chain.new(base: 2, base2: 3)
chain = %{final: 529} = Chain.merge(chain, base: 5)

# Chain.merge(chain, step2: 5)

Kino.Mermaid.new(Chain.mermaid())