Add types and specs for LastFm module

This commit is contained in:
Claudio Ortolina
2024-11-11 20:51:00 +00:00
parent 8e1916c22a
commit c7b9aadb70
6 changed files with 38 additions and 1 deletions
+6
View File
@@ -6,11 +6,14 @@ defmodule LastFm.Feed do
is technically prone to collision, it's very unlikely for that to happen due to the
nature of the data.
"""
@spec create_table!() :: :ok | no_return
def create_table! do
__MODULE__ = :ets.new(__MODULE__, [:ordered_set, :named_table, :public])
:ok
end
@spec update([LastFm.Track.t()]) :: :ok | no_return
def update(tracks) do
data = Enum.map(tracks, fn t -> {t.scrobbled_at_uts, t} end)
@@ -20,6 +23,7 @@ defmodule LastFm.Feed do
Phoenix.PubSub.broadcast(LastFm.PubSub, "feed:update", %{tracks: tracks})
end
@spec all() :: [LastFm.Track.t()]
def all do
m = [
{
@@ -29,9 +33,11 @@ defmodule LastFm.Feed do
}
]
# reversing to get tracks in DESC order
:ets.select_reverse(__MODULE__, m)
end
@spec subscribe() :: :ok
def subscribe do
Phoenix.PubSub.subscribe(LastFm.PubSub, "feed:update")
end