Fix object types not being shared across tabs#55
Conversation
|
貌似项目没维护了 |
Co-authored-by: wobsoriano <13049130+wobsoriano@users.noreply.github.com> Agent-Logs-Url: https://github.com/wobsoriano/pinia-shared-state/sessions/101ffac4-35cc-4a2a-8149-ef8c9bb7fb56
…t types Co-authored-by: wobsoriano <13049130+wobsoriano@users.noreply.github.com> Agent-Logs-Url: https://github.com/wobsoriano/pinia-shared-state/sessions/101ffac4-35cc-4a2a-8149-ef8c9bb7fb56
wobsoriano
left a comment
There was a problem hiding this comment.
@copilot use pnpm instead of npm
Co-authored-by: wobsoriano <13049130+wobsoriano@users.noreply.github.com> Agent-Logs-Url: https://github.com/wobsoriano/pinia-shared-state/sessions/e85ff73b-eb5f-413d-a465-d0d27a1597c7
|
@copilot add a changeset |
Co-authored-by: wobsoriano <13049130+wobsoriano@users.noreply.github.com> Agent-Logs-Url: https://github.com/wobsoriano/pinia-shared-state/sessions/385d369c-d7c6-4c9b-9c64-41a9c50b8bb9
|
@copilot can we add unit tests for this library? Use vitest |
Co-authored-by: wobsoriano <13049130+wobsoriano@users.noreply.github.com> Agent-Logs-Url: https://github.com/wobsoriano/pinia-shared-state/sessions/79497100-2c52-409c-98f8-9f6981b6b990
Co-authored-by: wobsoriano <13049130+wobsoriano@users.noreply.github.com> Agent-Logs-Url: https://github.com/wobsoriano/pinia-shared-state/sessions/79497100-2c52-409c-98f8-9f6981b6b990
Added vitest unit tests in 5679070. There are 23 tests across two files:
|
Great! Does it cover the issue that this PR is trying to fix as welll? |
Yes — the
The same pair of tests exists in |
surprise! |
When a store contains
ref-wrapped objects (e.g.,renderSettings = ref({ fps: 60 })), cross-tab updates via$patchwere replacing the reactive object entirely instead of merging into it — breaking any component that held a direct reference to the old reactive object.Root cause
The function form of
$patchsetsstate[key] = newValuethrough Vue's reactive proxy, which triggersref.value = newObject, creating a new reactive object. Components that captured the previous reactive reference (e.g. viastoreToRefsorconst settings = store.renderSettings) would hold a stale reference with no further updates.Changes
src/index.ts: Replace$patch(fn)with$patch(plainObject)built viareduceoverkeysToUpdatesrc/vanilla.ts: Replacestore[key] = evt.newValuedirect assignment withstore.$patch({ [key]: evt.newValue })src/__tests__/plugin.test.ts: Added 14 vitest unit tests for thePiniaSharedStatepluginsrc/__tests__/vanilla.test.ts: Added 9 vitest unit tests for theshare()vanilla functionWhen
$patchreceives a plain object, Pinia internally callsmergeReactiveObjects(), which mutates existing reactive objects in-place rather than replacing them — preserving reactive references throughout.Testing
Unit tests cover:
store.settings === originalSettings(same object reference, mutated in-place)omit,enable, andinitializeoptionssync()/unshare()lifecycle for the vanillashare()functionbroadcast-channelis mocked in-memory viavi.hoisted+vi.mock, so all tests run without browser APIs.Original prompt
⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.