mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 21:20:29 +08:00
use worker
This commit is contained in:
parent
a9139006ee
commit
112f456de0
@ -7,12 +7,13 @@ import { tray } from '../resolve/tray'
|
||||
import { calcTraffic } from '../utils/calc'
|
||||
import { getRuntimeConfig } from './factory'
|
||||
import { nativeImage } from 'electron'
|
||||
import svg2img from 'svg2img'
|
||||
import parseSvg from '../utils/parseSvg'
|
||||
|
||||
const icon = nativeImage.createFromPath(templateIcon)
|
||||
icon.setTemplateImage(true)
|
||||
const base64 = icon.toPNG().toString('base64')
|
||||
let hasShowTraffic = false
|
||||
let drawing = false
|
||||
let axiosIns: AxiosInstance = null!
|
||||
let mihomoTrafficWs: WebSocket | null = null
|
||||
let trafficRetry = 10
|
||||
@ -187,11 +188,13 @@ const mihomoTraffic = async (): Promise<void> => {
|
||||
|
||||
mihomoTrafficWs = new WebSocket(`ws://${server}/traffic?token=${encodeURIComponent(secret)}`)
|
||||
|
||||
mihomoTrafficWs.onmessage = (e): void => {
|
||||
mihomoTrafficWs.onmessage = async (e): Promise<void> => {
|
||||
const data = e.data as string
|
||||
const json = JSON.parse(data) as IMihomoTrafficInfo
|
||||
if (process.platform === 'darwin') {
|
||||
if (showTraffic) {
|
||||
if (drawing) return
|
||||
drawing = true
|
||||
const svgContent = `
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 156 36">
|
||||
<image height='36' width='36' href='data:image/png;base64,${base64}'/>
|
||||
@ -200,12 +203,11 @@ const mihomoTraffic = async (): Promise<void> => {
|
||||
<text x='156' y='15' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.up)}/s</text>
|
||||
<text x='156' y='34' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.down)}/s</text>
|
||||
</svg>`
|
||||
svg2img(svgContent, {}, (error, buffer) => {
|
||||
if (error) return
|
||||
const buffer = await parseSvg(svgContent)
|
||||
const image = nativeImage.createFromBuffer(buffer).resize({ height: 16 })
|
||||
image.setTemplateImage(true)
|
||||
tray?.setImage(image)
|
||||
})
|
||||
drawing = false
|
||||
hasShowTraffic = true
|
||||
} else {
|
||||
if (hasShowTraffic) {
|
||||
|
||||
28
src/main/utils/parseSvg.ts
Normal file
28
src/main/utils/parseSvg.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Worker } from 'worker_threads'
|
||||
|
||||
export default function parseSvg(svgStr: string): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = new Worker(workerString, {
|
||||
eval: true,
|
||||
workerData: svgStr
|
||||
})
|
||||
worker.on('message', resolve)
|
||||
worker.on('error', reject)
|
||||
worker.on('exit', (code) => {
|
||||
if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const workerString = `
|
||||
const { parentPort, workerData } = require('worker_threads')
|
||||
const svg2img = require('svg2img')
|
||||
|
||||
const svgStr = workerData
|
||||
svg2img(svgStr, (err, buffer) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
parentPort?.postMessage(buffer)
|
||||
})
|
||||
`
|
||||
Loading…
x
Reference in New Issue
Block a user