@@ -275,16 +275,32 @@ ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => {
275275} )
276276
277277ipcMain . on ( 'handle-uncaught-exception' , ( event , message ) => {
278- const listeners = process . listeners ( 'uncaughtException' )
279- process . removeAllListeners ( 'uncaughtException' )
280- process . on ( 'uncaughtException' , ( error ) => {
281- process . removeAllListeners ( 'uncaughtException' )
282- listeners . forEach ( ( listener ) => {
283- process . on ( 'uncaughtException' , listener )
284- } )
278+ suspendListeners ( process , 'uncaughtException' , ( error ) => {
285279 event . returnValue = error . message
286280 } )
287281 fs . readFile ( __filename , ( ) => {
288282 throw new Error ( message )
289283 } )
290284} )
285+
286+ ipcMain . on ( 'handle-unhandled-rejection' , ( event , message ) => {
287+ suspendListeners ( process , 'unhandledRejection' , ( error ) => {
288+ event . returnValue = error . message
289+ } )
290+ fs . readFile ( __filename , ( ) => {
291+ Promise . reject ( new Error ( message ) )
292+ } )
293+ } )
294+
295+ // Suspend listeners until the next event and then restore them
296+ const suspendListeners = ( emitter , eventName , callback ) => {
297+ const listeners = emitter . listeners ( eventName )
298+ emitter . removeAllListeners ( eventName )
299+ emitter . once ( eventName , ( ...args ) => {
300+ emitter . removeAllListeners ( eventName )
301+ listeners . forEach ( ( listener ) => {
302+ emitter . on ( eventName , listener )
303+ } )
304+ callback ( ...args )
305+ } )
306+ }
0 commit comments