Skip to content

Instantly share code, notes, and snippets.

@shoz-f
Created October 1, 2018 12:15
Show Gist options
  • Select an option

  • Save shoz-f/7d3ef3a6c19368bee0d5f9b05ee134a5 to your computer and use it in GitHub Desktop.

Select an option

Save shoz-f/7d3ef3a6c19368bee0d5f9b05ee134a5 to your computer and use it in GitHub Desktop.
GenServerで実装
defmodule StopwatchC do
use GenServer
import System, only: [ monotonic_time: 1 ]
@gclock __MODULE__
def activate() do
now = monotonic_time(100)
GenServer.start_link(StopwatchC, now, name: @gclock)
:ok
end
@impl true
def init(last_time) do
{:ok, last_time}
end
@impl true
def handle_call(:restart, _from, last_time) do
now = monotonic_time(100)
{:reply, now - last_time, now}
end
@impl true
def handle_call(:lap, _from, last_time) do
now = monotonic_time(100)
{:reply, now - last_time, last_time}
end
def restart() do
GenServer.call(@gclock, :restart)
end
def lap() do
GenServer.call(@gclock, :lap)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment