diff --git a/lib/live_json.ex b/lib/live_json.ex index bf94846..9806dec 100644 --- a/lib/live_json.ex +++ b/lib/live_json.ex @@ -10,16 +10,15 @@ defmodule LiveJson do ## def initialize(socket, doc_name, data) do - doc_atom = safer_string_to_atom("lj" <> doc_name) + values = Map.put(socket.assigns[:__live_json_values] || %{}, doc_name, data) + socket - |> Utils.assign(doc_atom, data) + |> Utils.assign(:__live_json_values, values) |> Utils.push_event("lj:init", %{doc_name: doc_name, data: data}) end def push_patch(socket, doc_name, new_data, method \\ :jsondiff) do - - doc_atom = safer_string_to_atom("lj" <> doc_name) - old_data = Map.get(socket.assigns, doc_atom) + old_data = Map.get(socket.assigns.__live_json_values, doc_name) data_patch = if method == :rfc do Jsonpatch.diff(old_data, new_data) @@ -30,8 +29,10 @@ defmodule LiveJson do # If there's no data in the patch, no reason to send it. if data_patch != %{} do + new_values = Map.put(socket.assigns.__live_json_values, doc_name, new_data) + socket - |> Utils.assign(doc_atom, new_data) + |> Utils.assign(:__live_json_values, new_values) |> Utils.push_event("lj:patch", %{doc_name: doc_name, patch: data_patch, method: method}) else socket @@ -60,20 +61,4 @@ defmodule LiveJson do socket |> Utils.push_event("lj:put", %{doc_name: doc_name, key: key, value: value}) end - - ## - # Helpers - ## - - # XXX: Don't know if this is wise or necessary, - # but maybe will help avoid the atom limit. - @spec safer_string_to_atom(binary) :: atom - def safer_string_to_atom(str) do - try do - String.to_existing_atom(str) - rescue - ArgumentError -> String.to_atom(str) - end - end - end