This allows modules to re-initialize any state they need whenever the
module is reloaded.
Without this, any such initialization can also be done in the top
level of the custom module, but this has two problems:
1. Top-level module code runs on load and reload, and for the initial
load, it runs *before* the connections are set up, so trying to use
send() would produce an error on load (not reload).
2. Top-level module code runs for the primary custom module, but also
for any nested modules, removing control about the process and
ordering from the primary custom module. In addition, a module that
can be used both as primary and nested module, there is no clean way
to distinguish between these cases.
Both of these problems can be solved by doing some bookkeeping in the
global object, but that produces messy code.
This commit allows modules to define a reload() function, which is
called (for the primary module only) after the module is reloaded. This
means that any initialization for the first load can be done in init()
and for reloads in reload(), and in both cases all connections will
already have been set up solving issue 1. In addition, neither of these
functions are called for nested modules, solving issue 2 (and the
primary module can then stil explicitly call a reload function on nested
modules if needed, as shown in the examples).
This applies to errors in e.g. the oscInFilter defined by a custom
plugin module. Previously, just the exception backtrace was shown,
without any indication of the exception type or where the error
occurred, e.g.:
(ERROR, UDP) links is not defined
With this commit, the full exception including backtrace is printed,
making debugging a lot easier, e.g.:
(ERROR, UDP) ReferenceError: links is not defined
at CustomModule.oscInFilter (/app/custom-module.js:221:83)
at OscServer.oscInFilter (/open-stage-control/app/server/src/server/osc/index.js:148:1)
at OscServer.oscInHandler (/open-stage-control/app/server/src/server/osc/index.js:162:1)
at OscServer.oscInHandlerWrapper (/open-stage-control/app/server/src/server/osc/index.js:174:1)
at osc.UDPPort.emit (node:events:394:28)
at Object.osc.firePacketEvents (/open-stage-control/app/server/node_modules/osc/src/osc-transports.js:25:1)
at osc.UDPPort.p.decodeOSC (/open-stage-control/app/server/node_modules/osc/src/osc-transports.js:85:1)