import { NodeCG } from "nodecg-types/types/server" ; import { ServiceManager } from "./serviceManager" ; import { BundleManager } from "./bundleManager" ; import { MessageManager } from "./messageManager" ; import { InstanceManager } from "./instanceManager" ; import { Service } from "./service" ; import { PersistenceManager } from "./persistenceManager" ; import { ServiceProvider } from "./serviceProvider" ; import { Logger } from "./utils/logger" ; /**
 * Main type of NodeCG extension that the core bundle exposes.
 * Contains references to all internal modules.
 */ export interface NodeCGIOCore { registerService < R , C > ( service ) : void ; requireService < C > ( nodecg , serviceType ) : ServiceProvider < C > | undefined ; } module . exports = ( nodecg ) : NodeCGIOCore => { nodecg . log . info ( "Minzig!" ) ; const serviceManager = new ServiceManager ( nodecg ) ; const bundleManager = new BundleManager ( nodecg ) ; const instanceManager = new InstanceManager ( nodecg , serviceManager , bundleManager ) ; const persistenceManager = new PersistenceManager ( nodecg , serviceManager , instanceManager , bundleManager ) ; new MessageManager ( nodecg , serviceManager , instanceManager , bundleManager , persistenceManager ) . registerMessageHandlers ( ) ; registerExitHandlers ( nodecg , bundleManager , instanceManager , serviceManager , persistenceManager ) ; return { registerService < R , C > ( service ) { serviceManager . registerService ( service ) ; } , requireService < C > ( nodecg , serviceType ) { const bundleName = nodecg . bundleName ; const svc = serviceManager . getService ( serviceType ) ; if ( svc . failed ) { nodecg . log . warn ( `The bundle "${ bundleName }" can't require the "${ serviceType }" service: ` + "no service with such name." ) ; return ; } return bundleManager . registerServiceDependency ( bundleName , svc . result as Service < unknown , C > ) ; } } ; } ; function onExit ( nodecg , bundleManager , instanceManager , serviceManager , persistenceManager ) { persistenceManager . save ( ) ; const bundles = bundleManager . getBundleDependencies ( ) ; for ( const bundleName in bundles ) { bundles [ bundleName ] ?. forEach ( ( bundleDependency ) => { if ( bundleDependency . serviceInstance !== undefined ) { bundleDependency . provider . updateClient ( undefined ) ; } } ) ; } const instances = instanceManager . getServiceInstances ( ) ; for ( const key in instances ) { if ( instances [ key ] !== undefined ) { const client = instances [ key ] ?. client ; const service = serviceManager . getService ( instances [ key ] ?. serviceType as string ) ; if ( ! service . failed && client ) { nodecg . log . info ( `Stopping service ${ key } of type ${ service . result . serviceType }.` ) ; try { service . result . stopClient ( client , new Logger ( key , nodecg ) ) ; } catch ( err ) { nodecg . log . info ( `Could not stop service ${ key } of type ${ service . result . serviceType }: ${ String ( err ) }` ) ; } } } } } function registerExitHandlers ( nodecg , bundleManager , instanceManager , serviceManager , persistenceManager ) { const handler = ( ) => { onExit ( nodecg , bundleManager , instanceManager , serviceManager , persistenceManager ) ; } ; process . on ( "exit" , handler ) ; process . on ( "SIGINT" , handler ) ; process . on ( "SIGTERM" , handler ) ; process . once ( "SIGUSR1" , ( ) => { handler ( ) ; process . kill ( process . pid , "SIGUSR1" ) ; } ) ; process . once ( "SIGUSR2" , ( ) => { handler ( ) ; process . kill ( process . pid , "SIGUSR2" ) ; } ) ; process . on ( "uncaughtException" , handler ) ; process . on ( "unhandledRejection" , handler ) ; }