Skip to content

Modern era update #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ by adding `expline` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:expline, "~> 0.1.0"}]
[{:expline, "~> 0.2"}]
end
```

Expand Down
50 changes: 36 additions & 14 deletions lib/expline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ defmodule Expline do
building, read the `Expline.Spline` module documentation.
"""

@typep state() :: Expline.Spline.t
@typep state() :: Expline.Spline.t()

@doc """
Builds a spline from the provided list of points and holds the state in a
process without links (outside of a supervision tree).

See `start_link/2` for more information.
"""
@spec start(list(Expline.Spline.point()), GenServer.options()) :: {:ok, pid()}
| {:error, {:already_started, pid()}}
| {:error, Expline.Spline.creation_error()}
@spec start(list(Expline.Spline.point()), {:graceful_shutdown, boolean()} | GenServer.options()) ::
{:ok, pid()}
| {:error, {:already_started, pid()}}
| {:error, Expline.Spline.creation_error()}
def start(points, opts \\ []) do
GenServer.start(__MODULE__, [points], opts)
{graceful_shutdown, opts} = Keyword.pop(opts, :graceful_shutdown, false)

case GenServer.start(__MODULE__, {graceful_shutdown, [points]}, opts) do
{:error, {:shutdown, reason}} -> {:error, reason}
other -> other
end
Comment on lines +29 to +34
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is (and below for start_link/2) the only significant change, everything else is formatter.

end

@doc """
Expand All @@ -36,21 +42,34 @@ defmodule Expline do

## Options and more information

- `graceful_shutdown` when `true`, gracefully shuts down `GenServer`
without a crash report, default: `false`

See `GenServer.start_link/3` for more information.
"""
@spec start_link(list(Expline.Spline.point()), GenServer.options()) :: {:ok, pid()}
| {:error, {:already_started, pid()}}
| {:error, Expline.Spline.creation_error()}
@spec start_link(
list(Expline.Spline.point()),
{:graceful_shutdown, boolean()} | GenServer.options()
) ::
{:ok, pid()}
| {:error, {:already_started, pid()}}
| {:error, Expline.Spline.creation_error()}
def start_link(points, opts \\ []) do
GenServer.start_link(__MODULE__, [points], opts)
{graceful_shutdown, opts} = Keyword.pop(opts, :graceful_shutdown, false)

case GenServer.start_link(__MODULE__, {graceful_shutdown, [points]}, opts) do
{:error, {:shutdown, reason}} -> {:error, reason}
other -> other
end
end

def init([list_of_points]) do
def init({graceful_shutdown, [list_of_points]}) do
case Expline.Spline.from_points(list_of_points) do
{:ok, spline} ->
{:ok, spline}

{:error, reason} ->
{:stop, reason}
{:stop, if(graceful_shutdown, do: {:shutdown, reason}, else: reason)}
end
end

Expand All @@ -61,17 +80,20 @@ defmodule Expline do
`t:Expline.Spline.interpolation_error/0` will be returned.
"""

@spec interpolate(GenServer.server(), float(), timeout()) :: {:ok, Expline.Spline.point()}
| {:error, Expline.Spline.interpolation_error()}
@spec interpolate(GenServer.server(), float(), timeout()) ::
{:ok, Expline.Spline.point()}
| {:error, Expline.Spline.interpolation_error()}
def interpolate(server, x, timeout \\ 5000) when is_float(x) do
GenServer.call(server, {:interpolate, x}, timeout)
end

@spec handle_call({:interpolate, Expline.Spline.dependent_value()}, GenServer.from(), state()) :: {:reply, {:ok, Expline.Spline.point()}, state()}
@spec handle_call({:interpolate, Expline.Spline.dependent_value()}, GenServer.from(), state()) ::
{:reply, {:ok, Expline.Spline.point()}, state()}
def handle_call({:interpolate, x}, _from, spline) do
case Expline.Spline.interpolate(spline, x) do
{:ok, y} ->
{:reply, {:ok, {x, y}}, spline}

{:error, reason} ->
{:reply, {:error, reason}, spline}
end
Expand Down
Loading