|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is Phoenix.SessionProcess, an Elixir library that creates a process for each user session in Phoenix applications. All user requests go through their dedicated session process, providing session isolation and state management. |
| 8 | + |
| 9 | +## Key Commands |
| 10 | + |
| 11 | +### Development Commands |
| 12 | +- `mix deps.get` - Install dependencies |
| 13 | +- `mix test` - Run all tests |
| 14 | +- `mix test test/path/to/specific_test.exs` - Run a specific test file |
| 15 | +- `mix compile` - Compile the project |
| 16 | +- `mix docs` - Generate documentation |
| 17 | +- `mix hex.publish` - Publish to Hex.pm (requires authentication) |
| 18 | + |
| 19 | +### Testing |
| 20 | +The test suite uses ExUnit. Tests are located in the `test/` directory. The test helper starts the supervisor automatically. |
| 21 | + |
| 22 | +## Architecture |
| 23 | + |
| 24 | +### Core Components |
| 25 | + |
| 26 | +1. **Phoenix.SessionProcess** (lib/phoenix/session_process.ex:1) |
| 27 | + - Main module providing the public API |
| 28 | + - Delegates to ProcessSupervisor for actual process management |
| 29 | + - Provides macros for creating session processes |
| 30 | + |
| 31 | +2. **Phoenix.SessionProcess.Supervisor** (lib/phoenix/session_process/superviser.ex) |
| 32 | + - Top-level supervisor that manages the Registry and ProcessSupervisor |
| 33 | + - Must be added to the application's supervision tree |
| 34 | + |
| 35 | +3. **Phoenix.SessionProcess.ProcessSupervisor** (lib/phoenix/session_process/process_superviser.ex) |
| 36 | + - DynamicSupervisor that manages individual session processes |
| 37 | + - Handles starting, terminating, and communicating with session processes |
| 38 | + |
| 39 | +4. **Phoenix.SessionProcess.SessionId** (lib/phoenix/session_process/session_id.ex) |
| 40 | + - Plug that generates unique session IDs |
| 41 | + - Must be placed after `:fetch_session` plug |
| 42 | + |
| 43 | +### Process Management Flow |
| 44 | + |
| 45 | +1. Session ID generation via the SessionId plug |
| 46 | +2. Process creation through `Phoenix.SessionProcess.start/1-3` |
| 47 | +3. Processes are registered in `Phoenix.SessionProcess.Registry` |
| 48 | +4. Communication via `call/2-3` and `cast/2` |
| 49 | +5. Automatic cleanup when processes terminate |
| 50 | + |
| 51 | +### Key Design Patterns |
| 52 | + |
| 53 | +- Uses Registry for process lookup by session ID |
| 54 | +- DynamicSupervisor for on-demand process creation |
| 55 | +- Provides two macros: `:process` (basic) and `:process_link` (with LiveView monitoring) |
| 56 | +- Session processes can monitor LiveView processes and notify them on termination |
| 57 | + |
| 58 | +## Configuration |
| 59 | + |
| 60 | +The library uses application configuration: |
| 61 | +```elixir |
| 62 | +config :phoenix_session_process, session_process: MySessionProcess |
| 63 | +``` |
| 64 | + |
| 65 | +This sets the default module to use when starting session processes without specifying a module. |
| 66 | + |
| 67 | +## Usage in Phoenix Applications |
| 68 | + |
| 69 | +1. Add supervisor to application supervision tree |
| 70 | +2. Add SessionId plug after fetch_session |
| 71 | +3. Define custom session process modules using the provided macros |
| 72 | +4. Start processes with session IDs |
| 73 | +5. Communicate using call/cast operations |
0 commit comments