Skip to content

Commit d67bb36

Browse files
committed
fix(Serialize): support SerializerSymbol
1 parent 993a81f commit d67bb36

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

packages/qwik/src/core/shared/shared-serialization.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ export const createSerializationContext = (
866866
let data;
867867
if ((arg as any).serialize) {
868868
data = (arg as any).serialize(toSerialize);
869+
} else if (SerializerSymbol in toSerialize) {
870+
data = (toSerialize as any)[SerializerSymbol](toSerialize);
869871
}
870872
if (data === undefined) {
871873
data = NEEDS_COMPUTATION;

packages/qwik/src/core/tests/use-serialized.spec.tsx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment as Signal, component$, useSignal } from '@qwik.dev/core';
1+
import { SerializerSymbol, Fragment as Signal, component$, useSignal } from '@qwik.dev/core';
22
import { domRender, ssrRenderToDom, trigger } from '@qwik.dev/core/testing';
33
import { describe, expect, it } from 'vitest';
44
import { useSerializer$ } from '../use/use-serializer';
@@ -95,6 +95,48 @@ describe.each([
9595
</>
9696
);
9797
});
98+
it('should support [SerializerSymbol]', async () => {
99+
const Counter = component$(() => {
100+
const count = useSerializer$({
101+
deserialize: (data: number) => new WithSerialize(data),
102+
});
103+
return (
104+
<button
105+
onClick$={() => {
106+
count.value.inc();
107+
count.force();
108+
}}
109+
>
110+
{count.value.count}
111+
</button>
112+
);
113+
});
114+
115+
const { vNode, container } = await render(<Counter />, { debug });
116+
expect(vNode).toMatchVDOM(
117+
<>
118+
<button>
119+
<Signal ssr-required>{'0'}</Signal>
120+
</button>
121+
</>
122+
);
123+
await trigger(container.element, 'button', 'click');
124+
expect(vNode).toMatchVDOM(
125+
<>
126+
<button>
127+
<Signal ssr-required>{'1'}</Signal>
128+
</button>
129+
</>
130+
);
131+
await trigger(container.element, 'button', 'click');
132+
expect(vNode).toMatchVDOM(
133+
<>
134+
<button>
135+
<Signal ssr-required>{'2'}</Signal>
136+
</button>
137+
</>
138+
);
139+
});
98140
});
99141

100142
class CustomSerialized {
@@ -103,3 +145,13 @@ class CustomSerialized {
103145
this.count++;
104146
}
105147
}
148+
149+
class WithSerialize {
150+
constructor(public count = 0) {}
151+
inc() {
152+
this.count++;
153+
}
154+
[SerializerSymbol](obj: this) {
155+
return obj.count;
156+
}
157+
}

0 commit comments

Comments
 (0)