perf: ♻️ Optimize Gnirehtet interaction

This commit is contained in:
viarotel 2023-10-31 23:19:34 +08:00
parent f9071062ba
commit a90e5705b2
2 changed files with 33 additions and 22 deletions

View File

@ -73,7 +73,26 @@ const shell = async (command, { debug = false, stdout, stderr } = {}) => {
}) })
} }
const install = deviceId => shell(`install ${deviceId}`)
const start = deviceId => shell(`start ${deviceId}`)
const stop = deviceId => shell(`stop ${deviceId}`)
const tunnel = deviceId => shell(`tunnel ${deviceId}`)
const installed = async (deviceId) => {
const res = await adbkit.isInstalled(deviceId, 'com.genymobile.gnirehtet')
console.log('gnirehtet.apk.installed', res)
return res
}
let relayProcess = null let relayProcess = null
const stopRelayProcess = () => {
if (!relayProcess) {
return
}
relayProcess?.kill()
relayProcess = null
}
const relay = async (args) => { const relay = async (args) => {
if (relayProcess) { if (relayProcess) {
return relayProcess return relayProcess
@ -89,43 +108,35 @@ const relay = async (args) => {
} }
resolve(process) resolve(process)
}, },
}).catch((error) => { stderr: (error) => {
stopRelayProcess()
reject(error)
},
})?.catch((error) => {
stopRelayProcess()
reject(error) reject(error)
}) })
}) })
} }
const install = deviceId => shell(`install ${deviceId}`)
const start = deviceId => shell(`start ${deviceId}`)
const stop = deviceId => shell(`stop ${deviceId}`)
const tunnel = deviceId => shell(`tunnel ${deviceId}`)
const installed = async (deviceId) => {
const res = await adbkit.isInstalled(deviceId, 'com.genymobile.gnirehtet')
console.log('gnirehtet.apk.installed', res)
return res
}
const run = async (deviceId) => { const run = async (deviceId) => {
await relay().catch((e) => { await relay().catch((error) => {
throw new Error(e?.message || 'Gnirehtet Relay fail') throw new Error(error?.message || 'Gnirehtet Relay fail')
}) })
await install(deviceId).catch((e) => { await install(deviceId).catch((error) => {
throw new Error(e?.message || 'Gnirehtet Install Client fail') throw new Error(error?.message || 'Gnirehtet Install Client fail')
}) })
await start(deviceId).catch((e) => { await start(deviceId).catch((error) => {
throw new Error(e?.message || 'Gnirehtet Start fail') throw new Error(error?.message || 'Gnirehtet Start fail')
}) })
} }
window.addEventListener('beforeunload', () => { window.addEventListener('beforeunload', () => {
stop() stop()
if (relayProcess) { stopRelayProcess()
relayProcess.kill()
}
}) })
export default (options = {}) => { export default (options = {}) => {

View File

@ -148,7 +148,7 @@ function useLoading() {
divEl.className = `${className}-wrap` divEl.className = `${className}-wrap`
divEl.innerHTML = ` divEl.innerHTML = `
<div class="${className}-core"></div> <div class="${className}-core"></div>
<div class="${className}-text"> 初始化服务中...</div> <div class="${className}-text"> Initializing service...</div>
` `
return { return {