diff --git a/resources/docs/docs/custom-module/custom-module.md b/resources/docs/docs/custom-module/custom-module.md index 6faeff91..a79cf24e 100644 --- a/resources/docs/docs/custom-module/custom-module.md +++ b/resources/docs/docs/custom-module/custom-module.md @@ -16,7 +16,14 @@ Using the server's `custom-module` option, users can load a custom javascript mo module.exports = { init: function(){ - // this will be executed once when the osc server starts + // this will be executed once when the osc server starts after + // connections are set up + // it is called for the main module only + }, + + reload: function(){ + // this will be executed after the custom module is reloaded + // it is called for the main module only }, oscInFilter:function(data){ @@ -48,7 +55,8 @@ module.exports = { }, unload: function(){ - // this will be executed when the custom module is reloaded + // this will be executed before the custom module is reloaded + // it is called for all modules, including other loaded modules }, } @@ -168,7 +176,7 @@ for (var ip in tcpServer.clients) { ## Autoreload -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. +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. After each reload the `module.exports.reload()` function (if any) is called. The `global` object persists accross reloads. diff --git a/resources/docs/docs/custom-module/examples.md b/resources/docs/docs/custom-module/examples.md index b6e605cf..ffbfe6dc 100644 --- a/resources/docs/docs/custom-module/examples.md +++ b/resources/docs/docs/custom-module/examples.md @@ -378,6 +378,11 @@ module.exports = { if (m.init) m.init() } }, + reload: function(){ + for (var m of submodules) { + if (m.reload) m.reload() + } + }, oscInFilter: function(data){ for (var m of submodules) { diff --git a/src/server/custom-module.js b/src/server/custom-module.js index b70ebfbe..1bbc1593 100644 --- a/src/server/custom-module.js +++ b/src/server/custom-module.js @@ -195,6 +195,15 @@ class CustomModule { console.log('(INFO) Reloading custom module...') if (this.load()) { console.log('(INFO) Custom module reloaded successfully') + + if (this.module.exports && this.module.exports.reload) { + try { + this.module.exports.reload() + } catch(e) { + console.error('(ERROR) Error while calling custom module reload function') + console.error(e) + } + } } else { console.error('(ERROR) Failed to reload custom module') this.unload()