Skip to content

Commit

Permalink
Merge pull request #84 from pmargreff/refactor/remove-unecessary-insp…
Browse files Browse the repository at this point in the history
…ect-calls

Refactor/remove unecessary inspect calls
  • Loading branch information
benwilson512 authored Jan 8, 2020
2 parents dfbdf98 + 0c223e2 commit fa785f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
28 changes: 18 additions & 10 deletions lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,26 @@ if Code.ensure_loaded?(Ecto) do
end

def load(source, batch, item) do
case fetch(source, batch, item) do
{:error, _message} ->
batch = normalize_key(batch, source.default_params)
{batch_key, item_key, item} = get_keys(batch, item)
entry = {item_key, item}
{batch_key, item_key, item} =
batch
|> normalize_key(source.default_params)
|> get_keys(item)

update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([entry]), &MapSet.put(&1, entry))
end)
if fetched?(source.results, batch_key, item_key) do
source
else
entry = {item_key, item}

_ ->
source
update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([entry]), &MapSet.put(&1, entry))
end)
end
end

defp fetched?(results, batch_key, item_key) do
case results do
%{^batch_key => {:ok, %{^item_key => _}}} -> true
_ -> false
end
end

Expand Down
22 changes: 14 additions & 8 deletions lib/dataloader/kv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,20 @@ defmodule Dataloader.KV do
end

def load(source, batch_key, id) do
case fetch(source, batch_key, id) do
{:error, _message} ->
update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([id]), &MapSet.put(&1, id))
end)

_ ->
source
if fetched?(source.results, batch_key, id) do
source
else
update_in(source.batches, fn batches ->
Map.update(batches, batch_key, MapSet.new([id]), &MapSet.put(&1, id))
end)
end
end

defp fetched?(results, batch_key, id) do
case results do
%{^batch_key => %{^id => {:error, _}}} -> false
%{^batch_key => %{^id => _}} -> true
_ -> false
end
end

Expand Down

0 comments on commit fa785f8

Please sign in to comment.