📎 Webclip
[elixir-talk:7975] A question about global state
The thread asks how to keep startup-derived terminal capability state in an Elixir library without checking environment variables on every call. The answers say processes are cheap, so an Agent is fine, and the process can be started by the library’s application instead of by users.
José Valim describes creating a Mix application with a supervisor, starting the Agent as a child, and storing the state behind explicit functions. Robert Virding adds that ETS can hold static data at startup or be managed by a process if the data changes.
Reading notes#
- The original question is about saving terminal capability information after checking environment variables once.
- One reply says processes are cheap enough that using one here should not be a problem.
- Another reply says the library’s agent should be supervised and started by the application’s supervisor.
- José Valim says a Mix application started with
mix new my_app --supalready gives an application callback for startup. - His example starts an Agent as a child and exposes a
get/1function for reading the stored map. - He says this makes global state explicit and easier to control, inspect, debug, and understand.
- Robert Virding says static data can be initialized at startup and stored in ETS.
- He says more dynamic data can also live in ETS, with a process managing updates.
