11const { default : OBSWebSocketLib } = require ( 'obs-websocket-js' ) ;
2- import { desktopCapturer } from 'electron' ;
32import type OBSWebSocketType from 'obs-websocket-js' ;
43import { log } from '/@/logger' ;
54import { Setting } from '../database/models/Setting' ;
@@ -13,10 +12,16 @@ export const OBSWebSocket = new class {
1312 private isDisconncting = false ;
1413 private isObsOpen = false ;
1514 private isConnected = false ;
15+ private isConnecting = false ; // Triggered when waiting for the app process to start up
1616 private settings ?: Setting < ObsWebSocketSettings > ;
17+ private psList ?: ( ( ) => Promise < { name : string } [ ] > ) ;
1718
1819 constructor ( ) {
1920 this . obs = new OBSWebSocketLib ( ) ;
21+
22+ import ( 'ps-list' ) . then ( ( { default : psList } ) => {
23+ this . psList = psList ;
24+ } ) ;
2025 }
2126
2227 async init ( ) {
@@ -121,15 +126,25 @@ export const OBSWebSocket = new class {
121126 return ;
122127 }
123128
124- const res = await desktopCapturer . getSources ( { types : [ 'window' , 'screen' ] } ) ;
125- const isOpen = res . find ( ( item ) => item . name . toLocaleLowerCase ( ) . includes ( 'obs' ) ) !== undefined ;
129+ if ( ! this . psList || this . isConnecting ) {
130+ return ;
131+ }
132+
133+ const processes = await this . psList ( ) ;
134+ const isOpen = processes . find ( ( process ) => process . name . toLocaleLowerCase ( ) . includes ( 'obs' ) ) !== undefined ;
126135
127136 // If app was closed and now is open, retrying connection
128137 if ( ! this . isObsOpen && isOpen && this . settings ) {
138+ this . isConnecting = true ;
139+
140+ // When process is detected, give time to app (3seconds) to open etc.
141+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3 * 1000 ) ) ;
142+
129143 this . connect ( ) ;
130144 }
131145
132146 this . isObsOpen = isOpen ;
147+ this . isConnecting = false ;
133148
134149 tellRenderer ( {
135150 subject : ObsWebSocketSubject . AppStatus ,
0 commit comments