Globals
Globals is a module that provides ETS backed global state storage with PubSub capabilities.
How this is different from other PubSub implementations:
- Support PubSub patterns and value storage together
- keys can be any() term such as atoms, strings, tuples, etc.
-
Application starts
Globalsautomatically no additional Supervision tree entry required -
Support for
depends_onfor variable dependencies awaitandupdate_awaitfor awaiting async updates
Usage
Simple key value storage:
Globals.set(:key, "value")
Globals.get(:key)PubSub pattern:
Globals.subscribe(:key)
Globals.put(:key, "value")
receive do
{:update, :key, "value"} -> :ok
end
:okCaching and PubSub within an Ecto / Phoenix LiveView context:
Especially in LiveView apps it's important to keep track of state changes and provide a live updating UI. Globals provides a simple way to do this.
def MyApp.Repo do
use Ecto.Repo, otp_app: :my_app
def init() do
Globals.register(:complex_query, fn ->
query("SELECT SUM(age) FROM users GROUP BY city")
end)
def insert(user_name, user_age, user_city) do
Repo.query("INSERT INTO users (name, age, city) VALUES (?, ?, ?)", [user_name, user_age, user_city])
Globals.update(:complex_query)
end
end
def MyAppWeb.LiveView do
def mount() do
Globals.subscribe(:complex_query)
{:ok, assign(socket, :complex_query, Globals.await(:complex_query))}
end
def handle_info({:update, :complex_query, value}, socket) do
{:noreply, assign(socket, :complex_query, value)}
end
def render(assigns) do
~H"""
... render ...
"""
end
endInstallation
The package can be installed by adding globals to your list of dependencies in mix.exs:
def deps do
[
{:globals, "~> 1.0"}
]
endThe docs can be found at https://hexdocs.pm/globals.