From 6ef1d412f939c8af9d62b9725f934825e6cdc2be Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 29 Nov 2022 08:36:09 +0100 Subject: [PATCH] custom module: expose `global` object that persists accross module reloads. --- CHANGELOG.md | 3 +++ resources/docs/docs/custom-module/custom-module.md | 3 +++ src/server/custom-module.js | 6 ++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce024c17..4d6d6840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/resources/docs/docs/custom-module/custom-module.md b/resources/docs/docs/custom-module/custom-module.md index 090f4cb2..b3988bc9 100644 --- a/resources/docs/docs/custom-module/custom-module.md +++ b/resources/docs/docs/custom-module/custom-module.md @@ -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: diff --git a/src/server/custom-module.js b/src/server/custom-module.js index d72dbdbe..dc373f4b 100644 --- a/src/server/custom-module.js +++ b/src/server/custom-module.js @@ -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) {