Skip to content

Commit

Permalink
perf: ♻️ Optimize configuration file logic
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Nov 8, 2023
1 parent 0605a81 commit 5f8cc5a
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 73 deletions.
13 changes: 5 additions & 8 deletions src/store/preference/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,18 @@ export function setStoreData(data, scope) {
})
}

export function mergeConfig(object, sources, { debug = false } = {}) {
export function mergeConfig(object, sources) {
const cloneObject = cloneDeep(object)
const cloneSources = cloneDeep(sources)

const customizer = (objValue, srcValue, key) => {
let value = srcValue || objValue
let value

if (typeof srcValue === 'boolean') {
if (srcValue) {
value = srcValue
}

if (debug) {
console.log(`srcValue.${key}`, srcValue)
console.log(`objValue.${key}`, objValue)
console.log(key, value)
else if (objValue) {
value = objValue
}

return value
Expand Down
30 changes: 13 additions & 17 deletions src/store/preference/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia'
import { cloneDeep, get, set } from 'lodash-es'
import { cloneDeep, get, pickBy, set } from 'lodash-es'
import model from './model/index.js'

import {
Expand All @@ -13,7 +13,7 @@ import {

import { replaceIP, restoreIP } from '@/utils/index.js'

const { adbPath, scrcpyPath } = window.electron?.configs || {}
const { adbPath, scrcpyPath, gnirehtetPath } = window.electron?.configs || {}

export const usePreferenceStore = defineStore({
id: 'app-preference',
Expand Down Expand Up @@ -54,15 +54,10 @@ export const usePreferenceStore = defineStore({
actions: {
getDefaultData,
init(scope = this.deviceScope) {
const globalData = mergeConfig(getDefaultData(), getStoreData())
let data = mergeConfig(getDefaultData(), getStoreData())

let data = {}

if (scope === 'global') {
data = globalData
}
else {
data = mergeConfig(globalData, getStoreData(replaceIP(scope)))
if (scope !== 'global') {
data = mergeConfig(data, getStoreData(replaceIP(scope)))
}

this.data = data
Expand All @@ -75,20 +70,21 @@ export const usePreferenceStore = defineStore({
this.init()
},
setData(data, scope = this.deviceScope) {
const cloneData = cloneDeep(data)

// console.log('adbPath', adbPath)
// console.log('scrcpyPath', scrcpyPath)
const pickData = pickBy(data, value => !!value)

if (data.adbPath === adbPath) {
delete cloneData.adbPath
delete pickData.adbPath
}

if (data.scrcpyPath === scrcpyPath) {
delete cloneData.scrcpyPath
delete pickData.scrcpyPath
}

if (data.gnirehtetPath === gnirehtetPath) {
delete pickData.gnirehtetPath
}

setStoreData(cloneData, replaceIP(scope))
setStoreData(pickData, replaceIP(scope))

this.init(scope)
},
Expand Down
14 changes: 7 additions & 7 deletions src/store/preference/model/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
label: 'preferences.audio.audio-source.name',
field: '--audio-source',
type: 'Select',
value: '',
value: undefined,
placeholder: 'preferences.audio.audio-source.placeholder',
tips: 'preferences.audio.audio-source.tips',
options: [
Expand All @@ -19,7 +19,7 @@ export default {
label: 'preferences.audio.audio-code.name',
field: '--audio-code',
type: 'AudioCodecSelect',
value: '',
value: undefined,
placeholder: 'preferences.audio.audio-code.placeholder',
options: [
{
Expand All @@ -40,34 +40,34 @@ export default {
audioCodec: {
hidden: true,
field: '--audio-codec',
value: '',
value: undefined,
},
audioEncoder: {
hidden: true,
field: '--audio-encoder',
value: '',
value: undefined,
},
audioBitRate: {
label: 'preferences.audio.audio-bit-rate.name',
field: '--audio-bit-rate',
type: 'Input.number',
value: '',
value: undefined,
placeholder: 'preferences.audio.audio-bit-rate.placeholder',
append: 'bps',
},
audioBuffer: {
label: 'preferences.audio.audio-buffer.name',
field: '--audio-buffer',
type: 'Input.number',
value: '',
value: undefined,
placeholder: 'preferences.audio.audio-buffer.placeholder',
append: 'ms',
},
audioOutputBuffer: {
label: 'preferences.audio.audio-output-buffer.name',
field: '--audio-output-buffer',
type: 'Input.number',
value: '',
value: undefined,
placeholder: 'preferences.audio.audio-output-buffer.placeholder',
append: 'ms',
},
Expand Down
10 changes: 5 additions & 5 deletions src/store/preference/model/camera/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export default {
label: 'preferences.camera.enable.name',
field: '--camera',
type: 'CameraSwitch',
value: null,
value: undefined,
placeholder: 'preferences.camera.enable.placeholder',
tips: 'preferences.camera.enable.tips',
},
cameraFacing: {
label: 'preferences.camera.camera-facing.name',
field: '--camera-facing',
type: 'Select',
value: '',
value: undefined,
placeholder: 'preferences.camera.camera-facing.placeholder',
options: [
{ label: 'preferences.camera.camera-facing.front', value: 'front' },
Expand All @@ -29,21 +29,21 @@ export default {
label: 'preferences.camera.camera-size.name',
field: '--camera-size',
type: 'Input',
value: '',
value: undefined,
placeholder: 'preferences.camera.camera-size.placeholder',
},
cameraAr: {
label: 'preferences.camera.camera-ar.name',
field: '--camera-ar',
type: 'Input',
value: '',
value: undefined,
placeholder: 'preferences.camera.camera-ar.placeholder',
},
cameraFps: {
label: 'preferences.camera.camera-fps.name',
field: '--camera-fps',
type: 'Input.number',
value: '',
value: undefined,
placeholder: 'preferences.camera.camera-fps.placeholder',
append: 'fps',
},
Expand Down
8 changes: 3 additions & 5 deletions src/store/preference/model/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default {
type: 'Select',
value: 'system',
placeholder: 'preferences.common.theme.placeholder',
tips: '',
options: [
{
label: 'preferences.common.theme.options[0]',
Expand All @@ -36,7 +35,6 @@ export default {
type: 'LanguageSelect',
value: defaultLanguage,
placeholder: 'preferences.common.language.placeholder',
tips: '',
options: [
{
label: 'preferences.common.language.chinese',
Expand Down Expand Up @@ -92,7 +90,7 @@ export default {
scrcpyAppend: {
label: 'preferences.common.scrcpy.append.name',
field: 'scrcpyAppend',
value: '',
value: undefined,
type: 'Input',
placeholder: 'preferences.common.scrcpy.append.placeholder',
tips: 'preferences.common.scrcpy.append.tips',
Expand All @@ -106,15 +104,15 @@ export default {
label: 'preferences.common.gnirehtet.fix.name',
field: 'gnirehtetFix',
type: 'Switch',
value: false,
value: undefined,
placeholder: 'preferences.common.gnirehtet.fix.placeholder',
tips: 'preferences.common.gnirehtet.fix.tips',
},
debug: {
label: 'preferences.common.debug.name',
field: 'debug',
type: 'Switch',
value: false,
value: undefined,
placeholder: 'preferences.common.debug.placeholder',
tips: 'preferences.common.debug.tips',
},
Expand Down
12 changes: 6 additions & 6 deletions src/store/preference/model/device/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,45 @@ export default {
label: 'preferences.device.show-touch.name',
field: '--show-touches',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.device.show-touch.placeholder',
tips: 'preferences.device.show-touch.tips',
},
stayAwake: {
label: 'preferences.device.stay-awake.name',
field: '--stay-awake',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.device.stay-awake.placeholder',
tips: 'preferences.device.stay-awake.tips',
},
turnScreenOff: {
label: 'preferences.device.control-in-close-screen.name',
field: '--turn-screen-off',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.device.control-in-close-screen.placeholder',
},
powerOffOnClose: {
label: 'preferences.device.control-end-video.name',
field: '--power-off-on-close',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.device.control-end-video.placeholder',
},
noPowerOn: {
label: 'preferences.device.control-in-stop-charging.name',
field: '--no-power-on',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.device.control-in-stop-charging.placeholder',
tips: 'preferences.device.control-in-stop-charging.tips',
},
overlayDisplay: {
label: 'preferences.device.display-overlay.name',
field: '--display-overlay',
type: 'Input',
value: '',
value: undefined,
placeholder: 'preferences.device.display-overlay.placeholder',
tips: 'preferences.device.display-overlay.tips',
},
Expand Down
6 changes: 3 additions & 3 deletions src/store/preference/model/otg/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ export default {
label: 'preferences.otg.enable.name',
field: '--otg',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.otg.enable.placeholder',
tips: 'preferences.otg.enable.tips',
},
hidKeyboard: {
label: 'preferences.otg.only-keyboard.name',
field: '--hid-keyboard',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.otg.only-keyboard.placeholder',
tips: 'preferences.otg.only-keyboard.tips',
},
hidMouse: {
label: 'preferences.otg.only-mouse.name',
field: '--hid-mouse',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.otg.only-mouse.placeholder',
tips: 'preferences.otg.only-mouse.tips',
},
Expand Down
12 changes: 6 additions & 6 deletions src/store/preference/model/record/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
label: 'preferences.record.lock-video-orientation.name',
field: '--lock-video-orientation',
type: 'Select',
value: '',
value: undefined,
placeholder: 'preferences.record.lock-video-orientation.placeholder',
options: [
{ label: '0°', value: '0' },
Expand All @@ -36,7 +36,7 @@ export default {
label: 'preferences.record.time-limit.name',
field: '--time-limit',
type: 'Input.number',
value: '',
value: undefined,
placeholder: 'preferences.record.time-limit.placeholder',
append: 's',
span: 24,
Expand All @@ -45,29 +45,29 @@ export default {
label: 'preferences.record.disable-video.name',
field: '--no-video',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.record.disable-video.placeholder',
},
noAudio: {
label: 'preferences.record.disable-audio.name',
field: '--no-audio',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.record.disable-audio.placeholder',
},
noVideoPlayback: {
label: 'preferences.record.no-video-playback.name',
field: '--no-video-playback',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.record.no-video-playback.placeholder',
tips: 'preferences.record.no-video-playback.tips',
},
noAudioPlayback: {
label: 'preferences.record.no-audio-playback.name',
field: '--no-audio-playback',
type: 'Switch',
value: null,
value: undefined,
placeholder: 'preferences.record.no-audio-playback.placeholder',
tips: 'preferences.record.no-audio-playback.tips',
},
Expand Down
Loading

0 comments on commit 5f8cc5a

Please sign in to comment.