-
Notifications
You must be signed in to change notification settings - Fork 2
Run plugs on the socket before the actual action #10
Conversation
Frankt now allows the configuration of plugs that will be run before the actual action handler. The plugs may modify the socket and add assigns that can be used later either in another socket or in the action handler.
7634002
to
7929ca5
Compare
When a channel configures its plugs, it can add some extra options that will be later received in the plug when called.
Travis builds will now lint the project with Credo in strict mode. If any errors are found, the test won't be run.
095b821
to
de8863b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides the comment, it looks amazing!
💙 ❤️ 💛 💜 💚
end | ||
|
||
@impl true | ||
def call(%{private: %{frankt_module: frankt_module}}, _opts) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this line pattern match to this?
def call(%{private: %{frankt_gettext: frankt_gettext, frankt_module: frankt_module}}, _opts) when not is_nil(frankt_gettext) do```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am taking a look at the code again and I think that the when not is_nil(frankt_gettext)
guard can be omitted since it would previously match with the function declaration in line 10.
def call(socket = %{private: %{frankt_gettext: nil}}, _opts) do
invoke_action(socket)
end
The function clause order would be the following one:
- No Gettext configured: go ahead and invoke the action.
- Gettext configured and locale set: go ahead and invoke the action in an internationalized environment.
- Other cases (Gettext configured but no locale set), raise an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally agree on that!
When using the `Frankt.Plug` module, it automatically declares the `Frankt.Plug` behaviour and imports the `Phoenix.Socket.assign/3` function into modules.
2ec41f4
to
a10dfbc
Compare
a10dfbc
to
87a4fb2
Compare
I've been adding some more improvements to the docs and updating the README. The docs still require more improvements and updates, but I think that we can merge this branch and prepare the 1.0 release. The docs can be improved bit by bit over time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This pull request closes #4.
The main objective of this PR is to allow Frankt users to act on the socket before the action handler is executed.
Acting on the socket
In some projects we have found the need of performing certain actions on the socket even before running the action handler. For example: assigning certain values in the socket or setting up monitoring instrumentation.
Before this PR there was no way to perform those actions on the socket, so the only possibility was to throw all those responsibilities into the action handler. It was soon obvious that we can handle those cases in a better way.
We took the inspiration from the Plug package. Frankt channels can now implement a
plugs
function which returns a list of modules implementing theFrankt.Plug
behaviour. Those modules are run in order and can do whathever they want with the socket: assigning or modifying values, pushing messages, setting up instrumentation, etc.As way to test the implementation and provide live documentation we reimplemented Frankt internals as a series of plugs, the last one being the action handler provided by the user. This has led to clearer and more modular code, while maintaing Frankt public interface unmodified.
Tasks
This is a detailed list of the tasks that have been done in this pull request:
I've also added Credo as a direct dependency so we can control the linting configuration and use the new checks that are not included in Ebert.