Skip to content

Commit ef4eb19

Browse files
committed
chore: Move start_link / init back to top
1 parent 530be2b commit ef4eb19

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

lib/ecto_watch.ex

+32-32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,38 @@ defmodule EctoWatch do
99

1010
use Supervisor
1111

12+
def start_link(opts) do
13+
case EctoWatch.Options.validate(opts) do
14+
{:ok, validated_opts} ->
15+
options = EctoWatch.Options.new(validated_opts)
16+
17+
validate_watcher_uniqueness(options.watchers)
18+
19+
Supervisor.start_link(__MODULE__, options, name: __MODULE__)
20+
21+
{:error, errors} ->
22+
raise ArgumentError, "Invalid options: #{Exception.message(errors)}"
23+
end
24+
end
25+
26+
def init(options) do
27+
# TODO:
28+
# Allow passing in options specific to Postgrex.Notifications.start_link/1
29+
# https://hexdocs.pm/postgrex/Postgrex.Notifications.html#start_link/1
30+
31+
postgrex_notifications_options =
32+
options.repo_mod.config()
33+
|> Keyword.put(:name, :ecto_watch_postgrex_notifications)
34+
35+
children = [
36+
{Postgrex.Notifications, postgrex_notifications_options},
37+
{EctoWatch.WatcherSupervisor, options},
38+
{WatcherTriggerValidator, nil}
39+
]
40+
41+
Supervisor.init(children, strategy: :rest_for_one)
42+
end
43+
1244
@since "0.8.0"
1345
@deprecated "subscribe/3 was removed in version 0.8.0. See the updated documentation"
1446
def subscribe(schema_mod_or_label, update_type, id) when is_atom(schema_mod_or_label) do
@@ -220,38 +252,6 @@ defmodule EctoWatch do
220252
end
221253
end
222254

223-
def start_link(opts) do
224-
case EctoWatch.Options.validate(opts) do
225-
{:ok, validated_opts} ->
226-
options = EctoWatch.Options.new(validated_opts)
227-
228-
validate_watcher_uniqueness(options.watchers)
229-
230-
Supervisor.start_link(__MODULE__, options, name: __MODULE__)
231-
232-
{:error, errors} ->
233-
raise ArgumentError, "Invalid options: #{Exception.message(errors)}"
234-
end
235-
end
236-
237-
def init(options) do
238-
# TODO:
239-
# Allow passing in options specific to Postgrex.Notifications.start_link/1
240-
# https://hexdocs.pm/postgrex/Postgrex.Notifications.html#start_link/1
241-
242-
postgrex_notifications_options =
243-
options.repo_mod.config()
244-
|> Keyword.put(:name, :ecto_watch_postgrex_notifications)
245-
246-
children = [
247-
{Postgrex.Notifications, postgrex_notifications_options},
248-
{EctoWatch.WatcherSupervisor, options},
249-
{WatcherTriggerValidator, nil}
250-
]
251-
252-
Supervisor.init(children, strategy: :rest_for_one)
253-
end
254-
255255
defp validate_watcher_uniqueness(watcher_options) do
256256
{without_labels, with_labels} = Enum.split_with(watcher_options, &(&1.label == nil))
257257

0 commit comments

Comments
 (0)