From 3c8a4a81b42c3b78c0843ce3adcffde8997c9498 Mon Sep 17 00:00:00 2001 From: Varixo Date: Mon, 19 Aug 2024 14:30:20 +0200 Subject: [PATCH] add store toJSON --- packages/qwik/src/core/v2/signal/v2-signal.ts | 2 +- packages/qwik/src/core/v2/signal/v2-store.ts | 13 ++++ .../qwik/src/core/v2/tests/use-store.spec.tsx | 67 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/packages/qwik/src/core/v2/signal/v2-signal.ts b/packages/qwik/src/core/v2/signal/v2-signal.ts index 14eb57a71a7..aa36283949c 100644 --- a/packages/qwik/src/core/v2/signal/v2-signal.ts +++ b/packages/qwik/src/core/v2/signal/v2-signal.ts @@ -213,7 +213,7 @@ export class Signal2 extends Subscriber implements ISignal2 { toString() { return ( `[${this.constructor.name}${(this as any).$invalid$ ? ' INVALID' : ''} ${String(this.$untrackedValue$)}]` + - this.$effects$?.map((e) => '\n -> ' + pad(qwikDebugToString(e[0]), ' ')).join('\n') || '' + (this.$effects$?.map((e) => '\n -> ' + pad(qwikDebugToString(e[0]), ' ')).join('\n') || '') ); } toJSON() { diff --git a/packages/qwik/src/core/v2/signal/v2-store.ts b/packages/qwik/src/core/v2/signal/v2-store.ts index 3963fd18d96..d2be0a9d01f 100644 --- a/packages/qwik/src/core/v2/signal/v2-store.ts +++ b/packages/qwik/src/core/v2/signal/v2-store.ts @@ -1,5 +1,6 @@ import { pad, qwikDebugToString } from '../../debug'; import { assertTrue } from '../../error/assert'; +import { _wrapProp } from '../../state/signal'; import { tryGetInvokeContext } from '../../use/use-core'; import { isSerializableObject } from '../../util/types'; import { SERIALIZER_PROXY_UNWRAP } from '../shared/shared-serialization'; @@ -121,6 +122,18 @@ export class StoreHandler> implements Pro // we will return the naked object which removes ourselves, // and that is not the intention so prevent of SERIALIZER_PROXY_UNWRAP. return undefined; + } else if (p === 'toJSON') { + return () => { + // we need to add subscription to all properties + // TODO: could this be done another way? + for (const key in this.$target$) { + if (isStore2(this.$target$[key])) { + continue; + } + this.get(this.$target$, key); + } + return this.$target$; + }; } const target = this.$target$; const ctx = tryGetInvokeContext(); diff --git a/packages/qwik/src/core/v2/tests/use-store.spec.tsx b/packages/qwik/src/core/v2/tests/use-store.spec.tsx index 4b3d9f0ca66..596e8e574a1 100644 --- a/packages/qwik/src/core/v2/tests/use-store.spec.tsx +++ b/packages/qwik/src/core/v2/tests/use-store.spec.tsx @@ -466,6 +466,73 @@ describe.each([ ); }); + it('should render value via JSON.stringify', async () => { + const Stringify = component$<{ + data: any; + style?: any; + }>((props) => { + return <>{JSON.stringify(props.data)}; + }); + + const Cmp = component$(() => { + const group = useStore({ + controls: { + ctrl: { + value: '', + }, + }, + }); + + return ( + + ); + }); + + const { vNode, document } = await render(, { debug }); + expect(vNode).toMatchVDOM( + + + + ); + await trigger(document.body, 'button', 'click'); + expect(vNode).toMatchVDOM( + + + + ); + }); + describe('regression', () => { it('#5597 - should update value', async () => { (globalThis as any).clicks = 0;