Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): deep store update #6681

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/qwik/src/core/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const createProxy = <T extends object>(
serializedArrayTarget,
new ReadWriteProxyHandler(storeTracker, manager)
) as T;
storeTracker.$proxyMap$.set(serializedArrayTarget, proxy);
storeTracker.$proxyMap$.set(target, proxy);
const serializedState = getSerializedState(target);
if (serializedState) {
addSubscriptions(serializedState, target, serializedArrayTarget);
Expand Down
70 changes: 70 additions & 0 deletions packages/qwik/src/core/v2/tests/use-store.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,76 @@ describe.each([
});
});

it('should deep watch store', async () => {
const Cmp = component$(() => {
const store = useStore({
nested: {
fields: { are: 'also tracked' },
},
list: ['Item 1'],
});

return (
<>
<p>{store.nested.fields.are}</p>
<button
id="tracked"
onClick$={() => {
// Even though we are mutating a nested object, this will trigger a re-render
store.nested.fields.are = 'tracked';
}}
></button>
<button
id="add-item"
onClick$={() => {
// Because store is deep watched, this will trigger a re-render
store.list.push(`Item ${store.list.length}`);
}}
></button>
<ul>
{store.list.map((item, key) => (
<li key={key}>{item}</li>
))}
</ul>
</>
);
});

const { vNode, document } = await render(<Cmp />, { debug });
expect(vNode).toMatchVDOM(
<Component>
<Fragment>
<p>
<Signal>also tracked</Signal>
</p>
<button id="tracked"></button>
<button id="add-item"></button>
<ul>
<li key="0">Item 1</li>
</ul>
</Fragment>
</Component>
);
await trigger(document.body, 'button#add-item', 'click');
await trigger(document.body, 'button#add-item', 'click');
expect(vNode).toMatchVDOM(
<Component>
<Fragment>
<p>
<Signal>also tracked</Signal>
</p>
<button id="tracked"></button>
<button id="add-item"></button>
<ul>
<li key="0">Item 1</li>
<li key="1">Item 1</li>
<li key="2">Item 2</li>
</ul>
</Fragment>
</Component>
);
});

describe('regression', () => {
it('#5597 - should update value', async () => {
(globalThis as any).clicks = 0;
Expand Down
Loading