Skip to content

Commit

Permalink
fix(devtools): preserve store reactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 26, 2023
1 parent 8dc5622 commit 709ed3b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/pinia/src/devtools/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ export async function actionGlobalCopyState(pinia: Pinia) {
export async function actionGlobalPasteState(pinia: Pinia) {
if (checkClipboardAccess()) return
try {
Object.assign(
pinia.state.value,
JSON.parse(await navigator.clipboard.readText())
)
loadStoresState(pinia, JSON.parse(await navigator.clipboard.readText()))
toastMessage('Global state pasted from clipboard.')
} catch (error) {
if (checkNotFocusedError(error)) return
Expand Down Expand Up @@ -111,7 +108,7 @@ export async function actionGlobalOpenStateFile(pinia: Pinia) {
const result = await open()
if (!result) return
const { text, file } = result
Object.assign(pinia.state.value, JSON.parse(text))
loadStoresState(pinia, JSON.parse(text))
toastMessage(`Global state imported from "${file.name}".`)
} catch (error) {
toastMessage(
Expand All @@ -121,3 +118,12 @@ export async function actionGlobalOpenStateFile(pinia: Pinia) {
console.error(error)
}
}

function loadStoresState(pinia: Pinia, state: Record<string, unknown>) {
for (const key in state) {
const storeState = pinia.state.value[key]
if (storeState) {
Object.assign(storeState, state[key])
}
}
}

0 comments on commit 709ed3b

Please sign in to comment.