MingaAgent.Session.SubscriberLifecycle (Minga v0.1.0)

Copy Markdown View Source

Owns subscriber roles, monitor identity, and detached-session reclamation.

Session performs every process and timer effect. This value validates the operation, returns the next lifecycle or a named preparation, and accepts the resulting OTP reference through a completion transition.

Transition table

CommandValid sourceResult
subscribevalid role, PID absentmonitor preparation
complete subscribeunchanged preparation sourceattachment installed, prior timer returned for cancellation
claim driverattached PID, driver vacant or same PIDrole transition
detachattached PIDattachment removed with monitor cleanup identity
DOWNmatching PID and monitor referenceattachment removed
reconcile reclaimdetached and turn reclaimabletimer preparation or unchanged
complete reclaim scheduleunchanged preparation sourceexact OTP timer reference installed
reclaim timeoutmatching timer, detached, turn reclaimablereclaim accepted
stopanyempty lifecycle plus ordered timer and monitor cleanup identity

Summary

Types

A validated reclaim schedule awaiting timer creation.

Remote attachment role.

Role outcome produced by installing a new subscriber.

A validated subscription awaiting monitor creation.

t()

Subscriber lifecycle state.

Functions

Claims a vacant driver role for an attached subscriber.

Installs an OTP reclaim timer when the validated source is still current.

Installs a subscription monitor when the validated source is still current.

Returns the default role for a new subscriber.

Detaches a subscriber and returns its monitor identity for cleanup.

Returns the active driver derived from canonical attachment roles.

Returns true when the PID owns the driver role.

Builds an empty detached lifecycle.

Validates a subscription before Session creates its process monitor.

Accepts only the installed timer identity and rechecks reclaim eligibility.

Returns the installed reclaim timer reference.

Reconciles the installed reclaim timer with attachment and turn eligibility.

Returns the role for an attached subscriber.

Clears lifecycle ownership and returns timer then monitor cleanup identities.

Consumes a subscriber DOWN only when both PID and monitor identity match.

Returns all attached subscriber PIDs.

Types

reclaim_preparation()

@type reclaim_preparation() :: t()

A validated reclaim schedule awaiting timer creation.

role()

Remote attachment role.

subscription_change()

@type subscription_change() ::
  :driver_initialized | :driver_changed | :viewer_attached

Role outcome produced by installing a new subscriber.

subscription_preparation()

@type subscription_preparation() :: {source :: t(), pid(), role()}

A validated subscription awaiting monitor creation.

t()

@type t() :: %MingaAgent.Session.SubscriberLifecycle{
  attachments: %{required(pid()) => MingaAgent.Session.SubscriberAttachment.t()},
  reclaim_timer: reference() | nil
}

Subscriber lifecycle state.

Functions

claim_driver(lifecycle, pid)

@spec claim_driver(t(), pid()) ::
  {:changed, t()} | :unchanged | {:error, :driver_taken | :not_subscribed}

Claims a vacant driver role for an attached subscriber.

complete_reclaim_schedule(lifecycle, source, timer_ref)

@spec complete_reclaim_schedule(t(), reclaim_preparation(), reference()) ::
  {:ok, t()}
  | {:error,
     :stale_preparation | :timer_already_installed | :subscriber_attached}

Installs an OTP reclaim timer when the validated source is still current.

complete_subscribe(lifecycle, arg, monitor_ref)

@spec complete_subscribe(t(), subscription_preparation(), reference()) ::
  {:ok, t(), subscription_change(), timer_to_cancel :: reference() | nil}
  | {:error, :stale_preparation | :already_attached | :driver_taken}

Installs a subscription monitor when the validated source is still current.

default_role(lifecycle)

@spec default_role(t()) :: role()

Returns the default role for a new subscriber.

detach(lifecycle, pid)

@spec detach(t(), pid()) ::
  {:removed, t(), MingaAgent.Session.SubscriberAttachment.t()} | :unchanged

Detaches a subscriber and returns its monitor identity for cleanup.

driver(subscriber_lifecycle)

@spec driver(t()) :: pid() | nil

Returns the active driver derived from canonical attachment roles.

driver?(lifecycle, pid)

@spec driver?(t(), pid()) :: boolean()

Returns true when the PID owns the driver role.

new()

@spec new() :: t()

Builds an empty detached lifecycle.

prepare_subscribe(lifecycle, pid, role)

@spec prepare_subscribe(t(), pid(), term()) ::
  {:monitor, subscription_preparation()}
  | {:already_attached, t()}
  | {:error, :invalid_role}

Validates a subscription before Session creates its process monitor.

reclaim_timeout(lifecycle, timer_ref, turn_reclaimable?)

@spec reclaim_timeout(t(), reference(), boolean()) ::
  {:reclaim, t()} | {:keep, t()} | {:error, :stale_timer}

Accepts only the installed timer identity and rechecks reclaim eligibility.

reclaim_timer(subscriber_lifecycle)

@spec reclaim_timer(t()) :: reference() | nil

Returns the installed reclaim timer reference.

reconcile_reclaim(lifecycle, enabled?, turn_reclaimable?)

@spec reconcile_reclaim(t(), boolean(), boolean()) ::
  {:start_timer, reclaim_preparation()}
  | {:cancel_timer, reference(), t()}
  | :unchanged

Reconciles the installed reclaim timer with attachment and turn eligibility.

role(subscriber_lifecycle, pid)

@spec role(t(), pid()) :: role() | nil

Returns the role for an attached subscriber.

stop(lifecycle)

@spec stop(t()) ::
  {t(), timer_ref :: reference() | nil, monitor_refs :: [reference()]}

Clears lifecycle ownership and returns timer then monitor cleanup identities.

subscriber_down(lifecycle, pid, monitor_ref)

@spec subscriber_down(t(), pid(), reference()) ::
  {:removed, t(), MingaAgent.Session.SubscriberAttachment.t()}
  | {:error, :stale_monitor}

Consumes a subscriber DOWN only when both PID and monitor identity match.

subscribers(subscriber_lifecycle)

@spec subscribers(t()) :: [pid()]

Returns all attached subscriber PIDs.