Skip to content

Commit

Permalink
VIP - Add more docs for Hook, add custom pattern for {:ok, data}
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Jul 2, 2024
1 parent e8e099f commit 3fdd66e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 23 deletions.
56 changes: 56 additions & 0 deletions lib/event/hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,62 @@ defmodule MishkaInstaller.Event.Hook do
>
> You can override these functions:
> `register: 0`, `start: 0`, `restart: 0`, `stop: 0`, `unregister: 0`, `get: 0`
### Consideration:
It should be brought to your attention that the output of the `call` function has a few trade-offs.
These trade-offs do not restrict your ability to customize the outcome of the `call` function;
nevertheless, they do cause you to move away from the foreseen and planned structure that is the
approach that this library takes.
In situations when you have access to a `:private` key, it is preferable for your data to be in the
form of a `Map` or a `Keyword`. This allows you to perform an accurate match prior to the
output of the data.
With the exception of these two circumstances, we do not collect any other data.
---
The list below is all the outputs that are checked in the form of different wrist patterns:
```elixir
{:ok, data} when is_list(data) # It can be merged with the `:private` key
{:ok, data} when is_map(data) # It can be merged with the `:private` key
# If you have :private, we do not recommend to use this pattern
{:ok, data} # It can not be merged with the `:private` key
{:error, _errors} # It does not need to be merged
data when is_list(data) # It can be merged with the `:private` key
data when is_map(data) # It can be merged with the `:private` key
```
### For example:
We do not recommend these the data or error data; they should have `Map` or `Keyword` format.
```elixir
def call(entries) do
{:reply, :halt, {:ok, "Message is sent!"}}
end
def call(entries) do
{:reply, entries}
end
```
But it is recommended to use these.
```elixir
def call(entries) do
{:reply, :halt, {:ok, %{name: "mishka"}}
end
def call(entries) do
{:reply, {:ok, [name: "mishka"]}
end
def call(entries) do
{:reply, {:error, any_data}
end
```
"""
alias MishkaInstaller.Event.{Event, ModuleStateCompiler}

Expand Down
53 changes: 30 additions & 23 deletions lib/event/module_state_compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,37 @@ defmodule MishkaInstaller.Event.ModuleStateCompiler do
unquote(Macro.escape(plugins))
|> MishkaInstaller.Event.ModuleStateCompiler.perform({:reply, state})

if !is_nil(return_status) do
state
else
case performed do
{:ok, data} when is_list(data) ->
if Keyword.keyword?(data) and !is_nil(private),
do: {:ok, Keyword.merge(data, private)},
else: {:ok, data}

{:ok, data} when is_map(data) ->
{:ok, if(!is_nil(private), do: Map.merge(data, private), else: data)}

{:error, _errors} = errors ->
errors

data when is_list(data) ->
if Keyword.keyword?(data) and !is_nil(private),
do: Keyword.merge(data, private),
else: data

data when is_map(data) ->
if !is_nil(private), do: Map.merge(data, private), else: data
new_state =
if !is_nil(return_status) do
state
else
case performed do
{:ok, data} when is_list(data) ->
if Keyword.keyword?(data) and !is_nil(private),
do: {:ok, Keyword.merge(data, private)},
else: {:ok, data}

{:ok, data} when is_map(data) ->
{:ok, if(!is_nil(private), do: Map.merge(data, private), else: data)}

# If you have :private, we do not recommend to use this pattern
{:ok, data} ->
{:ok, data}

{:error, _errors} = errors ->
errors

data when is_list(data) ->
if Keyword.keyword?(data) and !is_nil(private),
do: Keyword.merge(data, private),
else: data

data when is_map(data) ->
if !is_nil(private), do: Map.merge(data, private), else: data
end
end
end

new_state
rescue
_e -> state
end
Expand Down

0 comments on commit 3fdd66e

Please sign in to comment.