custom module: expose global object that persists accross module reloads.

This commit is contained in:
jean-emmanuel 2022-11-29 08:36:09 +01:00
parent 9197eea0ac
commit 6ef1d412f9
3 changed files with 10 additions and 2 deletions

View file

@ -11,6 +11,9 @@
- widgets
- switch/menu/dropdown: add a syntax that supports duplicate labels in `values` property
- custom module
- expose `global` object that persists accross module reloads.
## 1.20.0
- bug fixes

View file

@ -137,6 +137,7 @@ This function can be used to load native node modules or locally installed modul
- `__dirname`
- `__filename`
- `process`
- `global`
----
@ -145,6 +146,8 @@ This function can be used to load native node modules or locally installed modul
Custom modules (including submodules loaded with `require()`) are reloaded automatically when they are modified. Upon reload, timers (`setTimeout` and `setInterval`) and event listeners (added to the  `app` object) are reset.
The `global` object persists accross reloads.
??? "Reloading native modules"
When using modules loaded with `nativeRequire()`, you may need to tell the custom module how to unload some components to allow reloading using the `module.exports.unload` definition. For example, if a port is bound using the `http` module, you'll need to unbind it otherwise the custom module will fail to bind it again when reloading:

View file

@ -4,7 +4,8 @@ var settings = require('./settings'),
chokidar = require('chokidar'),
path = require('path'),
modulePathExtended = false,
loadedModules = {}
loadedModules = {},
globalObject = {}
class CustomModule {
@ -71,7 +72,8 @@ class CustomModule {
clearInterval: this.clearInterval.bind(this),
require: this.require.bind(this),
nativeRequire: require,
process: process
process: process,
global: globalObject
}
if (!this.submodule && !modulePathExtended) {