From f21de24b1978f4de220e383b865f5c25ff588c99 Mon Sep 17 00:00:00 2001 From: Alexandre Costa Date: Tue, 11 Jun 2024 15:07:32 -0700 Subject: [PATCH] Move documentation from server to eredis_sub --- lib/eredis_sub.ex | 43 ++++++++++++++++++++++++++++++---------- lib/eredis_sub/server.ex | 34 ++----------------------------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/lib/eredis_sub.ex b/lib/eredis_sub.ex index 09c47d5..faf4967 100644 --- a/lib/eredis_sub.ex +++ b/lib/eredis_sub.ex @@ -1,15 +1,36 @@ defmodule EredisSub do @moduledoc """ - Documentation for `EredisSub`. - """ + Publishes binary messages to Redis Pub/Sub channels. + Subscribes to channels and calls a handler function when a message is received. - def start_link(config \\ []) do - EredisSub.Server.start_link(config) - end + ## Usage - def child_spec(config) do - EredisSub.Server.child_spec(config) - end + ### Publish + + EredisSub.Server.publish("my_channel", "Hello, world!") + + ### Subscribe + + Implement the behaviour to be called when a message is received: + + defmodule MyModule do + @behaviour EredisSub.Handler + + @impl EredisSub.Handler + def handle(message, metadata) do + # Do something... + end + end + + Subscribe to a channel: + + metadata_example = %{subscribed_at: DateTime.utc_now()} + EredisSub.Server.subscribe("my_channel", {MyModule, :handle, metadata_example}) + """ + alias EredisSub.Server + + def start_link(config \\ []), do: Server.start_link(config) + def child_spec(config), do: Server.child_spec(config) @doc """ Publish a message to a channel. @@ -20,7 +41,7 @@ defmodule EredisSub do :ok """ def publish(channel, message) do - EredisSub.Server.publish(channel, message) + Server.publish(channel, message) end @doc """ @@ -41,7 +62,7 @@ defmodule EredisSub do :ok """ def subscribe(channel, handler) do - EredisSub.Server.subscribe(channel, handler) + Server.subscribe(channel, handler) end @doc """ @@ -53,6 +74,6 @@ defmodule EredisSub do :ok """ def unsubscribe_all(channel) do - EredisSub.Server.unsubscribe_all(channel) + Server.unsubscribe_all(channel) end end diff --git a/lib/eredis_sub/server.ex b/lib/eredis_sub/server.ex index 56d211b..eda0e8b 100644 --- a/lib/eredis_sub/server.ex +++ b/lib/eredis_sub/server.ex @@ -1,37 +1,7 @@ defmodule EredisSub.Server do @moduledoc """ - Publishes binary messages to Redis Pub/Sub channels. - Subscribes to channels and calls a handler function when a message is received. - - ## Usage - - ### Publish - - ```elixir - EredisSub.Server.publish("my_channel", "Hello, world!") - ``` - - ### Subscribe - - Implement the behaviour to be called when a message is received: - - ```elixir - defmodule MyModule do - @behaviour EredisSub.Handler - - @impl EredisSub.Handler - def handle(message, metadata) do - # Do something... - end - end - ``` - - Subscribe to a channel: - - ```elixir - metadata_example = %{subscribed_at: DateTime.utc_now()} - EredisSub.Server.subscribe("my_channel", {MyModule, :handle, metadata_example}) - ``` + Holds internal state for `EredisSub`. + Do not use this module directly. Instead, use `EredisSub`. """ use GenServer