Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't use Atom.to_string #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions lib/live_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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