custom module: prevent require() from reading submodules files each time and instead return the object in memory

This commit is contained in:
jean-emmanuel 2021-02-22 12:17:54 +01:00
parent b4e841906e
commit ba2c25b998
2 changed files with 11 additions and 7 deletions

View file

@ -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

View file

@ -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
}