Skip to content

Commit

Permalink
fix some use-task and use-visible tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Varixo committed Aug 8, 2024
1 parent 087872e commit 9e53aa9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
51 changes: 22 additions & 29 deletions packages/qwik/src/core/use/use-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { delay, isPromise, maybeThen, safeCall } from '../util/promises';
import { isFunction, isObject, type ValueOrPromise } from '../util/types';
import { ChoreType } from '../v2/shared/scheduler';
import { isContainer2, type Container2, type HostElement, type fixMeAny } from '../v2/shared/types';
import { EffectProperty } from '../v2/signal/v2-signal';
import {
createComputed2Qrl,
type ReadonlySignal2,
Expand Down Expand Up @@ -344,36 +345,28 @@ export const useTaskQrl = (qrl: QRL<TaskFn>, opts?: UseTaskOptions): void => {
}
};

export const runTask2 = (
task: TaskDescriptor | ComputedDescriptor<unknown>,
container: Container2,
host: HostElement
) => {
export const runTask2 = (task: Task, container: Container2, host: HostElement) => {
task.$flags$ &= ~TaskFlags.DIRTY;
const iCtx = newInvokeContext(container.$locale$, host as fixMeAny, undefined, TaskEvent);
const taskFn = task.$qrl$.getFn(iCtx, () => {
container.$subsManager$.$clearSub$(task);
}) as TaskFn;
iCtx.$container2$ = container;
const taskFn = task.$qrl$.getFn(iCtx) as TaskFn;

const track: Tracker = (obj: (() => unknown) | object | Signal, prop?: string) => {
if (isFunction(obj)) {
const ctx = newInvokeContext();
ctx.$subscriber$ = [SubscriptionType.HOST, task];
return invoke(ctx, obj);
}
const manager = getSubscriptionManager(obj);
if (manager) {
manager.$addSub$([SubscriptionType.HOST, task], prop);
} else {
logErrorAndStop(codeToText(QError_trackUseStore), obj);
}
if (prop) {
return (obj as Record<string, unknown>)[prop];
} else if (isSignal(obj)) {
return obj.value;
} else {
return obj;
}
const ctx = newInvokeContext();
ctx.$effectSubscriber$ = [task, EffectProperty.COMPONENT];
ctx.$container2$ = container;
return invoke(ctx, () => {
if (isFunction(obj)) {
return obj();
}
if (prop) {
return (obj as Record<string, unknown>)[prop];
} else if (isSignal(obj)) {
return obj.value;
} else {
return obj;
}
});
};
const handleError = (reason: unknown) => container.handleError(reason, host);
let cleanupFns: (() => void)[] | null = null;
Expand Down Expand Up @@ -558,7 +551,7 @@ export const runSubscriber2 = async (
} else if (isComputedTask(task)) {
return runComputed2(task, container, host);
} else {
return runTask2(task, container, host);
return runTask2(task as Task, container, host);
}
};

Expand Down Expand Up @@ -706,7 +699,7 @@ export const runTask = (
): ValueOrPromise<void> => {
task.$flags$ &= ~TaskFlags.DIRTY;

cleanupTask(task);
cleanupTask(task as Task);
const hostElement = task.$el$;
const iCtx = newInvokeContext(rCtx.$static$.$locale$, hostElement, undefined, TaskEvent);
iCtx.$renderCtx$ = rCtx;
Expand Down Expand Up @@ -791,7 +784,7 @@ export const runComputed = (
);
};

export const cleanupTask = (task: SubscriberEffect) => {
export const cleanupTask = (task: Task) => {
const destroy = task.$destroy$;
if (destroy) {
task.$destroy$ = null;
Expand Down
4 changes: 4 additions & 0 deletions packages/qwik/src/core/v2/shared/shared-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,10 @@ export const canSerialize2 = (value: any): boolean => {
return true;
} else if (isTask(value)) {
return true;
} else if (value instanceof Error) {
return true;
} else if (isPromise(value)) {
return true;
}
} else if (typeof value === 'function') {
if (isQrl(value) || isQwikComponent(value)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/core/v2/signal/v2-signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export const triggerEffects = (
DEBUG && log('schedule.effect.task', pad('\n' + String(effect), ' '));
container.$scheduler$(
effect.$flags$ & TaskFlags.VISIBLE_TASK ? ChoreType.VISIBLE : ChoreType.TASK,
effectSubscriptions as fixMeAny
effect
);
} else if (effect instanceof Signal2) {
// we don't schedule ComputedSignal/DerivedSignal directly, instead we invalidate it and
Expand Down
20 changes: 10 additions & 10 deletions packages/qwik/src/core/v2/tests/use-task.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,44 +406,44 @@ describe.each([
(globalThis as any).log = undefined;
});
it('should handle promises and tasks', async () => {
const log: string[] = [];
(global as any).log = [] as string[];
const MyComp = component$(() => {
log.push('render');
const promise = useSignal<Promise<number>>();
(global as any).log.push('render');

// Tasks should run one after the other, awaiting returned promises.
// Here we "sideload" a promise via the signal
useTask$(() => {
promise.value = Promise.resolve(0)
.then(() => {
log.push('inside.1');
(global as any).log.push('inside.1');
return delay(10);
})
.then(() => {
log.push('1b');
(global as any).log.push('1b');
return 1;
});
log.push('1a');
(global as any).log.push('1a');
});

useTask$(async () => {
log.push('2a');
(global as any).log.push('2a');
await delay(10);
log.push('2b');
(global as any).log.push('2b');
});

useTask$(() => {
promise.value = promise.value!.then(() => {
log.push('3b');
(global as any).log.push('3b');
return 3;
});
log.push('3a');
(global as any).log.push('3a');
});

return <p>Should have a number: "{promise.value}"</p>;
});
const { vNode } = await render(<MyComp />, { debug });
expect(log).toEqual([
expect((global as any).log).toEqual([
// 1st render
'render',
// task 1 returns sync and sideloads promise
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/core/v2/tests/use-visible-task.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe.each([
const double = useSignal(0);

useVisibleTask$(({ track }) => {
double.value = 2 * track(count);
double.value = 2 * track(() => count.value);
});
return (
<button
Expand Down

0 comments on commit 9e53aa9

Please sign in to comment.