RpcElixir.Watcher (elixir_ts_rpc v0.0.2)

Copy Markdown View Source

Dev-only GenServer that triggers recompilation on router source changes.

It watches the router's own source file and every handler module's source file, via RpcElixir.Router.source_files/1. Middleware and custom-type modules are not watched, even though editing them changes the output.

Requires the optional :file_system dep. Without it, init/1 warns and returns :ignore. The process never starts.

It is Phoenix-specific. Without :endpoint or :on_change there is nothing to do on a change. Non-Phoenix apps do not need it. Mix.Tasks.Compile.ElixirTsRpc already regenerates the client on each Elixir recompile.

Usage

# lib/my_app/application.ex  (Phoenix projects only)
children = [
  {RpcElixir.Watcher, router: MyApp.Router, endpoint: MyAppWeb.Endpoint}
]

Options

  • :router (required) — the RPC router module. A missing :router raises KeyError, even when :file_system is absent.

  • :endpoint — a Phoenix endpoint. The watcher calls Phoenix.CodeReloader.reload/1 on each relevant change. That reloads only the endpoint's :reloadable_compilers, which by default excludes :elixir_ts_rpc. So add it there, or the client is not regenerated:

    reloadable_compilers: [:gettext, :elixir, :app, :elixir_ts_rpc]

    Use :on_change instead to call codegen directly.

  • :on_change{mod, fun, args} invoked on change. Takes precedence over :endpoint when both are given.

  • :debounce_ms — coalesces rapid file events. Defaults to 200.

Restart expectations

The linked FileSystem process starts inside init/1. The watcher traps exits, so a FileSystem crash arrives as a message rather than killing the watcher silently. The watcher then stops with {:filesystem_exited, reason}. The supervisor is expected to restart the pair.

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts the watcher and links it to the caller.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts)

Starts the watcher and links it to the caller.

opts takes the module options listed above, plus :name. The default name is RpcElixir.Watcher. Returns :ignore when :file_system is not loaded.