TermUI.Dev.HotReload (TermUI v0.2.0)

View Source

Hot Reload integration for development mode.

Watches .ex files for changes and reloads modules without restarting the application. State is preserved across reloads where possible.

Usage

# Start hot reload
HotReload.start()

# Stop hot reload
HotReload.stop()

# Manually reload a module
HotReload.reload_module(MyModule)

How It Works

  1. File watcher monitors lib/ directory for .ex changes
  2. On change, affected modules are identified
  3. Modules are recompiled using Mix
  4. Old code is purged and new code loaded
  5. Notification sent to UI

Note: Uses polling-based approach for compatibility.

Summary

Functions

Checks if a module can be hot reloaded.

Returns a specification to start this module under a supervisor.

Gets the source file path for a module.

Gets recently reloaded modules.

Sets callback for reload notifications.

Manually reloads a specific module.

Returns whether hot reload is running.

Starts watching for file changes.

Starts the hot reload watcher.

Stops watching for file changes.

Types

state()

@type state() :: %{
  enabled: boolean(),
  watched_dirs: [String.t()],
  file_mtimes: %{required(String.t()) => integer()},
  on_reload: (module() -> any()) | nil
}

Functions

can_reload?(module)

@spec can_reload?(module()) :: boolean()

Checks if a module can be hot reloaded.

Some modules (like those with NIFs or ports) may not reload properly.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_module_source(module)

@spec get_module_source(module()) :: String.t() | nil

Gets the source file path for a module.

get_recent_reloads()

@spec get_recent_reloads() :: [{module(), DateTime.t()}]

Gets recently reloaded modules.

on_reload(callback)

@spec on_reload((module() -> any())) :: :ok

Sets callback for reload notifications.

reload_module(module)

@spec reload_module(module()) :: :ok | {:error, term()}

Manually reloads a specific module.

running?()

@spec running?() :: boolean()

Returns whether hot reload is running.

start()

@spec start() :: :ok

Starts watching for file changes.

start_link(opts \\ [])

Starts the hot reload watcher.

stop()

@spec stop() :: :ok

Stops watching for file changes.