|
1 | 1 | defmodule Inertia.SSR.Config do |
2 | 2 | @moduledoc false |
3 | 3 |
|
| 4 | + alias Inertia.SSR.Adapter |
| 5 | + |
4 | 6 | use GenServer |
5 | 7 |
|
6 | | - # Client |
| 8 | + @type state :: %{adapter: module(), config: struct()} |
| 9 | + |
| 10 | + def start_link(opts) do |
| 11 | + adapter = Keyword.fetch!(opts, :adapter) |
| 12 | + config = Keyword.fetch!(opts, :config) |
7 | 13 |
|
8 | | - def start_link(init_arg) do |
9 | | - GenServer.start_link(__MODULE__, init_arg, name: __MODULE__) |
| 14 | + GenServer.start_link( |
| 15 | + __MODULE__, |
| 16 | + %{ |
| 17 | + adapter: adapter, |
| 18 | + config: config |
| 19 | + }, |
| 20 | + name: __MODULE__ |
| 21 | + ) |
10 | 22 | end |
11 | 23 |
|
12 | | - def module(pid) do |
13 | | - GenServer.call(pid, :module) |
| 24 | + @doc "Stores the adapter module and its config" |
| 25 | + def set_adapter(adapter_module, config) do |
| 26 | + GenServer.call(__MODULE__, {:set_adapter, adapter_module, config}) |
14 | 27 | end |
15 | 28 |
|
16 | | - # Server (callbacks) |
| 29 | + @doc "Forwards the page call to the adapter" |
| 30 | + def call(page) do |
| 31 | + GenServer.call(__MODULE__, {:call, page}) |
| 32 | + end |
| 33 | + |
| 34 | + @impl true |
| 35 | + @spec init(state()) :: {:ok, state()} |
| 36 | + def init(state), do: {:ok, state} |
17 | 37 |
|
18 | 38 | @impl true |
19 | | - def init(state) do |
20 | | - {:ok, state} |
| 39 | + @spec handle_call( |
| 40 | + {:call, Adapter.page()}, |
| 41 | + GenServer.from(), |
| 42 | + state() |
| 43 | + ) :: {:reply, Adapter.ssr_result(), state()} |
| 44 | + def handle_call({:call, page}, _from, %{adapter: adapter, config: config} = state) do |
| 45 | + {:reply, adapter.call(page, config), state} |
21 | 46 | end |
22 | 47 |
|
23 | 48 | @impl true |
24 | | - def handle_call(:module, _from, state) do |
25 | | - {:reply, state[:module], state} |
| 49 | + def handle_call({:set_adapter, adapter_module, config}, _from, _state) do |
| 50 | + {:reply, :ok, %{adapter: adapter_module, config: config}} |
26 | 51 | end |
27 | 52 | end |
0 commit comments