@palisarbaro/stateful_hot_reload
v1.0.1
Published
A simple hot reload library for node.js that allows you to preserve application state across reloads.
Readme
A simple hot reload library for node.js that allows you to preserve application state across reloads.
DON'T USE IN PRODUCTION
Why not use nodemon or something similar?
They just reboot the whole application, meaning you lose its state.
This leads to the following problems.
- This slows down reloads because state initialization is slow (connecting to the database, waiting for other services, etc.).
- During development, it can be quite tedious to click buttons in the browser to get the app state suitable for testing a specific feature. Losing the app state forces you to repeat this process over and over again.
Installation
npm i @palisarbaro/stateful_hot_reload
Limitations
- You should not relay on require cache
- Works only with commonjs modules
mainfunction shouldn't throw (otherwise saving invalid code can terminate node process)
Usage
- You should have
mainfunction that accept boolean (true if reloaded, false if first run) and returnshutdownfunction. - Do the state inititalization.
- Only then call
hot_reloadingwithmainfunction. - Call
watchto monitor changes. Or calldo_reloadmanually when you need to reload.
Bothmainandshutdowncan be async
hot_reloading
Immidiatly calls main
do_reload
Stops the previous run by calling shutdown
Clears require cache, so you can reimport new version of code.
Calls main again.
watch
root - directory to watchignore - array of RegExp applied to full path of watched files (default is [/\/node_modules\//])debounce_timeout - delay(milliseconds) before reload to prevent multiple reloads when saving multiple files (default is 100 ms)
