Lightning.Collaboration.Session (Lightning v2.15.0-pre5)

View Source

Manages individual user sessions for collaborative workflow editing.

A Session acts as a bridge between a parent process (typically a Phoenix channel) and a SharedDoc process that manages the Y.js CRDT document. Each user editing a workflow has their own Session process that handles:

  • User presence tracking via Lightning.Workflows.Presence
  • Bi-directional Y.js message routing between parent and SharedDoc
  • Connection management to existing SharedDoc processes via Registry lookup
  • Workflow document initialization with database data when needed
  • Cleanup when the parent process disconnects

Sessions are temporary processes that monitor their parent and terminate when the parent disconnects, ensuring proper cleanup of presence tracking and SharedDoc observers.

Summary

Functions

Returns a specification to start this module under a supervisor.

Get the current document.

Resets the workflow document to the latest snapshot from the database.

Saves the current workflow state from the Y.Doc to the database.

Start a new session for a workflow.

Types

start_opts()

@type start_opts() :: [
  workflow: Lightning.Workflows.Workflow.t(),
  user: Lightning.Accounts.User.t(),
  parent_pid: pid()
]

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_doc(session_pid)

Get the current document.

initialize_workflow_document(doc, workflow)

lookup_shared_doc(document_name)

reset_workflow(session_pid, user)

@spec reset_workflow(pid(), Lightning.Accounts.User.t()) ::
  {:ok, Lightning.Workflows.Workflow.t()}
  | {:error, :workflow_deleted | :internal_error}

Resets the workflow document to the latest snapshot from the database.

This operation:

  1. Fetches the latest workflow from the database
  2. Clears all Y.Doc collections (jobs, edges, triggers arrays)
  3. Re-serializes the workflow to the Y.Doc
  4. Broadcasts updates to all connected clients via SharedDoc

All collaborative editing history in the Y.Doc is preserved (operation log), but the document content is replaced with the database state.

Parameters

  • session_pid: The Session process PID
  • user: The user performing the reset (for authorization logging)

Returns

  • {:ok, workflow} - Successfully reset with updated workflow
  • {:error, :workflow_deleted} - Workflow has been deleted from database
  • {:error, :internal_error} - SharedDoc not available

Examples

iex> Session.reset_workflow(session_pid, user)
{:ok, %Workflow{lock_version: 5}}

iex> Session.reset_workflow(session_pid, user)
{:error, :workflow_deleted}

save_workflow(session_pid, user)

@spec save_workflow(pid(), Lightning.Accounts.User.t()) ::
  {:ok, Lightning.Workflows.Workflow.t()}
  | {:error,
     :workflow_deleted
     | :deserialization_failed
     | :internal_error
     | Ecto.Changeset.t()}

Saves the current workflow state from the Y.Doc to the database.

This function:

  1. Extracts the current workflow data from the SharedDoc's Y.Doc
  2. Converts Y.js types to Elixir maps suitable for Ecto
  3. Calls Lightning.Workflows.save_workflow/3 for validation and persistence
  4. Returns the saved workflow

Note: This assumes all Y.js updates have been processed before this call, which is guaranteed by Phoenix Channel's synchronous message handling.

Parameters

  • session_pid: The Session process PID
  • user: The user performing the save (for authorization and auditing)

Returns

  • {:ok, workflow} - Successfully saved
  • {:error, :workflow_deleted} - Workflow has been deleted
  • {:error, changeset} - Validation or persistence error

Examples

iex> Session.save_workflow(session_pid, user)
{:ok, %Workflow{}}

iex> Session.save_workflow(session_pid, user)
{:error, %Ecto.Changeset{}}

send_yjs_message(session_pid, chunk)

start_link(args, opt \\ [])

start_sync(session_pid, chunk)

stop(session_pid)

Start a new session for a workflow.

The session will be started as a child of the Collaboration.Supervisor.

Options

  • workflow - The workflow struct to start the session for.
  • user - The user to start the session for.
  • parent_pid - The pid of the parent process. Defaults to the current process.

stop_shared_doc(session_pid)

update_doc(session_pid, fun)