Skip to content

Commit

Permalink
perf: ♻️ Optimized contextBridge related improvements for stability
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Dec 3, 2024
1 parent d5cada2 commit 0e098c3
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 190 deletions.
57 changes: 25 additions & 32 deletions electron/exposes/adbkit/index.js → electron/exposes/adb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
}
30 changes: 13 additions & 17 deletions electron/exposes/gnirehtet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
}
14 changes: 7 additions & 7 deletions electron/exposes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -12,6 +12,8 @@ import '$electron/helpers/console.js'

export default {
init(expose) {
adb.init()

expose('nodePath', path)

expose('appLog', appLog)
Expand All @@ -23,14 +25,12 @@ export default {
configs,
})

const adbkitExecute = adbkit()

expose('adbkit', adbkitExecute)
expose('adb', adb)

expose('scrcpy', scrcpy({ adbkit: adbkitExecute }))
expose('scrcpy', scrcpy)

expose('gnirehtet', gnirehtet({ adbkit: adbkitExecute }))
expose('gnirehtet', gnirehtet)

expose('findInPageModal', search())
expose('findInPageModal', search)
},
}
74 changes: 9 additions & 65 deletions electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {}) {
Expand Down Expand Up @@ -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 = '',
Expand Down Expand Up @@ -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,
}
110 changes: 54 additions & 56 deletions electron/exposes/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

let findInPage = null
const theme = {
isDark: false,
}

async function open({ ...args } = {}) {
await create(args)
let findInPage = null

return findInPage.openFindWindow()
}
async function open({ ...args } = {}) {
await create(args)

function close() {
remote.getCurrentWebContents().stopFindInPage('clearSelection')
return findInPage.openFindWindow()
}

if (!findInPage) {
return false
}
function close() {
remote.getCurrentWebContents().stopFindInPage('clearSelection')

return findInPage.closeFindWindow()
if (!findInPage) {
return false
}

async function update({ isDark = false, ...args } = {}) {
if (isDark === theme.isDark) {
return findInPage
}

try {
await findInPage.destroy()
}
catch (error) {
console.warn('error', error.message)
}
return findInPage.closeFindWindow()
}

findInPage = null
async function update({ isDark = false, ...args } = {}) {
if (isDark === theme.isDark) {
return findInPage
}

return create({ ...args, isDark })
try {
await findInPage.destroy()
}
catch (error) {
console.warn('error', error.message)
}

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',
}
: {}),
})
findInPage = null

return findInPage
}
return create({ ...args, isDark })
}

return {
open,
close,
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,
}
Loading

0 comments on commit 0e098c3

Please sign in to comment.