Skip to content

Commit

Permalink
perf: ⚡️ Optimize the method of obtaining the available displays of t…
Browse files Browse the repository at this point in the history
…he device
  • Loading branch information
viarotel committed Jan 13, 2025
1 parent d25714a commit 9e31c84
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
22 changes: 0 additions & 22 deletions electron/exposes/adb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import appStore from '$electron/helpers/store.js'
import { formatFileSize } from '$renderer/utils/index'
import { Adb } from '@devicefarmer/adbkit'
import dayjs from 'dayjs'
import { uniq } from 'lodash-es'
import adbConnectionMonitor from './helpers/adbConnectionMonitor/index.js'
import { streamToBase64 } from '$electron/helpers/index.js'
import { parseBatteryDump } from './helpers/battery/index.js'
Expand Down Expand Up @@ -160,26 +159,6 @@ const isInstalled = async (id, pkg) => client.getDevice(id).isInstalled(pkg)

const version = async () => client.version()

const display = async (deviceId) => {
let value = []
try {
const res = await deviceShell(deviceId, 'dumpsys display')

const regex = /Display Id=(\d+)/g

const match = res.match(regex) || []

const mapValue = match.map(item => item.split('=')[1])

value = uniq(mapValue)
}
catch (error) {
console.warn(error?.message || error)
}

return value
}

const watch = async (callback) => {
const tracker = await client.trackDevices()
tracker.on('add', async (ret) => {
Expand Down Expand Up @@ -354,7 +333,6 @@ export default {
install,
isInstalled,
version,
display,
push,
pull,
watch,
Expand Down
27 changes: 27 additions & 0 deletions electron/exposes/scrcpy/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,30 @@ export function parseScrcpyCodecList(rawText) {
}
}
}

/**
* Gets a list of available display ids
* @param {*} text
* @returns
*/
export function parseDisplayIds(text) {
if (!text || typeof text !== 'string') {
return []
}

try {
const displayIdPattern = /--display-id=(\d+)/g

const matches = Array.from(text.matchAll(displayIdPattern))

const displayIds = matches.map(match => Number.parseInt(match[1], 10))

const uniqueDisplayIds = [...new Set(displayIds)].filter(id => !Number.isNaN(id))

return uniqueDisplayIds.sort((a, b) => a - b)
}
catch (error) {
console.error('Error parsing display IDs:', error)
return []
}
}
12 changes: 11 additions & 1 deletion electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { adbPath, scrcpyPath } from '$electron/configs/index.js'
import appStore from '$electron/helpers/store.js'
import commandHelper from '$renderer/utils/command/index.js'

import { getDisplayOverlay, parseScrcpyAppList, parseScrcpyCodecList } from './helper.js'
import { getDisplayOverlay, parseDisplayIds, parseScrcpyAppList, parseScrcpyCodecList } from './helper.js'

const exec = util.promisify(_exec)

Expand Down Expand Up @@ -132,6 +132,15 @@ async function getAppList(serial) {
return value
}

async function getDisplayIds(serial) {
const res = await execShell(`--serial="${serial}" --list-displays`)

const stdout = res.stdout
const value = parseDisplayIds(stdout)

return value
}

async function startApp(serial, args = {}) {
let { commands, packageName, ...options } = args

Expand Down Expand Up @@ -167,4 +176,5 @@ export default {
helper,
getAppList,
startApp,
getDisplayIds,
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default {
return false
}
const res = await this.$adb.display(deviceId)
const res = await this.$scrcpy.getDisplayIds(deviceId)
this.deviceOptions
= res?.map((item) => {
Expand Down

0 comments on commit 9e31c84

Please sign in to comment.