-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
main.ts
43 lines (35 loc) · 1.16 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import type { UnknownAction } from '@reduxjs/toolkit'
import electron, { app } from 'electron'
import { TidyURL } from 'tidy-url'
import { sleep } from 'tings'
import { Channel } from '../shared/state/channels.js'
import { openedUrl, readiedApp } from './state/actions.js'
import { dispatch, getState } from './state/store.js'
app.on('ready', () => dispatch(readiedApp()))
// App doesn't always close on ctrl-c in console, this fixes that
app.on('before-quit', () => app.exit())
app.on('open-url', (event, url) => {
event.preventDefault()
const tidyUrl = TidyURL.clean(url)
const urlOpener = async () => {
if (getState().data.pickerStarted) {
dispatch(openedUrl(tidyUrl.url))
}
// If B was opened by sending it a URL, the `open-url` electron.app event
// can be fired before the picker window is ready. Here we wait before trying again.
else {
await sleep(500)
urlOpener()
}
}
urlOpener()
})
/**
* Enter actions from renderer into main's store's queue
*/
electron.ipcMain.on(Channel.PREFS, (_, action: UnknownAction) =>
dispatch(action),
)
electron.ipcMain.on(Channel.PICKER, (_, action: UnknownAction) =>
dispatch(action),
)