Skip to content

Instantly share code, notes, and snippets.

@bceskavich
Created May 21, 2019 20:14
Show Gist options
  • Save bceskavich/d4823617c44ebc2c32af9d32cf2dfae2 to your computer and use it in GitHub Desktop.
Save bceskavich/d4823617c44ebc2c32af9d32cf2dfae2 to your computer and use it in GitHub Desktop.
Frame.io Elixir Study Group Exercise 4: GenServers
defmodule PopularityStoreServer do
@moduledoc """
Our emoji popularity store, implemented as a GenServer. A GenServer is a
generic approach for implementing a stateful process. In this store module, we
bundle both the client API for interacting with our GenServer and the internal
process callbacks used to respond to inbound messages.
"""
use GenServer
# %%% Client API %%%
#
# Here we expose a series of functions for interacting with our GenServer
# process. These functions generally wrap calls to functions on the `GenServer`
# module, and will ultimately invoke callbacks on the GenServer process that
# we handle below.
#
# Docs for valid GenServer functions: https://hexdocs.pm/elixir/GenServer.html#functions
@doc """
Starts our popularity store as a global GenServer, initialized with an empty
map. The `name: __MODULE__` option makes our GenServer process globally accessible,
via the `__MODULE__` alias helper.
"""
def start_link(_opts) do
GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
end
@doc """
Retrieves an emoji by the given name. Returns nil if no emoji is found.
TODO: Implement this function and it's GenServer callback to match the
functionality from Exercise 3.
"""
def get(emoji_name) do
end
@doc """
Returns a list of the most popular emoji stored.
TODO: Implement this function and it's GenServer callback to match the
functionality from Exercise 3.
"""
def get_most_popular(to_take \\ 5) do
end
@doc """
Increments the popularity count of the emoji given by 1. If the emoji isn't
already in state, an initial popularity count is set to 1.
TODO: Implement this function and it's GenServer callback to match the
functionality from Exercise 3.
"""
def inc_popularity(emoji_name) do
end
@doc """
Removes an emoji from the popularity list
TODO: Implement the GenServer function call here and the associated internal
callback below.
"""
def remove_emoji(emoji_name) do
end
# %%% Internal Callbacks %%%
#
# Functions used to handle interaction with our GenServer process. You can find
# the full list of valid callbacks plus valid responses for each here:
# https://hexdocs.pm/elixir/GenServer.html#callbacks
# Callback invoked by `GenServer.start_link/3`. We reply with the initial state.
def init(state) do
{:ok, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment