<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="228365" data-post-id="228365">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fabioticconi" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fabioticconi/120/5764_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fabioticconi
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m trying with ETS tables. There’s something I don’t get.. I create the table in the Command module, as soon as it’s loaded:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @on_load :init
  def init() do
    res = :ets.new(:commands, [
      :named_table,
      :ordered_set,
      :public,
      {:read_concurrency, true},
      {:write_concurrency, true}
    ])
    whereis = :ets.whereis(:commands)
    content = :ets.tab2list(:commands)
    Logger.debug("table created: #{inspect(res)} #{inspect(whereis)} #{inspect(content)}")
  end
</code></pre>
<p>This works fine:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">21:45:58.988 [debug] table created: :commands #Reference&lt;0.2865878229.9043973.175739&gt; []
</code></pre>
<p>The <code>__using__</code> macro is like this now, with a <code>Process.sleep</code> to make sure the ETS table is setup at that point:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmacro __using__(opts) do
    quote do
      @behaviour Command

      @on_load :register_command

      def register_command() do
        Process.sleep(5000)
        Command.register_command(__MODULE__, unquote(opts))
      end

      def parse_args(_cmd_args), do: {:ok, nil}
      defoverridable [parse_args: 1]
    end
  end
</code></pre>
<p>And then in the <code>register_command</code>, I try to insert:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def register_command(module, cmd_opts) do
    Logger.debug("register_command: #{module} #{inspect(cmd_opts)}")
    cmd_name = Keyword.fetch!(cmd_opts, :name)
    :ets.insert(:commands, {cmd_name, module})
  end
</code></pre>
<p>But this, after the 5 seconds, crashes:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">21:53:54.865 [warn]  The on_load function for module Elixir.Commands.Info returned:
{:badarg,
 [
   {:ets, :insert, [:commands, {"info", Commands.Info}],
    [error_info: %{cause: :id, module: :erl_stdlib_errors}]},
   {Command, :register_command, 2, [file: 'lib/command.ex', line: 60]},
   {:code_server, :"-handle_on_load/5-fun-0-", 1,
    [file: 'code_server.erl', ...]}
 ]}

21:53:54.858 [error] Process #PID&lt;0.211.0&gt; raised an exception
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: the table identifier does not refer to an existing ETS table

    (stdlib 3.15.2) :ets.insert(:commands, {"info", Commands.Info})
    lib/command.ex:60: Command.register_command/2
    (kernel 8.0.2) code_server.erl:1317: anonymous fn/1 in :code_server.handle_on_load/5
</code></pre>
<p>How does the table name, <code>:commands</code>, not refer to an existing ETS table? I don’t get. Is this because it’s happening at <strong>compile time</strong> and when the compilation finishes, the ETS table dies?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="228365" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/command-pattern-via-tcp/42809/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-228365" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="228365"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="228366" data-post-id="228366">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fabioticconi" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fabioticconi/120/5764_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fabioticconi
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="al2o3cr" data-post="9" data-topic="42809">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/48/3457_2.png" class="avatar"> al2o3cr:</div>
<blockquote>
<p>Worth looking into <a href="https://erlang.org/doc/man/persistent_term.html" rel="nofollow"><code>persistent_term</code> </a> for storing the map - an Agent still forces every access through a single thread.</p>
</blockquote>
</aside>
<p>I had discounted this in the beginning, but.. it’s brilliant. It works across compilation because the VM will not be stopped. It’s pretty much <em>exactly</em> my use case and it’s of a crazy simplicity.</p>
<p>Too bad it doesn’t support an <code>update</code> method but I don’t strictly need it, I’ll keep it flat: <code>{:command, cmd_name}</code> as key to avoid clashes, and the module as value. Brilliant.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="228366" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/command-pattern-via-tcp/42809/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-228366" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="228366"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="228367" data-post-id="228367">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="fabioticconi" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fabioticconi/120/5764_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  fabioticconi
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Now, final question on the subject (thanks all for your help, by the way): why do I need to remove the <code>_build</code> directory to trigger the <code>@on_load</code> hook? Shouldn’t that trigger when a module is <em>loaded</em>, and not when compiled?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="228367" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/command-pattern-via-tcp/42809/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-228367" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="228367"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>