custom module: add stop() callback (runs when the server stops

This commit is contained in:
jean-emmanuel 2024-07-15 11:03:07 +02:00
parent c56a5bfbeb
commit 026230d8e1
6 changed files with 59 additions and 11 deletions

View file

@ -12,6 +12,7 @@
- custom module
- provide error backtraces in module.exports functions
- add `reload()` callback to provide finer control over the module's lifecycle
- add `stop()` callback (runs when the server stops)
- remote control
- add /SESSION/CREATE command

View file

@ -10,6 +10,7 @@ Using the server's `custom-module` option, users can load a custom javascript mo
// Do whatever you want
// initialize variables
// declare functions
// listen to app events
// load modules
// etc
@ -21,6 +22,11 @@ module.exports = {
// it is called for the main module only
},
stop: function(){
// this will be executed once when the osc server stops
// 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

View file

@ -210,6 +210,18 @@ class CustomModule {
}
}
stop() {
if (this.module.exports && this.module.exports.stop) {
try {
this.module.exports.stop()
} catch(e) {
console.error('(ERROR, CUSTOM MODULE) Error while calling custom module stop function')
console.error(e)
}
}
}
setTimeout(fn, t) {
var timeout = setTimeout(fn, t)
this.timeouts.push(timeout)

View file

@ -115,9 +115,6 @@ if (process.platform === 'darwin') {
)
}
app.on('before-quit',()=>{
process.exit()
})
app.on('ready',()=>{
var menu = Menu.buildFromTemplate(template)

View file

@ -175,21 +175,21 @@ function startServerProcess() {
function stopServerProcess() {
if (settings.read('no-gui')) {
if (serverProcess) serverProcess.kill()
if (serverProcess) serverProcess.kill('SIGINT')
return
}
var toClose = [...clientWindows].filter(w=>w && !w.isDestroyed()),
closed = 0
if (toClose.length === 0) serverProcess.kill()
if (toClose.length === 0) serverProcess.kill('SIGINT')
for (var w of toClose) {
w.on('closed', ()=>{
closed++
if (closed === toClose.length) {
clientWindows = []
if (serverProcess) serverProcess.kill()
if (serverProcess) serverProcess.kill('SIGINT')
}
})
w.close()
@ -283,10 +283,26 @@ if (settings.read('docs')) {
server.bindCallbacks(callbacks)
serverStarted = true
process.on('exit',()=>{
if (osc.midi) osc.midi.stop()
var closeServer = ()=>{
osc.server.stop()
zeroconf.unpublishAll()
})
}
try {
let {app} = require('electron')
app.on('ready', ()=>{
process.on('SIGINT', function() {
closeServer()
process.exit(0)
})
})
} catch(e) {
process.on('SIGINT', function() {
closeServer()
process.exit(0)
})
}
if (!process.env.OSC_SERVER_PROCESS) server.eventEmitter.on('serverStarted', ()=>{
if (!settings.read('no-qrcode')) showQRCode()
@ -299,8 +315,17 @@ if (settings.read('docs')) {
// - node process: server (node mode in a forked process)
app = require('./electron-app')
process.on('exit',()=>{
if (serverProcess) serverProcess.kill()
app.on('ready', ()=>{
process.on('SIGINT', function() {
if (serverProcess) serverProcess.kill('SIGINT')
process.exit(0)
})
})
app.on('before-quit',()=>{
console.log('PARENT PROCESS QUIT')
if (serverProcess) serverProcess.kill('SIGINT')
})

View file

@ -217,6 +217,13 @@ class OscServer {
}
stop() {
if (this.customModule.stop) this.customModule.stop()
if (midi) midi.stop()
}
}