@@ -10,6 +10,8 @@ import {
1010 protocol ,
1111} from 'electron'
1212import * as electronLocalShortcut from 'electron-localshortcut'
13+ import * as fs from 'fs'
14+ import * as os from 'os'
1315import { autoUpdater } from 'electron-updater'
1416const dev = require ( 'electron-is-dev' )
1517
@@ -20,6 +22,38 @@ const { newWindowConfig } = require('./utils')
2022const server = 'https://hazel-wmigqegsed.now.sh'
2123const feed = `${ server } /update/${ process . platform } /${ app . getVersion ( ) } `
2224
25+ app . setAsDefaultProtocolClient ( 'graphql-playground' )
26+
27+ const log = {
28+ info : ( ...args ) => {
29+ console . log ( ...args )
30+ // fs.appendFileSync(
31+ // os.homedir() + '/pg-logs.log',
32+ // JSON.stringify(args) + '\n',
33+ // )
34+ } ,
35+ }
36+
37+ log . info ( protocol )
38+ // log.info(protocol.registerStringProtocol)
39+
40+ // protocol.registerBufferProtocol('graphql-playground', (request, callback) => {
41+ // setTimeout(() => {
42+ // forceSend('OpenUrl', request.url)
43+ // }, 5000)
44+ // callback()
45+ // })
46+
47+ app . on ( 'open-url' , ( event , url ) => {
48+ event . preventDefault ( )
49+ forceSend ( 'OpenUrl' , url )
50+ } )
51+
52+ app . on ( 'open-file' , ( event , path ) => {
53+ event . preventDefault ( )
54+ forceSend ( 'OpenSelectedFile' , path )
55+ } )
56+
2357function getFocusedWindow ( ) : any | null {
2458 return BrowserWindow . getFocusedWindow ( )
2559}
@@ -54,30 +88,24 @@ function getFocusedWindow(): any | null {
5488function send ( channel : string , arg : string ) {
5589 const focusedWindow = getFocusedWindow ( )
5690 if ( focusedWindow ) {
57- console . log ( 'sending to focused window' , channel , arg )
91+ log . info ( 'sending to focused window' , channel , arg )
5892 focusedWindow . webContents . send ( channel , arg )
5993 } else {
60- console . log ( 'no focused window' )
94+ log . info ( 'no focused window' )
6195 }
6296}
6397
98+ const readyWindowsPromises = { }
99+
64100async function forceSend ( channel : string , arg : string ) {
65101 await appPromise
66102 const currentWindows = BrowserWindow . getAllWindows ( )
67103 let window = currentWindows [ 0 ]
68104 if ( ! window ) {
69105 window = createWindow ( )
70- console . log ( 'created window' )
71- await new Promise ( r => {
72- console . log ( 'waiting for dom to be ready' )
73- window . webContents . addListener ( 'dom-ready' , r )
74- } )
75- await new Promise ( r => setTimeout ( r , 200 ) )
76- console . log ( 'did finish load' )
77106 }
78- // send(channel, arg)
79- // console.log('window')
80- console . log ( 'force sending' , channel , arg )
107+ await readyWindowsPromises [ window . id ]
108+ log . info ( 'force sending' , channel , arg )
81109 window . webContents . send ( channel , arg )
82110}
83111
@@ -147,12 +175,12 @@ function createWindow() {
147175 } = require ( 'electron-devtools-installer' )
148176
149177 installExtension ( REACT_DEVELOPER_TOOLS )
150- . then ( name => console . log ( `Added Extension: ${ name } ` ) )
151- . catch ( err => console . log ( 'An error occurred: ' , err ) )
178+ . then ( name => log . info ( `Added Extension: ${ name } ` ) )
179+ . catch ( err => log . info ( 'An error occurred: ' , err ) )
152180
153181 installExtension ( REDUX_DEVTOOLS )
154- . then ( name => console . log ( `Added Extension: ${ name } ` ) )
155- . catch ( err => console . log ( 'An error occurred: ' , err ) )
182+ . then ( name => log . info ( `Added Extension: ${ name } ` ) )
183+ . catch ( err => log . info ( 'An error occurred: ' , err ) )
156184
157185 // newWindow.webContents.openDevTools()
158186 }
@@ -174,6 +202,11 @@ function createWindow() {
174202 // electronLocalShortcut.register(newWindow, 'Cmd+Shift+[', () => {
175203 // send('Tab', 'Prev')
176204 // })
205+ readyWindowsPromises [ newWindow . id ] = new Promise ( r => {
206+ ipcMain . once ( 'ready' , ( ) => {
207+ r ( )
208+ } )
209+ } )
177210
178211 return newWindow
179212}
@@ -292,35 +325,23 @@ app.on('ready', () => {
292325 Menu . setApplicationMenu ( menu )
293326
294327 ipcMain . on ( 'get-file-data' , event => {
295- console . log ( 'get-file-data' , event )
328+ log . info ( 'get-file-data' , event )
296329 // this.fileAdded(event)
297330 } )
298331
299332 ipcMain . on ( 'load-file-content' , ( event , filePath ) => {
300- console . log ( 'load-file-content' , event , filePath )
333+ log . info ( 'load-file-content' , event , filePath )
301334 } )
302335
303336 protocol . registerFileProtocol ( 'file:' , ( request , filePath ) => {
304- console . log ( 'file:' , request , filePath )
337+ log . info ( 'file:' , request , filePath )
305338 } )
306339
307340 if ( appResolve ) {
308341 appResolve ( )
309342 }
310343} )
311344
312- app . setAsDefaultProtocolClient ( 'graphql-playground' )
313-
314- app . on ( 'open-url' , ( event , url ) => {
315- event . preventDefault ( )
316- forceSend ( 'OpenUrl' , url )
317- } )
318-
319- app . on ( 'open-file' , ( event , path ) => {
320- event . preventDefault ( )
321- forceSend ( 'OpenSelectedFile' , path )
322- } )
323-
324345// Quit when all windows are closed.
325346app . on ( 'window-all-closed' , ( ) => {
326347 // On OS X it is common for applications and their menu bar
0 commit comments