Normandy.Coordination.SharedContext (normandy v0.2.0)
View SourceManages shared context between multiple agents.
SharedContext provides a key-value store that multiple agents can read from and write to, enabling information sharing across agent boundaries.
Example
# Create shared context
context = SharedContext.new()
# Store data
context = SharedContext.put(context, "research_results", data)
# Retrieve data
{:ok, data} = SharedContext.get(context, "research_results")
# Store with namespace
context = SharedContext.put(context, {"agent_1", "status"}, "processing")
Summary
Functions
Deletes a key from the context.
Retrieves a value from the shared context.
Retrieves a value with a default if not found.
Checks if a key exists in the context.
Returns all keys in the context.
Merges data from another context.
Creates a new shared context.
Stores a value in the shared context.
Returns all data in the context.
Updates a value using a function.
Types
Functions
Deletes a key from the context.
Example
context = SharedContext.delete(context, "key")
Retrieves a value from the shared context.
Returns {:ok, value} if found, {:error, :not_found} otherwise.
Examples
{:ok, value} = SharedContext.get(context, "key")
{:error, :not_found} = SharedContext.get(context, "missing")
Retrieves a value with a default if not found.
Example
value = SharedContext.get(context, "key", "default")
Checks if a key exists in the context.
Example
SharedContext.has_key?(context, "key")
#=> true
Returns all keys in the context.
Example
keys = SharedContext.keys(context)
#=> ["key1", "agent_1:status"]
Merges data from another context.
Example
context = SharedContext.merge(context1, context2)
@spec new() :: t()
Creates a new shared context.
Example
context = SharedContext.new()
Stores a value in the shared context.
Examples
# Simple key
context = SharedContext.put(context, "key", "value")
# Namespaced key
context = SharedContext.put(context, {"agent_1", "status"}, "active")
Returns all data in the context.
Example
data = SharedContext.to_map(context)
#=> %{"key1" => "value1", "agent_1:status" => "active"}
Updates a value using a function.
If the key doesn't exist, uses the initial value.
Example
context = SharedContext.update(context, "counter", 0, fn count -> count + 1 end)