Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
remove deepCopy(), replaced with the spread operator
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Aug 18, 2023
1 parent bd28dcf commit 6493f25
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/stores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export interface WebConfig {
productDeletionDialog: boolean;
}

function deepCopy<T>(data: T): T {
return JSON.parse(JSON.stringify(data));
}

function _readFromLocalStorage() {
const stringified = localStorage.getItem(localStorageKey);
if (stringified === null) return null;
Expand Down Expand Up @@ -84,12 +80,12 @@ export const useConfigStore = defineStore("config", () => {
});

const configLocalStorage = ref({
...deepCopy(defaultConfig.value),
...defaultConfig.value,
...readFromLocalStorage(),
});
writeToLocalStorage(configLocalStorage.value); // in case defaultConfig has added new properties

const config = ref(deepCopy(configLocalStorage.value));
const config = ref({ ...configLocalStorage.value });
const configJson = computed(() => JSON.stringify(config.value));

let configServerJson = ref<string | null | undefined>();
Expand Down Expand Up @@ -146,15 +142,15 @@ export const useConfigStore = defineStore("config", () => {
);
return;
}
configServer.value = { ...deepCopy(defaultConfig.value), ...parsed };
configServer.value = { ...defaultConfig.value, ...parsed };
});

watch(
configServer,
(val) => {
if (val === null) return;
config.value = deepCopy(val);
configLocalStorage.value = deepCopy(config.value);
config.value = { ...val };
configLocalStorage.value = { ...config.value };
writeToLocalStorage(configLocalStorage.value);
},
{ deep: true, immediate: true }
Expand All @@ -164,9 +160,9 @@ export const useConfigStore = defineStore("config", () => {

function reset() {
if (configServer.value) {
config.value = deepCopy(configServer.value);
config.value = { ...configServer.value };
} else {
config.value = deepCopy(configLocalStorage.value);
config.value = { ...configLocalStorage.value };
}
}

Expand Down

0 comments on commit 6493f25

Please sign in to comment.