Created
October 1, 2018 12:15
-
-
Save shoz-f/7d3ef3a6c19368bee0d5f9b05ee134a5 to your computer and use it in GitHub Desktop.
GenServerで実装
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 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