Elixir foundationSort Order

Elixir: Tracking Requests with an Agent - [018]

Since the problem we are solving is server side rendering which means we'll be working with single page applications that make network calls after the page loads we need to track these network calls so we can figure out when a page finishes rendering.

Subscribing to Network.requestWillBeSent

Let's start by modifying our GenServer

defmodule Botiful.Render.Engine do  use GenServer  # ...  def init(url) do    conn = ChroxyClient.page_session!(%{ host: "localhost", port: 1330 })    {:ok, requests} = Agent.start_link(fn -> [] end)     {:ok, _} = RPC.Page.enable(conn)    {:ok, _} = RPC.Network.enable(conn)       :ok = PageSession.subscribe(conn, "Network.requestWillBeSent", self())        {:ok, _} = RPC.Page.navigate(conn, %{url: url})        {:ok, %{conn: conn, requests: requests}}  end  def handle_info(    {:chrome_remote_interface, "Network.requestWillBeSent", data},     state  )    url = data["params"]["request"]["url"]    Agent.update(fn state -> state ++ [url] end)    {:noreply, state}  endend

Now when we start the GenServer it will track all the urls of the network calls in the agent.

Getting all the Urls

We can also implement the function that we can use to output all the urls that have been tracked. Inside the GenServer

# Public APIdef network_urls(pid) do  GenServer.call(pid, :network_urls)end  # Callbackdef handle_call(:network_urls, _from , state) do  {:reply, Agent.get(state.requests, fn state -> state end), state}end

That's pretty much it! We can now start our GenServer and call Botiful.Render.Engine.network_urls(pid)