@@ -10,6 +10,8 @@ import {
10
10
protocol ,
11
11
} from 'electron'
12
12
import * as electronLocalShortcut from 'electron-localshortcut'
13
+ import * as fs from 'fs'
14
+ import * as os from 'os'
13
15
import { autoUpdater } from 'electron-updater'
14
16
const dev = require ( 'electron-is-dev' )
15
17
@@ -20,6 +22,38 @@ const { newWindowConfig } = require('./utils')
20
22
const server = 'https://hazel-wmigqegsed.now.sh'
21
23
const feed = `${ server } /update/${ process . platform } /${ app . getVersion ( ) } `
22
24
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
+
23
57
function getFocusedWindow ( ) : any | null {
24
58
return BrowserWindow . getFocusedWindow ( )
25
59
}
@@ -54,30 +88,24 @@ function getFocusedWindow(): any | null {
54
88
function send ( channel : string , arg : string ) {
55
89
const focusedWindow = getFocusedWindow ( )
56
90
if ( focusedWindow ) {
57
- console . log ( 'sending to focused window' , channel , arg )
91
+ log . info ( 'sending to focused window' , channel , arg )
58
92
focusedWindow . webContents . send ( channel , arg )
59
93
} else {
60
- console . log ( 'no focused window' )
94
+ log . info ( 'no focused window' )
61
95
}
62
96
}
63
97
98
+ const readyWindowsPromises = { }
99
+
64
100
async function forceSend ( channel : string , arg : string ) {
65
101
await appPromise
66
102
const currentWindows = BrowserWindow . getAllWindows ( )
67
103
let window = currentWindows [ 0 ]
68
104
if ( ! window ) {
69
105
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' )
77
106
}
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 )
81
109
window . webContents . send ( channel , arg )
82
110
}
83
111
@@ -147,12 +175,12 @@ function createWindow() {
147
175
} = require ( 'electron-devtools-installer' )
148
176
149
177
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 ) )
152
180
153
181
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 ) )
156
184
157
185
// newWindow.webContents.openDevTools()
158
186
}
@@ -174,6 +202,11 @@ function createWindow() {
174
202
// electronLocalShortcut.register(newWindow, 'Cmd+Shift+[', () => {
175
203
// send('Tab', 'Prev')
176
204
// })
205
+ readyWindowsPromises [ newWindow . id ] = new Promise ( r => {
206
+ ipcMain . once ( 'ready' , ( ) => {
207
+ r ( )
208
+ } )
209
+ } )
177
210
178
211
return newWindow
179
212
}
@@ -292,35 +325,23 @@ app.on('ready', () => {
292
325
Menu . setApplicationMenu ( menu )
293
326
294
327
ipcMain . on ( 'get-file-data' , event => {
295
- console . log ( 'get-file-data' , event )
328
+ log . info ( 'get-file-data' , event )
296
329
// this.fileAdded(event)
297
330
} )
298
331
299
332
ipcMain . on ( 'load-file-content' , ( event , filePath ) => {
300
- console . log ( 'load-file-content' , event , filePath )
333
+ log . info ( 'load-file-content' , event , filePath )
301
334
} )
302
335
303
336
protocol . registerFileProtocol ( 'file:' , ( request , filePath ) => {
304
- console . log ( 'file:' , request , filePath )
337
+ log . info ( 'file:' , request , filePath )
305
338
} )
306
339
307
340
if ( appResolve ) {
308
341
appResolve ( )
309
342
}
310
343
} )
311
344
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
-
324
345
// Quit when all windows are closed.
325
346
app . on ( 'window-all-closed' , ( ) => {
326
347
// On OS X it is common for applications and their menu bar
0 commit comments