Phoenix.SessionProcess.ReducerCompiler (phoenix_session_process v1.0.1)

Copy Markdown View Source

Compile-time support for reducer modules.

This module handles the compilation of @throttle and @debounce module attributes into metadata functions that can be queried at runtime.

Module Attributes

@throttle

Throttle an action - execute immediately, then block for duration:

@throttle {"fetch-list", "3000ms"}
def handle_action(%{type: "fetch-list"}, state) do
  # Only executes once per 3 seconds
end

@debounce

Debounce an action - delay execution until duration passes since last call:

@debounce {"update-query", "500ms"}
def handle_action(%{type: "update-query", payload: query}, state) do
  # Waits 500ms after last call before executing
end

Generated Functions

The compiler generates the following metadata functions:

  • __reducer_throttles__/0 - Returns list of {action_pattern, duration} tuples
  • __reducer_debounces__/0 - Returns list of {action_pattern, duration} tuples
  • __reducer_module__/0 - Returns true to mark as a reducer module
  • __reducer_action_prefix__/0 - Returns the action prefix for routing (can be nil)

Summary

Functions

Called before module compilation completes.

Register a debounce attribute.

Register a throttle attribute.

Functions

__before_compile__(env)

(macro)

Called before module compilation completes.

Generates metadata functions based on accumulated module attributes.

register_debounce(module, arg)

Register a debounce attribute.

Called automatically when @debounce is used in a reducer module. Should not be called directly.

register_throttle(module, arg)

Register a throttle attribute.

Called automatically when @throttle is used in a reducer module. Should not be called directly.