Skip to content

Commit 2b62a2f

Browse files
author
pompurin404
committed
fix auto update profile
1 parent 6edb0a6 commit 2b62a2f

File tree

9 files changed

+30
-53
lines changed

9 files changed

+30
-53
lines changed

src/main/config/override.ts

-8
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ export async function createOverride(item: Partial<IOverrideItem>): Promise<IOve
5656
switch (newItem.type) {
5757
case 'remote': {
5858
if (!item.url) {
59-
dialog.showErrorBox(
60-
'URL is required for remote script',
61-
'URL is required for remote script'
62-
)
6359
throw new Error('URL is required for remote script')
6460
}
6561
try {
@@ -81,10 +77,6 @@ export async function createOverride(item: Partial<IOverrideItem>): Promise<IOve
8177
}
8278
case 'local': {
8379
if (!item.file) {
84-
dialog.showErrorBox(
85-
'File is required for local script',
86-
'File is required for local script'
87-
)
8880
throw new Error('File is required for local script')
8981
}
9082
const data = item.file

src/main/config/profile.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
119119
switch (newItem.type) {
120120
case 'remote': {
121121
if (!item.url) {
122-
dialog.showErrorBox(
123-
'URL is required for remote profile',
124-
'URL is required for remote profile'
125-
)
126122
throw new Error('URL is required for remote profile')
127123
}
128124
try {
@@ -152,7 +148,7 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
152148
if (headers['subscription-userinfo']) {
153149
newItem.extra = parseSubinfo(headers['subscription-userinfo'])
154150
}
155-
setProfileStr(id, data)
151+
await setProfileStr(id, data)
156152
} catch (e) {
157153
dialog.showErrorBox('Failed to fetch remote profile', `${e}\nurl: ${item.url}`)
158154
throw new Error(`Failed to fetch remote profile ${e}`)
@@ -161,14 +157,10 @@ export async function createProfile(item: Partial<IProfileItem>): Promise<IProfi
161157
}
162158
case 'local': {
163159
if (!item.file) {
164-
dialog.showErrorBox(
165-
'File is required for local profile',
166-
'File is required for local profile'
167-
)
168160
throw new Error('File is required for local profile')
169161
}
170162
const data = item.file
171-
setProfileStr(id, data)
163+
await setProfileStr(id, data)
172164
break
173165
}
174166
}

src/main/core/mihomoApi.ts

-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import axios, { AxiosInstance } from 'axios'
22
import { getAppConfig, getControledMihomoConfig } from '../config'
33
import WebSocket from 'ws'
44
import { window } from '..'
5-
import { dialog } from 'electron'
65

76
let axiosIns: AxiosInstance = null!
87
let mihomoTrafficWs: WebSocket | null = null
@@ -189,8 +188,6 @@ const mihomoTraffic = (): void => {
189188
if (trafficRetry) {
190189
trafficRetry--
191190
mihomoTraffic()
192-
} else {
193-
dialog.showErrorBox('External controller traffic error', 'Retry limit reached')
194191
}
195192
}
196193

@@ -234,8 +231,6 @@ const mihomoMemory = (): void => {
234231
if (memoryRetry) {
235232
memoryRetry--
236233
mihomoMemory()
237-
} else {
238-
dialog.showErrorBox('External controller memory error', 'Retry limit reached')
239234
}
240235
}
241236

@@ -281,8 +276,6 @@ const mihomoLogs = (): void => {
281276
if (logsRetry) {
282277
logsRetry--
283278
mihomoLogs()
284-
} else {
285-
dialog.showErrorBox('External controller logs error', 'Retry limit reached')
286279
}
287280
}
288281

@@ -328,8 +321,6 @@ const mihomoConnections = (): void => {
328321
if (connectionsRetry) {
329322
connectionsRetry--
330323
mihomoConnections()
331-
} else {
332-
dialog.showErrorBox('External controller connections error', 'Retry limit reached')
333324
}
334325
}
335326

src/main/core/profileUpdater.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
1-
import { addProfileItem, getProfileConfig, getProfileItem } from '../config'
1+
import { addProfileItem, getCurrentProfileItem, getProfileConfig, getProfileItem } from '../config'
22

33
const intervalPool: Record<string, NodeJS.Timeout> = {}
44

5-
export function initProfileUpdater(): void {
6-
const { items } = getProfileConfig()
7-
8-
for (const item of items) {
5+
export async function initProfileUpdater(): Promise<void> {
6+
const { items, current } = getProfileConfig()
7+
const currentItem = getCurrentProfileItem()
8+
for (const item of items.filter((i) => i.id !== current)) {
99
if (item.type === 'remote' && item.interval) {
10-
addProfileItem(getProfileItem(item.id))
10+
await addProfileItem(item)
1111
intervalPool[item.id] = setInterval(
12-
() => {
13-
addProfileItem(getProfileItem(item.id))
12+
async () => {
13+
await addProfileItem(item)
1414
},
1515
item.interval * 60 * 1000
1616
)
1717
}
1818
}
19+
if (currentItem.type === 'remote' && currentItem.interval) {
20+
await addProfileItem(currentItem)
21+
intervalPool[currentItem.id] = setInterval(
22+
async () => {
23+
await addProfileItem(currentItem)
24+
},
25+
currentItem.interval * 60 * 1000 + 10000 // +10s
26+
)
27+
}
1928
}
2029

2130
export function addProfileUpdater(id: string): void {
22-
const { items } = getProfileConfig()
23-
const item = items.find((i) => i.id === id)
31+
const item = getProfileItem(id)
2432

25-
if (item?.type === 'remote' && item.interval) {
33+
if (item.type === 'remote' && item.interval) {
2634
if (intervalPool[id]) {
2735
clearInterval(intervalPool[id])
2836
}
2937
intervalPool[id] = setInterval(
30-
() => {
31-
addProfileItem(getProfileItem(id))
38+
async () => {
39+
await addProfileItem(item)
3240
},
3341
item.interval * 60 * 1000
3442
)

src/main/core/tray.ts

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const buildContextMenu = (): Menu => {
7474
window?.webContents.send('appConfigUpdated')
7575
} catch (e) {
7676
setAppConfig({ sysProxy: { enable: !enable } })
77-
console.error(e)
7877
} finally {
7978
updateTrayMenu()
8079
}

src/main/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ if (!gotTheLock) {
5959
// Set app user model id for windows
6060
electronApp.setAppUserModelId('party.mihomo.app')
6161
startCore().then(() => {
62-
setTimeout(() => {
63-
initProfileUpdater()
64-
}, 30000)
62+
setTimeout(async () => {
63+
await initProfileUpdater()
64+
}, 60000)
6565
})
6666
// Default open or close DevTools by F12 in development
6767
// and ignore CommandOrControl + R in production.

src/main/utils/ipc.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function registerIpcMainHandlers(): void {
116116
function getFilePath(ext: string[]): string[] | undefined {
117117
return dialog.showOpenDialogSync({
118118
title: '选择订阅文件',
119-
filters: [{ name: 'Yaml Files', extensions: ext }],
119+
filters: [{ name: `${ext} file`, extensions: ext }],
120120
properties: ['openFile']
121121
})
122122
}

src/renderer/src/components/sider/sysproxy-switcher.tsx

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@ const SysproxySwitcher: React.FC = () => {
1919
id: 'sysproxy'
2020
})
2121
const onChange = async (enable: boolean): Promise<void> => {
22-
try {
23-
await triggerSysProxy(enable)
24-
await patchAppConfig({ sysProxy: { enable } })
25-
} catch (e) {
26-
console.error(e)
27-
}
22+
await triggerSysProxy(enable)
23+
await patchAppConfig({ sysProxy: { enable } })
2824
}
2925

3026
return (

src/renderer/src/pages/syspeoxy.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ const Sysproxy: React.FC = () => {
9090
try {
9191
await triggerSysProxy(true)
9292
await patchAppConfig({ sysProxy: { enable: true } })
93-
} catch (e) {
93+
} catch {
9494
await patchAppConfig({ sysProxy: { enable: false } })
95-
console.error(e)
9695
}
9796
}
9897

0 commit comments

Comments
 (0)