Skip to content

Tracked file reads

With the HMR loader active, synchronous file reads performed during a tracked computation can become dependencies. For example, run this file with hmr app.py and edit message.txt:

from pathlib import Path

print(Path("message.txt").read_text())

Create message.txt before starting. In hmr==0.7.6.3, the audit hook tracks qualifying open events while an HMR computation is active and the path passes a loader's include/exclude filter. A later file-change notification invalidates the dependent computation. No decorator is required for this module-level read.

This is not general filesystem virtualization. Do not assume directory listings, reads outside an active tracked computation, excluded paths, or arbitrary file-descriptor access become dependencies. The watcher currently ignores deletion events; removal alone is not a reliable trigger to re-run a file reader.

track(Path(...)) and notify(Path(...)) are low-level dependency operations, not file readers or watcher setup. Use matching resolved paths when calling them manually.

from reactivity.hmr.fs import *

_filters module-attribute

_filters: list[PathFilter] = []

add_filter module-attribute

add_filter = _filters.append

__all__ module-attribute

__all__ = ('notify', 'setup_fs_audithook', 'track')

PathFilter

PathFilter = Callable[[Path], bool]

fs_signals

fs_signals()
Source code in reactivity/hmr/fs.py
11
12
13
@defaultdict
def fs_signals():
    return Subscribable(context=HMR_CONTEXT)

setup_fs_audithook cached

setup_fs_audithook()
Source code in reactivity/hmr/fs.py
23
24
25
26
27
28
29
30
31
32
33
@cache
def setup_fs_audithook():
    @sys.addaudithook
    def _(event: str, args: tuple):
        if event == "open":
            file, _, flags = args

            if (flags % 2 == 0) and _filters and isinstance(file, str) and HMR_CONTEXT.leaf.current_computations:
                p = Path(file).resolve()
                if any(f(p) for f in _filters):
                    track(p)

track

track(file: Path)
Source code in reactivity/hmr/fs.py
36
37
def track(file: Path):
    fs_signals[file].track()

notify

notify(file: Path)
Source code in reactivity/hmr/fs.py
40
41
def notify(file: Path):
    fs_signals[file].notify()