Skip to content

Commit

Permalink
redesign of store initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
grych committed Feb 17, 2017
1 parent ba3412d commit ec47b38
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 27 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
49 changes: 30 additions & 19 deletions lib/drab.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions lib/drab/controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/drab/core.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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 """
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions priv/templates/drab/drab.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})

0 comments on commit ec47b38

Please sign in to comment.