perf: ♻️ Optimized contextBridge related improvements for stability

This commit is contained in:
viarotel 2024-12-03 09:54:49 +08:00
parent d5cada2dfb
commit 0e098c3739
15 changed files with 129 additions and 198 deletions

View File

@ -116,7 +116,7 @@ const getDeviceIP = async (id) => {
return value
}
catch (error) {
console.warn('adbkit.getDeviceIP.error', error.message)
console.warn('adb.getDeviceIP.error', error.message)
}
}
@ -179,13 +179,6 @@ const display = async (deviceId) => {
return value
}
const clearOverlayDisplayDevices = async (deviceId) => {
return deviceShell(
deviceId,
'settings put global overlay_display_devices none',
)
}
const watch = async (callback) => {
const tracker = await client.trackDevices()
tracker.on('add', async (ret) => {
@ -273,32 +266,32 @@ async function pull(id, filePath, args = {}) {
})
}
export default () => {
const binPath = appStore.get('common.adbPath') || adbPath
function init() {
const bin = appStore.get('common.adbPath') || adbPath
client = Adb.createClient({
bin: binPath,
bin,
})
return {
shell,
spawnShell,
getDevices,
deviceShell,
kill,
connect,
disconnect,
getDeviceIP,
tcpip,
screencap,
install,
isInstalled,
version,
display,
clearOverlayDisplayDevices,
push,
pull,
watch,
readdir,
}
}
export default {
init,
shell,
spawnShell,
getDevices,
deviceShell,
kill,
connect,
disconnect,
getDeviceIP,
tcpip,
screencap,
install,
isInstalled,
version,
display,
push,
pull,
watch,
readdir,
}

View File

@ -6,9 +6,9 @@ import {
} from '$electron/configs/index.js'
import appStore from '$electron/helpers/store.js'
const appDebug = appStore.get('common.debug') || false
import adb from '$electron/exposes/adb/index.js'
let adbkit = null
const appDebug = appStore.get('common.debug') || false
const shell = async (command, { debug = false, stdout, stderr } = {}) => {
const spawnPath = appStore.get('common.gnirehtetPath') || gnirehtetPath
@ -80,7 +80,7 @@ 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')
const res = await adb.isInstalled(deviceId, 'com.genymobile.gnirehtet')
return res
}
@ -122,7 +122,7 @@ const run = async (deviceId) => {
})
const gnirehtetFix = appStore.get('common.gnirehtetFix') || false
const isInstalled = installed(deviceId)
const isInstalled = await installed(deviceId)
if (gnirehtetFix || !isInstalled) {
await install(deviceId).catch((error) => {
@ -141,17 +141,13 @@ window.addEventListener('beforeunload', () => {
stopRelayProcess()
})
export default (options = {}) => {
adbkit = options.adbkit
return {
shell,
relay,
install,
installed,
start,
stop,
tunnel,
run,
}
export default {
shell,
relay,
install,
installed,
start,
stop,
tunnel,
run,
}

View File

@ -3,7 +3,7 @@ import path from 'node:path'
import * as configs from '$electron/configs/index.js'
import appLog from '$electron/helpers/log.js'
import store from '$electron/helpers/store.js'
import adbkit from './adbkit/index.js'
import adb from './adb/index.js'
import electron from './electron/index.js'
import gnirehtet from './gnirehtet/index.js'
import scrcpy from './scrcpy/index.js'
@ -12,6 +12,8 @@ import '$electron/helpers/console.js'
export default {
init(expose) {
adb.init()
expose('nodePath', path)
expose('appLog', appLog)
@ -23,14 +25,12 @@ export default {
configs,
})
const adbkitExecute = adbkit()
expose('adb', adb)
expose('adbkit', adbkitExecute)
expose('scrcpy', scrcpy)
expose('scrcpy', scrcpy({ adbkit: adbkitExecute }))
expose('gnirehtet', gnirehtet)
expose('gnirehtet', gnirehtet({ adbkit: adbkitExecute }))
expose('findInPageModal', search())
expose('findInPageModal', search)
},
}

View File

@ -2,13 +2,10 @@ import { exec as _exec, spawn } from 'node:child_process'
import util from 'node:util'
import { adbPath, scrcpyPath } from '$electron/configs/index.js'
import appStore from '$electron/helpers/store.js'
import { sleep } from '$renderer/utils/index.js'
import commandHelper from '$renderer/utils/command/index.js'
import { getDisplayOverlay, parseScrcpyAppList, parseScrcpyCodecList } from './helper.js'
let adbkit
const exec = util.promisify(_exec)
async function shell(command, { stdout, stderr, signal, ...options } = {}) {
@ -111,54 +108,6 @@ async function record(serial, { title, args = '', savePath, ...options } = {}) {
)
}
async function mirrorGroup(serial, { openNum = 1, ...options } = {}) {
const displayOverlay = getDisplayOverlay(serial)
const command = `settings put global overlay_display_devices "${[
...Array.from({ length: openNum }).keys(),
]
.map(() => displayOverlay)
.join(';')}"`
await adbkit.deviceShell(serial, command)
await sleep()
const displayList = await adbkit.display(serial)
const filterList = displayList.filter(item => item !== '0')
const results = []
for (let index = 0; index < filterList.length; index++) {
const displayId = filterList[index]
let args = options.args || ''
if (args.includes('--display-id')) {
args = args.replace(/(--display-id=)"[^"]*"/, `$1"${displayId}"`)
}
else {
args += ` --display-id="${displayId}"`
}
const title = options?.title?.({ displayId, index }) || options?.title
const promise = mirror(serial, {
...options,
title,
args,
exec: true,
})
results.push(promise)
await sleep(1500)
}
return Promise.allSettled(results)
}
async function helper(
serial,
command = '',
@ -209,18 +158,13 @@ async function startApp(serial, args = {}) {
return displayId
}
export default (options = {}) => {
adbkit = options.adbkit
return {
shell,
execShell,
getEncoders,
mirror,
record,
mirrorGroup,
helper,
getAppList,
startApp,
}
export default {
shell,
execShell,
getEncoders,
mirror,
record,
helper,
getAppList,
startApp,
}

View File

@ -2,74 +2,72 @@ import { primaryColor } from '$renderer/configs/index.js'
import remote from '@electron/remote'
import { FindInPage } from 'electron-find-in-page'
export default () => {
const theme = {
isDark: false,
const theme = {
isDark: false,
}
let findInPage = null
async function open({ ...args } = {}) {
await create(args)
return findInPage.openFindWindow()
}
function close() {
remote.getCurrentWebContents().stopFindInPage('clearSelection')
if (!findInPage) {
return false
}
let findInPage = null
async function open({ ...args } = {}) {
await create(args)
return findInPage.openFindWindow()
}
function close() {
remote.getCurrentWebContents().stopFindInPage('clearSelection')
if (!findInPage) {
return false
}
return findInPage.closeFindWindow()
}
async function update({ isDark = false, ...args } = {}) {
if (isDark === theme.isDark) {
return findInPage
}
try {
await findInPage.destroy()
}
catch (error) {
console.warn('error', error.message)
}
findInPage = null
return create({ ...args, isDark })
}
async function create({ ...args } = {}) {
if (findInPage) {
return update(args)
}
theme.isDark = args.isDark
findInPage = new FindInPage(remote.getCurrentWebContents(), {
...args,
preload: true,
inputFocusColor: primaryColor,
...(theme.isDark
? {
boxShadowColor: '#4C4D4F',
boxBgColor: '#262626',
inputColor: '#CFD3DC',
inputBgColor: '#141414',
textColor: '#CFD3DC',
textHoverBgColor: '#4C4D4F',
}
: {}),
})
return findInPage.closeFindWindow()
}
async function update({ isDark = false, ...args } = {}) {
if (isDark === theme.isDark) {
return findInPage
}
return {
open,
close,
try {
await findInPage.destroy()
}
catch (error) {
console.warn('error', error.message)
}
findInPage = null
return create({ ...args, isDark })
}
async function create({ ...args } = {}) {
if (findInPage) {
return update(args)
}
theme.isDark = args.isDark
findInPage = new FindInPage(remote.getCurrentWebContents(), {
...args,
preload: true,
inputFocusColor: primaryColor,
...(theme.isDark
? {
boxShadowColor: '#4C4D4F',
boxBgColor: '#262626',
inputColor: '#CFD3DC',
inputBgColor: '#141414',
textColor: '#CFD3DC',
textHoverBgColor: '#4C4D4F',
}
: {}),
})
return findInPage
}
export default {
open,
close,
}

View File

@ -32,7 +32,7 @@ export default (App) => {
app.config.globalProperties.$appLog = window.appLog
app.config.globalProperties.$electron = window.electron
app.config.globalProperties.$adb = window.adbkit
app.config.globalProperties.$adb = window.adb
app.config.globalProperties.$scrcpy = window.scrcpy
app.config.globalProperties.$gnirehtet = window.gnirehtet

View File

@ -217,7 +217,7 @@ function onClosed() {
async function getTableData() {
loading.value = true
const data = await window.adbkit.readdir(device.value.id, currentPath.value)
const data = await window.adb.readdir(device.value.id, currentPath.value)
loading.value = false
@ -264,7 +264,7 @@ function handlePrev() {
}
async function handleAdd(dirname) {
await window.adbkit.deviceShell(
await window.adb.deviceShell(
device.value.id,
`mkdir ${currentPath.value}/${dirname}`,
)
@ -286,7 +286,7 @@ async function handleRemove(row) {
return error.message
}
await window.adbkit.deviceShell(
await window.adb.deviceShell(
device.value.id,
`rm -r ${currentPath.value}/${row.name}`,
)
@ -329,7 +329,7 @@ async function handleDownload(row) {
for (let index = 0; index < pathList.length; index++) {
const item = pathList[index]
await window.adbkit
await window.adb
.pull(device.value.id, item, { savePath })
.catch(e => console.warn(e?.message))
}

View File

@ -2,7 +2,7 @@ import { debounce } from 'lodash-es'
import { createStderr, createStdout, textFormatter } from 'vue-command'
import { useFixCursor } from './helper.js'
const $adb = window.adbkit
const $adb = window.adb
export function useAdb({ vShell, history, loading } = {}) {
const adb = async (args) => {

View File

@ -1,6 +1,6 @@
import { createStderr, createStdout } from 'vue-command'
const $adb = window.adbkit
const $adb = window.adb
export function useAdb({ loading }) {
const adb = async (args) => {

View File

@ -68,7 +68,7 @@ export function useFileActions() {
let failCount = 0
await allSettledWrapper(files, (item) => {
return window.adbkit.push(device.id, item, { savePath }).catch(() => {
return window.adb.push(device.id, item, { savePath }).catch(() => {
++failCount
})
})

View File

@ -65,7 +65,7 @@ export function useInstallAction() {
let failCount = 0
await allSettledWrapper(files, (item) => {
return window.adbkit.install(device.id, item).catch((e) => {
return window.adb.install(device.id, item).catch((e) => {
console.warn(e)
++failCount
})

View File

@ -41,7 +41,7 @@ export function useScreenshotAction({ floating } = {}) {
const savePath = window.nodePath.resolve(deviceConfig.savePath, fileName)
try {
await window.adbkit.screencap(device.id, { savePath })
await window.adb.screencap(device.id, { savePath })
}
catch (error) {
if (error.message) {

View File

@ -91,7 +91,7 @@ export function useShellAction() {
const filePath = successFiles?.[0]
if (filePath) {
window.adbkit.deviceShell(device.id, `sh ${filePath}`)
window.adb.deviceShell(device.id, `sh ${filePath}`)
}
})

View File

@ -76,7 +76,7 @@ export const useDeviceStore = defineStore({
this.list = data
},
async getList() {
const res = await window.adbkit.getDevices()
const res = await window.adb.getDevices()
const data
= res?.map(item => ({

View File

@ -47,7 +47,7 @@ export async function selectAndSendFileToDevice(
const failFiles = []
await allSettledWrapper(files, async (item) => {
const ret = await window.adbkit.push(deviceId, item).catch((e) => {
const ret = await window.adb.push(deviceId, item).catch((e) => {
console.warn(e?.message)
failFiles.push(`${deviceId}-${item}`)
})