Qqwy/elixir_gen_frp
{ "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
Section titled “GenFRP”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:
- Your FRP module contains
use GenFRP, initial_state: some_useful_starting_state. (some_useful_starting_statecan be anything you wish, of course) - It implements the
update(state, event) :: statefunction. - It implements the
render(state, last_rendered_state) :: anyfunction.
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")endThis 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`"Installation
Section titled “Installation”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"}]endDocumentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/gen_frp.
Changelog
Section titled “Changelog”- 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.