@@ -19,6 +19,9 @@ export class InstanceManager extends EventEmitter {
1919 private readonly bundles : BundleManager ,
2020 ) {
2121 super ( ) ;
22+ bundles . on ( "reregisterInstance" , ( serviceInstance ?: string ) =>
23+ this . reregisterHandlersOfInstance ( serviceInstance ) ,
24+ ) ;
2225 }
2326
2427 /**
@@ -199,17 +202,14 @@ export class InstanceManager extends EventEmitter {
199202
200203 // Check if a error happened while creating the client
201204 if ( client . failed ) {
202- this . nodecg . log . error (
203- `The "${ inst . serviceType } " service produced an error while creating a client: ${ client . errorMessage } ` ,
204- ) ;
205- inst . client = undefined ;
205+ throw client . errorMessage ; // Error logging happens in catch block
206206 } else {
207207 // Update service instance object
208208 inst . client = client . result ;
209209 }
210210 } catch ( err ) {
211211 this . nodecg . log . error (
212- `The "${ inst . serviceType } " service function produced an error while creating a client: ${ err } ` ,
212+ `The "${ inst . serviceType } " service produced an error while creating a client: ${ err } ` ,
213213 ) ;
214214 inst . client = undefined ;
215215 }
@@ -228,4 +228,46 @@ export class InstanceManager extends EventEmitter {
228228 }
229229 }
230230 }
231+
232+ /**
233+ * Removes all handlers from the service client of the instance and lets bundles readd their handlers.
234+ * @param instanceName the name of the instance which handlers should be re-registred
235+ */
236+ private reregisterHandlersOfInstance ( instanceName ?: string ) : void {
237+ if ( ! instanceName ) return ;
238+
239+ const inst = this . getServiceInstance ( instanceName ) ;
240+ if ( ! inst ) {
241+ this . nodecg . log . error ( `Can't re-register handlers of instance "${ instanceName } ": instance not found` ) ;
242+ return ;
243+ }
244+
245+ const svc = this . services . getService ( inst . serviceType ) ;
246+ if ( svc . failed ) {
247+ this . nodecg . log . error (
248+ `Can't reregister handlers of instance "${ instanceName } ": can't get service: ${ svc . errorMessage } ` ,
249+ ) ;
250+ return ;
251+ }
252+
253+ // Client should be recreated because the Service has no way to reset the handlers.
254+ if ( svc . result . reCreateClientToRemoveHandlers ) {
255+ this . updateInstanceClient ( inst , instanceName , svc . result ) ;
256+ return ;
257+ }
258+
259+ if ( ! svc . result . removeHandlers ) return ; // Service provides no way to remove handlers, thus this service has no handlers
260+
261+ // Remove handlers
262+ try {
263+ svc . result . removeHandlers ( inst . client ) ;
264+ } catch ( err ) {
265+ this . nodecg . log . error (
266+ `Can't re-register handlers of instance "${ instanceName } ": error while removing handlers: ${ err . toString ( ) } ` ,
267+ ) ;
268+ }
269+ // Readd handlers by running the `onAvailable` function of all bundles
270+ // that are using this service instance.
271+ this . bundles . handleInstanceUpdate ( inst , instanceName ) ;
272+ }
231273}
0 commit comments