Hook lifecycle
The behavior below is verified against hmr==0.7.6.3.
from reactivity.hmr import on_dispose
resource = open("output.log", "a")
@on_dispose
def close_resource():
resource.close()
Place this code in a module managed by HMR. on_dispose looks up the reactive module using the caller's __file__ (or its explicit file argument); it is not a general-purpose shutdown decorator for ordinary imported modules.
When that module is loaded again, HMR first parses and compiles the replacement, calls its previously registered disposal callbacks, clears them, and then executes the new source. A syntax error therefore does not dispose the currently loaded module. Exceptions from individual disposal callbacks are suppressed. These callbacks are not guaranteed to run when the watcher stops or the process exits; provide separate final-shutdown cleanup where needed.
Reload-wide callbacks
pre_reload and post_reload register synchronous, zero-argument callbacks around a reload batch. They also run around the initial entry execution. A pre-hook registered by the entry file itself is too late for that first pre phase. A post-hook means the processing phase ended, not that every changed module executed successfully.
Callbacks are registered by function name, not by module or function identity. Registering another callback with the same name replaces the previous one. Use distinct names across modules. use_pre_reload and use_post_reload register only for the duration of a with block and remove that name on exit; they do not restore a previous callback with the same name.
Hooks are called directly, without awaiting returned values. Use ordinary def callbacks, not async def. Unlike module-disposal callbacks, exceptions from pre/post hooks are not suppressed by the hook dispatcher.
from reactivity.hmr.hooks import *
pre_reload_hooks
module-attribute
pre_reload_hooks: dict[str, Callable[[], Any]] = {}
post_reload_hooks
module-attribute
post_reload_hooks: dict[str, Callable[[], Any]] = {}
pre_reload
pre_reload[T](func: Callable[[], T]) -> Callable[[], T]
Source code in reactivity/hmr/hooks.py
|
|
def pre_reload[T](func: Callable[[], T]) -> Callable[[], T]:
pre_reload_hooks[func.__name__] = func
return func
|
post_reload
post_reload[T](func: Callable[[], T]) -> Callable[[], T]
Source code in reactivity/hmr/hooks.py
|
|
def post_reload[T](func: Callable[[], T]) -> Callable[[], T]:
post_reload_hooks[func.__name__] = func
return func
|
use_pre_reload
Source code in reactivity/hmr/hooks.py
|
|
@contextmanager
def use_pre_reload(func):
pre_reload(func)
try:
yield func
finally:
pre_reload_hooks.pop(func.__name__, None)
|
use_post_reload
Source code in reactivity/hmr/hooks.py
|
|
@contextmanager
def use_post_reload(func):
post_reload(func)
try:
yield func
finally:
post_reload_hooks.pop(func.__name__, None)
|
call_pre_reload_hooks
Source code in reactivity/hmr/hooks.py
|
|
def call_pre_reload_hooks():
for func in pre_reload_hooks.values():
func()
|
call_post_reload_hooks
Source code in reactivity/hmr/hooks.py
|
|
def call_post_reload_hooks():
for func in post_reload_hooks.values():
func()
|
on_dispose
on_dispose(
func: Callable[[], Any], __file__: str | None = None
)
Source code in reactivity/hmr/hooks.py
|
|
def on_dispose(func: Callable[[], Any], __file__: str | None = None):
path = Path(currentframe().f_back.f_globals["__file__"] if __file__ is None else __file__).resolve() # type: ignore
from .core import ReactiveModule
module = ReactiveModule.instances[path]
module.register_dispose_callback(func)
|