import { app } from "electron" ; import events from "events" ; import { createMainWindow , getOpenWindows , trackWindow } from "./window" ; const urlEventEmitter = new events . EventEmitter ( ) ; const urlEventChannel = "deeplink:url" ; let urlEventQueue = [ ] ; let isWindowReady = false ; export function subscribe ( subscribeCallback ) { urlEventEmitter . on ( urlEventChannel , subscribeCallback ) ; const unsubscribe = ( ) => urlEventEmitter . removeListener ( urlEventChannel , subscribeCallback ) ; return unsubscribe ; } export function windowReady ( ) { isWindowReady = true ; urlEventQueue . forEach ( item => urlEventEmitter . emit ( urlEventChannel , item ) ) ; urlEventQueue = [ ] ; } export function windowDestroyed ( ) { isWindowReady = false ; } function emitURL ( url ) { if ( isWindowReady ) { urlEventEmitter . emit ( urlEventChannel , url ) ; } else { urlEventQueue . push ( url ) ; } } const appReady = new Promise ( resolve => app . on ( "ready" , ( ) => { if ( process . platform === "win32" || process . platform === "linux" ) { if ( process . argv ) { const deeplinkURL = process . argv . slice ( 1 ) [ 0 ] ; if ( deeplinkURL != null && deeplinkURL !== "" ) { emitURL ( deeplinkURL ) ; } } } resolve ( ) ; } ) ) ; app . on ( "will-finish-launching" , ( ) => { app . on ( "open-url" , ( event , url ) => { event . preventDefault ( ) ; emitURL ( url ) ; appReady . then ( ( ) => { if ( getOpenWindows ( ) . length === 0 ) { trackWindow ( createMainWindow ( ) ) ; } } ) ; } ) ; } ) ; const gotSingleInstanceLock = app . requestSingleInstanceLock ( ) ; if ( ! gotSingleInstanceLock ) { app . quit ( ) ; } else { app . on ( "second-instance" , ( event , commandLine , workingDirectory ) => { if ( getOpenWindows ( ) . length === 0 ) { appReady . then ( ( ) => { trackWindow ( createMainWindow ( ) ) ; } ) ; } if ( process . platform === "win32" || process . platform === "linux" ) { const deeplinkURL = commandLine . slice ( 1 ) [ 0 ] ; if ( deeplinkURL != null && deeplinkURL !== "" ) { emitURL ( deeplinkURL ) ; } } } ) ; }