View Source LiveGuard.GuardedStages protocol (Live Guard v0.1.0)
Optional
By this protocol you can implement guarded_stages/1
functions.
Summary
Functions
Optional
This function is for optimization.
Types
@type t() :: term()
All the types that implement this protocol.
Functions
Optional
This function is for optimization.
By default if you use the on_mount/4
callback of LiveGuard, it will attach hooks to all attachable LiveView lifecycle stages (:handle_params
, :handle_event
, :handle_info
and :after_render
).
If you need to protect for example only the :handle_event
LiveView lifecycle stage for an individual LiveView module you can use this function.
You can put this file anywhere but /lib/my_app_web/live/guarded_stages.ex
is recommended.
It must return a list of valid attachable LiveView lifecycle stages.
Example
# /lib/my_app_web/live/guarded_stages.ex
defimpl LiveGuard.GuardedStages, for: Atom do
@before_compile {LiveGuard, :before_compile_guarded_stages}
def guarded_stages(MyModuleLive), do: [:handle_event]
# other `guarded_stages?/1` functions...
end
In this case it will only attach hook to :handle_event
LiveView lifecycle stage.
Note: As you can see, you don't have to define catch-all
guarded_stages/1
function because we used@before_compile {LiveGuard, :before_compile_guarded_stages}
hook. It returns all the attachable LiveView lifecycle stages.