Last active
February 11, 2016 09:49
-
-
Save tomodutch/186eb803720d21744b77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule UID.Registry do | |
| @moduledoc """ | |
| """ | |
| use GenServer | |
| @doc """ | |
| Link the Registry with the current process. | |
| """ | |
| def start_link(opts \\ []) do | |
| GenServer.start_link(__MODULE__, 1, opts) | |
| end | |
| @doc ~S""" | |
| Generate a new unique identifier starting at 1. | |
| ## Examples | |
| iex> {:ok, pid} = UID.Registry.start_link | |
| ...> UID.Registry.generate_uid(pid) | |
| 1 | |
| """ | |
| def generate_uid(pid) do | |
| GenServer.call(pid, :get_uid) | |
| end | |
| def handle_call(:get_uid, _from, state) do | |
| {:reply, state, state + 1} | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment