diff --git a/TODO.md b/TODO.md index a051301..bedf679 100644 --- a/TODO.md +++ b/TODO.md @@ -17,4 +17,3 @@ Changes: * dependencies for modules (for ex. Modal depends on Query) * before_handler callback (to run before each handler), if return anything else than Socket, do not proceed * handling event handler crashes without disconnect the whole socket (spawn instead of spawn_link) -* `get_store/2` should operate on GenServer state, not getting store from browser every time diff --git a/lib/drab.ex b/lib/drab.ex index 8a77857..c8da469 100644 --- a/lib/drab.ex +++ b/lib/drab.ex @@ -82,26 +82,21 @@ defmodule Drab do {:noreply, state} end - @doc false - def handle_cast({:onload, socket}, {store, session, commander}) do - # update the Store with values taken from session - # Drab.Core.inherited_store(socket) |> Enum.each(fn({k, v}) -> Drab.Core.put_store(socket, k, v) end) - # Drab.Core.put_store(socket, :something, :dupa) - - # save the store and session in Drab server - Drab.Core.save_store(socket, Drab.Core.store(socket)) - Drab.Core.save_session(socket, Drab.Core.session(socket)) + def handle_cast({:onconnect, socket}, {store, session, commander}) do + tasks = [Task.async(fn -> Drab.Core.save_session(socket, Drab.Core.session(socket)) end), + Task.async(fn -> Drab.Core.save_store(socket, Drab.Core.store(socket)) end)] + Enum.each(tasks, fn(task) -> Task.await(task) end) - onload = drab_config(commander).onload - handle_callback(socket, commander, onload) #returns socket + onconnect = drab_config(commander).onconnect + handle_callback(socket, commander, onconnect) #returns socket {:noreply, {store, session, commander}} end @doc false - def handle_cast({:onconnect, socket}, {store, session, commander}) do - onconnect = drab_config(commander).onconnect - handle_callback(socket, commander, onconnect) #returns socket + def handle_cast({:onload, socket}, {store, session, commander}) do + onload = drab_config(commander).onload + handle_callback(socket, commander, onload) #returns socket {:noreply, {store, session, commander}} end @@ -123,17 +118,24 @@ defmodule Drab do @doc false - def handle_call(:get_store, _from, store) do - {:reply, store, store} + def handle_call(:get_store, _from, {store, session, commander}) do + {:reply, store, {store, session, commander}} + end + + @doc false + def handle_call(:get_session, _from, {store, session, commander}) do + {:reply, session, {store, session, commander}} end defp handle_callback(socket, commander, callback) do if callback do - apply(commander, callback, [socket]) - else - socket + # TODO: rethink the subprocess strategies - now it is just spawn_link + spawn_link fn -> + apply(commander, callback, [socket]) + end end + socket end defp handle_event(socket, event_handler_function, payload, reply_to, store, session, commander) do @@ -165,12 +167,21 @@ defmodule Drab do {:noreply, {store, session, commander}} end + @doc false + def get_store(pid) do + GenServer.call(pid, :get_store) + end @doc false def update_store(pid, new_store) do GenServer.cast(pid, {:update_store, new_store}) end + @doc false + def get_session(pid) do + GenServer.call(pid, :get_session) + end + @doc false def update_session(pid, new_session) do GenServer.cast(pid, {:update_session, new_session}) diff --git a/lib/drab/controller.ex b/lib/drab/controller.ex index c904cd4..c46f14f 100644 --- a/lib/drab/controller.ex +++ b/lib/drab/controller.ex @@ -23,8 +23,8 @@ defmodule Drab.Controller do """ defmacro __using__(options) do - quote do - Module.put_attribute(__MODULE__, :__drab_opts__, unquote(options)) + quote bind_quoted: [options: options] do + Module.put_attribute(__MODULE__, :__drab_opts__, options) unless Module.defines?(__MODULE__, {:__drab__, 0}) do def __drab__() do # default commander is named as a controller diff --git a/lib/drab/core.ex b/lib/drab/core.ex index 8691964..4c12761 100644 --- a/lib/drab/core.ex +++ b/lib/drab/core.ex @@ -82,7 +82,9 @@ defmodule Drab.Core do uid = get_store(socket, :user_id) """ def get_store(socket, key) do - store(socket)[key] + store = Drab.get_store(socket.assigns.drab_pid) + store[key] + # store(socket)[key] end @doc """ @@ -122,7 +124,8 @@ defmodule Drab.Core do You must explicit which session keys you want to access in `:access_session` option in `use Drab.Commander`. """ def get_session(socket, key) do - session(socket)[key] + Drab.get_session(socket.assigns.drab_pid)[key] + # session(socket)[key] end @doc """ @@ -145,6 +148,7 @@ defmodule Drab.Core do def store(socket) do store_token = execjs(socket, "Drab.get_drab_store_token()") detokenize_store(socket, store_token) + # GenServer.call(socket.assigns.drab_pid, :get_store) end @doc false diff --git a/priv/templates/drab/drab.core.js b/priv/templates/drab/drab.core.js index 008a75b..45af060 100644 --- a/priv/templates/drab/drab.core.js +++ b/priv/templates/drab/drab.core.js @@ -25,12 +25,12 @@ Drab.on_connect(function(resp, drab) { }) } + // launch server-side onconnect callback - every time it is connected + drab.channel.push("onconnect", { drab_store_token: Drab.drab_store_token }) + // initialize onload on server side, just once if (!drab.onload_launched) { drab.channel.push("onload", { drab_store_token: Drab.drab_store_token }) drab.onload_launched = true } - - // launch server-side onconnect callback - every time it is connected - drab.channel.push("onconnect", { drab_store_token: Drab.drab_store_token }) })