The description for Phoenix.CodeReloader
starts like this:
For each request, Phoenix goes through all modules and checks if any of them implement a
__phoenix_recompile__?/0
function. If they do and it returns true, the module source file is touched, forcing it to be recompiled. For this functionality to work, Phoenix requires you to add the:phoenix
compiler to your list of compilers: […]
Can someone explain how this works? I’m confused by:
- Where does
__phoenix_recompile__?
come from? - What does adding
:phoenix
to the list of compilers do? - Where does the reloading of a successfully-compiled new version of a source file happen?
What I want to accomplish
To learn LiveView, I want to provide a localhost view into the runtime structure created by a set of modules. The modules have very specific constraints that mean I don’t need to care about a lot of special cases. What I have is:
- a set of modules. Each has a single “constructor” function (all with the same name) that makes a big nested structure.
- a genserver module that maintains a map from module name to big nested structure.
My “watcher” app is to start by loading up all the modules, populating the genserver map, and displaying pretty things in a browser interface.
When one of the module source files is (successfully) changed, I want the watcher app to:
- Notice that a module is out of date (probably via
file_system
). - Delete that module from the genserver.
- Load the new version of the module’s code.
- Run the constructor function and add the new big nested structure to the genserver.
By “very specific constraints” above, I mean, for example, that there are no compile-time dependencies between these modules. There’s an extra level of indirection meaning that the Animal
module does not have to be redefined when the Species
module changes.
Because my case is so simple, it seems like Phoenix.CodeReloader
, phoenix_live_reload
, the vsn
module attribute, and IEX’s r
command are doing things I don’t have to do.
I’m hoping I don’t have to understand complicated cases to implement the simple case.