Mob.Router.Hooks (mob v0.9.5)

Copy Markdown View Source

Extension points plugins register on the router at runtime — typically from their lifecycle.on_start, which runs before the app's own on_start starts the root screen. Nothing to configure in the app.

:before_navigate

Called with the destination (a screen module or a route atom) before a navigation that mounts a screen — push_screen, reset_to (either form). Pops and pop_to go back to screens already mounted and don't call it. The hook returns:

  • :ok — navigate as usual.
  • {:redirect, module} — mount module instead (e.g. a "please update" screen).
  • {:error, reason} — refuse: navigation is left untouched and the current screen repaints, as for an unknown destination.

Hooks run in the order registered; the first non-:ok answer wins. A hook that raises, exits, or returns anything else counts as {:error, _}. It runs in the router process, so it must be fast when there's nothing to do — a hook that has to fetch code (mob_deliver's just-in-time screens) blocks navigation while it does.

:after_first_render

Called once per VM, in its own process, after the router's first paint of the root screen: the supervision tree is up and the first screen has mounted and been handed to the renderer. Not called by test routers that don't render (Mob.Router.start_link/3).

Mob.Router.Hooks.register(:before_navigate, {MyPlugin, :before_navigate, []})
Mob.Router.Hooks.register(:after_first_render, {MyPlugin, :first_render, []})

The destination is appended to a :before_navigate hook's arguments; :after_first_render hooks get the arguments as given.

Summary

Functions

Registers mfa for hook. Registering the same MFA twice is a no-op.

Removes mfa from hook.

Types

hook()

@type hook() :: :before_navigate | :after_first_render

mfa_hook()

@type mfa_hook() :: {module(), atom(), list()}

verdict()

@type verdict() :: :ok | {:redirect, module()} | {:error, term()}

Functions

register(hook, mfa)

@spec register(hook(), mfa_hook()) :: :ok

Registers mfa for hook. Registering the same MFA twice is a no-op.

unregister(hook, mfa)

@spec unregister(hook(), mfa_hook()) :: :ok

Removes mfa from hook.