View Source LiveGuard.GuardedStages protocol (Live Guard v0.1.8)

Optional

By this protocol you can implement guarded_stages/1 functions.

Summary

Types

t()

A LiveView module.

Functions

Optional

This function is for optimization.

Types

@type t() :: module()

A LiveView module.

Functions

Link to this function

guarded_stages(live_view_module)

View Source
@spec guarded_stages(live_view_module :: module()) :: [
  LiveGuard.attachable_lifecycle_stages()
]

Optional

This function is for optimization.

By default if you use the on_mount/4 callback of LiveGuard, it will attach hooks to attachable LiveView lifecycle stages (:handle_params, :handle_event, :handle_info and :handle_async).

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 (unless :after_render).

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 the attachable LiveView lifecycle stages (:handle_params, :handle_event, :handle_info and :handle_async). This is optional.