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): fix the same task duplicates in serialized state #6623

Merged
merged 8 commits into from
Jul 1, 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
20 changes: 17 additions & 3 deletions packages/qwik/src/core/v2/shared/shared-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@ export const createSerializationContext = (
const unwrapObj = unwrapProxy(obj);
if (unwrapObj !== obj) {
discoveredValues.push(unwrapObj);
const manager = getSubscriptionManager(obj as object)!;

// add subscription host to the discovered values
for (const sub of manager.$subs$) {
for (let i = SubscriptionProp.HOST; i < sub.length; i++) {
discoveredValues.push(sub[i]);
}
}
} else if (id === undefined || isRoot) {
// Object has not been seen yet, must scan content
// But not for root.
Expand Down Expand Up @@ -1131,9 +1139,15 @@ function subscriptionManagerToString(
) {
const data: string[] = [];
for (const sub of subscriptionManager.$subs$) {
data.push(
sub.map((val, propId) => (propId === SubscriptionProp.TYPE ? val : $addRoot$(val))).join(' ')
);
let subData = '';
for (let i = 0; i < sub.length; i++) {
if (i === SubscriptionProp.TYPE) {
subData += sub[i];
} else {
subData += ' ' + $addRoot$(sub[i]);
}
}
data.push(subData);
}
return data.join(';');
}
Expand Down
60 changes: 58 additions & 2 deletions packages/qwik/src/core/v2/tests/use-task.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,63 @@ describe.each([
</Component>
);
});

it('should unsubscribe from removed component', async () => {
(global as any).logs = [] as string[];

const ToggleChild = component$((props: { name: string; count: number }) => {
useTask$(({ track }) => {
const count = track(() => props.count);
const logText = `Child of "${props.name}" (${count})`;
(global as any).logs.push(logText);
});

return (
<div>
<h1>Toggle {props.name}</h1>
</div>
);
});

const Toggle = component$(() => {
const store = useStore({
count: 0,
cond: false,
});
return (
<div>
<button id="increment" type="button" onClick$={() => store.count++}>
Root increment
</button>
<div>
{!store.cond ? (
<ToggleChild name="A" count={store.count} />
) : (
<ToggleChild name="B" count={store.count} />
)}
<button type="button" id="toggle" onClick$={() => (store.cond = !store.cond)}>
Toggle
</button>
</div>
</div>
);
});

const { document } = await render(<Toggle />, { debug });

await trigger(document.body, '#increment', 'click');
await trigger(document.body, '#toggle', 'click');
await trigger(document.body, '#increment', 'click');
await trigger(document.body, '#toggle', 'click');

expect((global as any).logs).toEqual([
'Child of "A" (0)', // init
'Child of "A" (1)', // increment
'Child of "B" (1)', // toggle
'Child of "B" (2)', // increment
'Child of "A" (2)', // toggle
]);
});
});
describe('queue', () => {
it('should execute dependant tasks', async () => {
Expand Down Expand Up @@ -470,8 +527,7 @@ describe.each([
});

describe('regression', () => {
// TODO(optimizer-test): problem still exists with the optimizer!
it.skip('#5782', async () => {
it('#5782', async () => {
const Child = component$(({ sig }: { sig: SignalType<SignalType<number>> }) => {
const counter = useSignal(0);
useTask$(({ track }) => {
Expand Down
Loading