Vault is a lightweight Elixir library for immutable data storage within a process subtree.
Due to Elixir's actor model nature, it's common for a process to have global context that is valid for every function call inside of the process and its children.
For example, this context can include:
Vault.init/1 provides you a guarantee that the context can only be defined once per existing process subtree, so you won't override it by accident. This makes it easy to reason about your context origination.
# Initialize vault in parent process
Vault.init(current_user: %{id: 1, first_name: "Alice", role: "admin"})
# Access data from any descendant process, even these not linked!
spawn(fn ->
Vault.get(:current_user) # => %{id: 1, first_name: "Alice", role: "admin"}
Vault.init(current_user: :user) # => raises, because the ancestor already has vault initialized
end)
# Access data from the parent process itself
Vault.get(:current_user) # => %{id: 1, first_name: "Alice", role: "admin"}
However, if for some reason you need to split initializations, you can use Vault.unsafe_merge/1, but the immutability is no longer guaranteed.
unsafe_* functions).Map module for familiar data access.ProcessTree library by JB Steadman, which does all the heavy lifting of traversing process trees and propagating data back. You can read more about how ancestors are fetched in this amazing blog post by the library's author.def deps do
[
{:vault, "~> 0.2.1"}
]
end
8 commits
Elixir
100.0%
Vault is a lightweight Elixir library for immutable data storage within a process subtree.
Due to Elixir's actor model nature, it's common for a process to have global context that is valid for every function call inside of the process and its children.
For example, this context can include:
Vault.init/1 provides you a guarantee that the context can only be defined once per existing process subtree, so you won't override it by accident. This makes it easy to reason about your context origination.
# Initialize vault in parent process
Vault.init(current_user: %{id: 1, first_name: "Alice", role: "admin"})
# Access data from any descendant process, even these not linked!
spawn(fn ->
Vault.get(:current_user) # => %{id: 1, first_name: "Alice", role: "admin"}
Vault.init(current_user: :user) # => raises, because the ancestor already has vault initialized
end)
# Access data from the parent process itself
Vault.get(:current_user) # => %{id: 1, first_name: "Alice", role: "admin"}
However, if for some reason you need to split initializations, you can use Vault.unsafe_merge/1, but the immutability is no longer guaranteed.
unsafe_* functions).Map module for familiar data access.ProcessTree library by JB Steadman, which does all the heavy lifting of traversing process trees and propagating data back. You can read more about how ancestors are fetched in this amazing blog post by the library's author.def deps do
[
{:vault, "~> 0.2.1"}
]
end
8 commits
Elixir
100.0%