diff --git a/CHANGELOG.md b/CHANGELOG.md index 006b73c5..38b339a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - script: issue when using the `options` argument in `set()` (options leaked to subsequent set() calls in the script) - multixy: `decimals` property not applied - multixy: spring behavior not working until all points are released + - custom module: prevent require() from reading submodules files each time and instead return the object in memory # 1.8.8 diff --git a/src/server/custom-module.js b/src/server/custom-module.js index a932abd0..f95297ae 100644 --- a/src/server/custom-module.js +++ b/src/server/custom-module.js @@ -20,7 +20,7 @@ class CustomModule { this.timeouts = [] this.intervals = [] - this.submodules = [] + this.submodules = {} this.context = this.submodule ? context : { ...context, @@ -153,10 +153,10 @@ class CustomModule { this.context.app.removeAllListeners(name) } - for (let mod of this.submodules) { - mod.unload() + for (let name in this.submodules) { + this.submodules[name].unload() } - this.submodules = [] + this.submodules = {} if (this.submodule) { this.watcher.close() @@ -204,10 +204,13 @@ class CustomModule { require(filename) { - var mod = new CustomModule(filename, this.context, this.parent || this) - this.submodules.push(mod) + if (!this.submodules[filename]) { - return mod.exports + this.submodules[filename] = new CustomModule(filename, this.context, this.parent || this) + + } + + return this.submodules[filename].exports }