MingaAgent.BufferForkStore (Minga v0.1.0)

Copy Markdown View Source

Holds the mapping of file paths to Buffer.Fork pids for an agent session.

Each agent session gets its own store. Forks are created lazily on the first write to a file that has an open buffer. The store monitors each fork and removes it if the fork process dies.

The store is an Agent wrapping a map and a list of monitor refs. Tool callbacks (which run in a spawned Task) query and update it via the public API.

Lifecycle

{:ok, store} = BufferForkStore.start_link()
{:ok, fork_pid} = BufferForkStore.get_or_create(store, "/abs/path/lib/foo.ex", buffer_pid)
forks = BufferForkStore.all(store)
BufferForkStore.stop(store)

Summary

Types

The store's internal state.

Functions

Returns all forks as a %{path => fork_pid} map.

Returns a specification to start this module under a supervisor.

Discards all forks without merging.

Returns the fork pid for path, or nil if no fork exists.

Returns the fork pid for path, creating one from buffer_pid if needed.

Merges all dirty forks back to their parent buffers. Returns results per path.

Starts a new empty fork store.

Stops the store, cleaning up all forks.

Types

state()

@type state() :: %{
  forks: %{required(String.t()) => pid()},
  monitors: %{required(reference()) => String.t()}
}

The store's internal state.

Functions

all(store)

@spec all(GenServer.server()) :: %{required(String.t()) => pid()}

Returns all forks as a %{path => fork_pid} map.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

discard_all(store)

@spec discard_all(GenServer.server()) :: :ok

Discards all forks without merging.

get(store, path)

@spec get(GenServer.server(), String.t()) :: pid() | nil

Returns the fork pid for path, or nil if no fork exists.

get_or_create(store, path, buffer_pid)

@spec get_or_create(GenServer.server(), String.t(), pid()) ::
  {:ok, pid()} | {:error, term()}

Returns the fork pid for path, creating one from buffer_pid if needed.

If a fork already exists for this path, returns it. Otherwise creates a new fork from the parent buffer, stores it, and returns the pid.

merge_all(store)

@spec merge_all(GenServer.server()) :: [
  {String.t(), :ok | {:conflict, term()} | {:error, term()}}
]

Merges all dirty forks back to their parent buffers. Returns results per path.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts a new empty fork store.

stop(store)

@spec stop(GenServer.server()) :: :ok

Stops the store, cleaning up all forks.