Skip to content
Oeiuwq Faith Blog OpenSource Porfolio

Qqwy/elixir_gen_frp

null

Qqwy/elixir_gen_frp.json
{
"createdAt": "2016-12-19T19:35:43Z",
"defaultBranch": "master",
"description": null,
"fullName": "Qqwy/elixir_gen_frp",
"homepage": null,
"language": "Elixir",
"name": "elixir_gen_frp",
"pushedAt": "2017-01-23T15:24:57Z",
"stargazersCount": 17,
"topics": [],
"updatedAt": "2024-05-17T16:46:12Z",
"url": "https://github.com/Qqwy/elixir_gen_frp"
}

GenFRP is a library that allows for programming in elixir using the Functional Reactive Programming method.

This is a way to store and dispatch information that is very readable and extensible.

The idea is as follows:

  1. Your FRP module contains use GenFRP, initial_state: some_useful_starting_state. (some_useful_starting_state can be anything you wish, of course)
  2. It implements the update(state, event) :: state function.
  3. It implements the render(state, last_rendered_state) :: any function.

update is called whenever an event is sent to the GenFRP process by using GenFRP.send_event(frp_pid, whatever_you_want_to_send).

render is called whenever you want to know its current state (in some representation format that is useful for the outside world, i.e. rendered). GenFRP uses a very simple caching mechanism to ensure that render is only invoked when the internal state has changed since its last invocation.

Here is a very simple example (which can be found as GenFRP.Example.Map as well)

defmodule GenFRP.Example.Map do
use GenFRP
def init, do: %{}
def update(state, [{key, val}]) do
Map.put(state, key, val)
end
def update(state, event) do
IO.puts "Unrecognized event passed to `#{inspect(__MODULE__)}.update/2`: #{event}"
end
@doc """
Returns the key-value pairs as strings, separated over multiple lines, in alphabetical order.
"""
def render(state, last_rendered_state) do
Enum.map(state, fn {key, val} ->
"`#{key}`: `#{val}`"
end)
end
Enum.join("\n")
end

This can be used as follows:

{:ok, pid} = GenFRP.start_link(GenFRP.Example.Map)
# ... maybe some other code here...
GenFRP.send_event(pid, {:foo, :bar})
# ... maybe some more code here...
GenFRP.send_event(pid, {:baz, 42})
# ... maybe yet some other code here...
GenFRP.render(pid)
"`foo`: `bar`
`baz`: `42`
"

The package can be installed by adding gen_frp to your list of dependencies in mix.exs:

def deps do
[{:gen_frp, "~> 0.5.0"}]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/gen_frp.

  • 0.5.0 Changed the way the initial state works to init/0, to follow the naming convention that GenServer et al. already use. Also, documentation was improved.
  • 0.1.0 Initial released version.