this repo has no description

Functions for fetching data from the cache

hauleth.dev 41cfa637 5dc8616e

verified
Changed files
+33 -12
lib
esl_hn
+23
lib/esl_hn.ex
···
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
end
···
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
+
+
def all(page \\ 1, per_page \\ 10) do
+
skip = per_page * (page - 1)
+
+
ids = EslHn.Cache.get(EslHn, :index, [])
+
+
ids
+
|> Enum.drop(skip)
+
|> Enum.take(per_page)
+
|> Enum.map(&story/1)
+
end
+
+
def story(id) do
+
EslHn.Cache.get(EslHn, id)
+
end
+
+
def broadcast_new(stories) do
+
Phoenix.PubSub.broadcast(EslHn.PubSub, "hn:new", {:new_stories, stories})
+
end
+
+
def subscribe_new do
+
Phoenix.PubSub.subscribe(EslHn.PubSub, "hn:new")
+
end
end
+10 -12
lib/esl_hn/hn.ex
···
end
end
-
def top_stories(opts \\ []) do
-
with {:ok, ids} <- top_stories_ids(opts) do
-
ids
-
|> Task.async_stream(&story/1)
-
|> map_while(fn
-
{:ok, {:ok, data}} -> {:cont, data}
-
other -> {:halt, other}
-
end)
-
|> case do
-
list when is_list(list) -> {:ok, list}
-
error -> error
-
end
end
end
···
end
end
+
def stories(ids, opts \\ []) do
+
ids
+
|> Task.async_stream(&story(&1, opts))
+
|> map_while(fn
+
{:ok, {:ok, data}} -> {:cont, data}
+
other -> {:halt, other}
+
end)
+
|> case do
+
list when is_list(list) -> {:ok, list}
+
error -> error
end
end