Normandy.Coordination.SharedContext (normandy v0.2.0)

View Source

Manages 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

key()

@type key() :: String.t() | {String.t(), String.t()}

t()

@type t() :: %Normandy.Coordination.SharedContext{data: map(), metadata: map()}

Functions

delete(context, key)

@spec delete(t(), key()) :: t()

Deletes a key from the context.

Example

context = SharedContext.delete(context, "key")

get(shared_context, key)

@spec get(t(), key()) :: {:ok, term()} | {:error, :not_found}

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")

get(context, key, default)

@spec get(t(), key(), term()) :: term()

Retrieves a value with a default if not found.

Example

value = SharedContext.get(context, "key", "default")

has_key?(shared_context, key)

@spec has_key?(t(), key()) :: boolean()

Checks if a key exists in the context.

Example

SharedContext.has_key?(context, "key")
#=> true

keys(shared_context)

@spec keys(t()) :: [String.t()]

Returns all keys in the context.

Example

keys = SharedContext.keys(context)
#=> ["key1", "agent_1:status"]

merge(shared_context1, shared_context2)

@spec merge(t(), t()) :: t()

Merges data from another context.

Example

context = SharedContext.merge(context1, context2)

new()

@spec new() :: t()

Creates a new shared context.

Example

context = SharedContext.new()

put(context, key, value)

@spec put(t(), key(), term()) :: t()

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")

to_map(shared_context)

@spec to_map(t()) :: map()

Returns all data in the context.

Example

data = SharedContext.to_map(context)
#=> %{"key1" => "value1", "agent_1:status" => "active"}

update(context, key, initial, fun)

@spec update(t(), key(), term(), (term() -> term())) :: t()

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)