Hmr class
Child-side runtime API. Lets the supervised application react to reload lifecycle events the hmr supervisor emits.
Usage from your bin/main.dart:
import 'package:hmr/runtime.dart';
void main() {
Hmr.instance.init();
Hmr.instance.onReload((_) => print('reloaded'));
Hmr.instance.onRestart((_) => print('restarted — re-initialising state'));
runApp();
}
When not running under the supervisor (HMR_PARENT_PID unset), init is a
no-op and registered handlers simply never fire. This means production
builds need no conditional imports.
Constructors
- Hmr.forTesting()
-
Fresh, isolated instance for unit tests. Bypasses the static singleton so
each test starts with an empty handler list.
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isActive → bool
-
True when the process was launched by the hmr supervisor (the supervisor
sets
HMR_PARENT_PIDin the child's environment).no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
dispatch(
RunnerEvent event) → void - Routes an event to every registered handler, isolating errors so a misbehaving hook can never crash the supervised app.
-
init(
) → void -
Registers the
ext.hmr.dispatchVM service extension so the parent can forward events. Safe to call multiple times. No-op when isActive is false, so production code can call it unconditionally. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on<
E extends RunnerEvent> (void handler(E event)) → void -
Registers a handler for events of type
E. Subscribe to RunnerEvent to receive every event. -
onFileCreated(
void handler(FsCreated change)) → void -
onFileDeleted(
void handler(FsDeleted change)) → void -
onFileModified(
void handler(FsModified change)) → void -
onFileMoved(
void handler(FsMoved change)) → void -
onReload(
void handler(ReloadSucceeded event)) → void - Fires after every successful hot reload (state preserved).
-
onRestart(
void handler(ReloadSucceeded event)) → void - Fires after every successful hot restart (state lost — main() re-runs). Use this to re-warm caches, re-open connections, etc.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited