Merge pull request #851 from matthijskooijman/custom-module-reload-function

custom-module: Call reload() function on auto-reload
This commit is contained in:
jean-emmanuel 2024-06-22 12:00:52 +02:00 • committed by GitHub
commit a21e789437
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 5 deletions

View file

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

View file

@ -378,9 +378,9 @@ module.exports = {
if (m.init) m.init()
}
},
unload: function(){
reload: function(){
for (var m of submodules) {
if (m.unload) m.unload()
if (m.reload) m.reload()
}
},
oscInFilter: function(data){

View file

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