Skip to content

Commit

Permalink
perf: ♻️ Optimize Gnirehtet interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 31, 2023
1 parent f907106 commit a90e570
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
53 changes: 32 additions & 21 deletions electron/exposes/gnirehtet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
const stopRelayProcess = () => {
if (!relayProcess) {
return
}

relayProcess?.kill()
relayProcess = null
}
const relay = async (args) => {
if (relayProcess) {
return relayProcess
Expand All @@ -89,43 +108,35 @@ const relay = async (args) => {
}
resolve(process)
},
}).catch((error) => {
stderr: (error) => {
stopRelayProcess()
reject(error)
},
})?.catch((error) => {
stopRelayProcess()
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) => {
await relay().catch((e) => {
throw new Error(e?.message || 'Gnirehtet Relay fail')
await relay().catch((error) => {
throw new Error(error?.message || 'Gnirehtet Relay fail')
})

await install(deviceId).catch((e) => {
throw new Error(e?.message || 'Gnirehtet Install Client fail')
await install(deviceId).catch((error) => {
throw new Error(error?.message || 'Gnirehtet Install Client fail')
})

await start(deviceId).catch((e) => {
throw new Error(e?.message || 'Gnirehtet Start fail')
await start(deviceId).catch((error) => {
throw new Error(error?.message || 'Gnirehtet Start fail')
})
}

window.addEventListener('beforeunload', () => {
stop()

if (relayProcess) {
relayProcess.kill()
}
stopRelayProcess()
})

export default (options = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion electron/loading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function useLoading() {
divEl.className = `${className}-wrap`
divEl.innerHTML = `
<div class="${className}-core"></div>
<div class="${className}-text"> 初始化服务中...</div>
<div class="${className}-text"> Initializing service...</div>
`

return {
Expand Down

0 comments on commit a90e570

Please sign in to comment.