Skip to content

The module loader

rfsuite.require(path) (src/rfsuite/lib/require.lua) loads a suite module once and hands the same result to every later caller. A relative path is taken under the code root, rfsuite.paths.core (see below); the loaded results are kept in rfsuite.modules, keyed by that full path.

Card paths

Where the suite sits on the card is stated once, in src/rfsuite/lib/paths.lua:

Field Value today Holds
core /SCRIPTS/RFSUITE/core the suite's code
user /SCRIPTS/RFSUITE/user the pilot's data: preferences, flight log, user themes
legacyUser /SCRIPTS/TOOLS/rfsuite.user where an earlier install kept that data, read only to move it

lib/require.lua loads that file and publishes it as rfsuite.paths. Every Lua state of the suite -- the tool, both widgets and the special-function script -- loads lib/require.lua before any other suite module, so every module can build its paths from the table instead of spelling them: loadScript(rfsuite.paths.core .. "/lib/log.lua", mode). Neither root ends in a slash. If lib/require.lua cannot be loaded or run, no module finds its paths, and the state does not start.

lib/require.lua also decides, once per Lua state, which folder user means. While the old folder exists and the new one holds no moved.txt, user stays the old folder and moveTo names the new one; lib/precompile.lua then runs the copy (lib/user_move.lua). A module keeps the path it read when it loaded, so the switch happens at the next start and never inside a running state.

Only lib/paths.lua itself, lib/require.lua (to load it), the files that load lib/require.lua (src/main.lua, the two widgets' main.lua, src/functions/rfsbg.lua), and the model template (src/templates/5.Rotorflight/Rotorflight.lua), which runs without it, name the code root directly. bin/core/verify_card_path_literals.lua fails on any other file that does.

A desktop test that loads a suite module without lib/require.lua provides the table itself, from the same file, for example _G.rfsuite.paths = dofile("src/rfsuite/lib/paths.lua") when it runs from the repository root.

The sound packs are outside both roots, at /SOUNDS/rf/<language>/, with the tones directly in /SOUNDS/rf/. A release archive carries the pack of its own language only, or the English pack for a translation the suite does not recognise as an announcement language (bin/package/build_package.py, stage_sounds). lib/audio_playback.lua (resolveEventPath) looks for an announcement's file in /SOUNDS/rf/<language>/, then in the radio's own /SOUNDS/<language>/, then in /SOUNDS/rf/<package language>/ -- the language the archive was built for, lib/system_locale.lua packageLanguage(), or en from source -- and remembers the answer, a miss included, for the life of the Lua state.

What a load returns

The module's file rfsuite.require returns Kept in the cache
returns a value (a table, a function, false, ...) that value yes -- later calls get the same value without running the file again
returns nothing nil no
cannot be read or compiled nil and the error, and prints a [require] Failed to load line when the radio gives a reason no
raises an error while it runs nil and the error, and prints a [require] Error executing line no

A caller can therefore treat any falsy result as "not available", which is what rfsuite.require(path) or false followed by if not mod relies on.

A module that has to run once returns its table

Only a value is kept. A module that returns nothing is run again on every rfsuite.require of it, as is a file that failed to load, so a module whose body must run only once ends in return M.

A failed load is not remembered, so the caller remembers it

Because a failure is not cached, a caller that asks again on every pass loads the file again every time -- from the card, unless the tool already holds its compiled chunk (see below). Callers that run on every pass therefore keep the answer themselves:

  • SmartFuel (tasks/events/telemetry_bg/smart.lua) records false for a module it did not load and does not ask for it again until the Lua state restarts.
  • The background telemetry runner (tasks/events/telemetry_bg/tasks.lua), the custom telemetry drain (tasks/events/telemetry_bg/drain.lua; both have a loader of their own -- loadScript and pcall, not rfsuite.require) and the MSP API version read, which every connection waits on, ask again for a module that did not load after one second rather than on every pass. For the runner that is the drain, SmartFuel and the adjustment teller; for the drain its decoder table (lib/rf2tlm_sensors.lua) and lib/crsf.lua, and while one is missing it takes nothing off the link. A load can fail without the file being broken -- the loader's pcall also catches the radio's instruction limit when it stops a pass while the module's chunk runs -- so a failure that does not last costs about a second. A stop that lands in Lua EdgeTX calls itself -- a widget's get function, the reactive sweep -- reaches no pcall of the suite's and disables the widget; that is why the tool opened from the dashboard makes a page's widgets on the pass after the one that loads and builds it (see Dashboard views).
  • The background function script (functions/rfsbg.lua) waits the same second before it asks again for the drain, which it loads when it starts, and for the adjustment teller, which it loads only while Adjustment events is on.

Reloading and releasing

  • rfsuite.require(path, true) runs the file again even when it is cached. If it now returns nothing, the cached value is dropped; if it fails to load or raises, the cached value stays.
  • rfsuite.clearModule(path) drops one entry and rfsuite.clearAllModules() drops all of them; the next rfsuite.require runs the module again. In the tool, loadScript keeps the compiled chunk of most files (src/main.lua), so that run need not read the card.

Chunks a page lets go when the pilot leaves its area

The tool's loadScript keeps a compiled chunk until the tool closes, except a page's own page.lua -- in the tool started from the Tools menu (src/main.lua) and in the tool opened from the dashboard (widgets/dashboard/tool_host.lua). A page module can ask for the chunks under its own folder to go earlier, by naming the menu its entries sit in:

local M = { visitMenu = "helicopter_setup_menu" }

When the page is released, the page registry (app/pages/init.lua) notes the page's folder. On every change of menu or page the tool then calls registry.endVisits(currentMenuId) (ui/home.lua): while the pilot is on that menu or on a page whose file is in that folder, the chunks stay; once neither holds, the registry calls rfsuite.utils.releaseChunksUnder(folder), which both loaders publish for their own cache, and the next visit reads the files from the card again. So several menu entries that open the same page keep its code while the pilot moves between them.

Only chunks under the page's folder go. Files it loads from elsewhere -- MSP APIs, libraries other pages use too -- stay cached, and no module instance is touched: the page drops its own in its onClose, and a module someone else still holds keeps its code alive. Nothing is collected at that moment, because the old scene is still on screen until the next build replaces it. A menu change the pilot makes -- opening an entry or going back -- asks for a collection right after the new screen is built (ui/home.lua), and that is when the memory comes back; on other routes out of the area it returns with the collector's own work. The release walks only the kept chunks of files under app/pages/, which each loader lists as it caches them, not the whole cache. With the debug log level, the tool writes a line page area left for <menu>: N visit(s) ended, M chunk(s) let go.

The helicopter setup is the page that uses it. It also reads each step's file past the cache (rfsuite.utils.loadScriptUncached) and holds only the current step's chunk on its controller: a card set rebuilt on the same step reuses it, and once another step is built the old step's code goes with its cards.