Skip to content

Commit

Permalink
fix qrl resolve chore
Browse files Browse the repository at this point in the history
  • Loading branch information
Varixo committed Aug 22, 2024
1 parent ea6ed77 commit d10846b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
48 changes: 23 additions & 25 deletions packages/qwik/src/core/v2/shared/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ export const createScheduler = (

function schedule(
type: ChoreType.QRL_RESOLVE,
ignore0: null,
ignore1: QRLInternal<any>
ignore: null,
target: QRLInternal<any>
): ValueOrPromise<void>;
function schedule(type: ChoreType.JOURNAL_FLUSH): ValueOrPromise<void>;
function schedule(type: ChoreType.WAIT_FOR_ALL): ValueOrPromise<void>;
Expand Down Expand Up @@ -369,7 +369,7 @@ export const createScheduler = (
break;
case ChoreType.QRL_RESOLVE: {
const target = chore.$target$ as QRLInternal<any>;
returnValue = target.resolve();
returnValue = !target.resolved ? target.resolve() : null;
break;
}
}
Expand Down Expand Up @@ -417,7 +417,12 @@ function choreComparator(a: Chore, b: Chore, shouldThrowOnHostMismatch: boolean)
if (a.$type$ !== ChoreType.JOURNAL_FLUSH) {
const aHost = a.$host$;
const bHost = b.$host$;
if (aHost !== bHost) {

const aHostIsQrlResolve = a.$type$ === ChoreType.QRL_RESOLVE;
const bHostIsQrlResolve = b.$type$ === ChoreType.QRL_RESOLVE;

// QRL_RESOLVE does not have a host.
if (aHost !== bHost && !aHostIsQrlResolve && !bHostIsQrlResolve) {
if (vnode_isVNode(aHost) && vnode_isVNode(bHost)) {
// we are running on the client.
const hostDiff = vnode_documentPosition(aHost, bHost);
Expand All @@ -444,6 +449,18 @@ function choreComparator(a: Chore, b: Chore, shouldThrowOnHostMismatch: boolean)
return microTypeDiff;
}

/**
* QRL_RESOLVE is a special case. It does not have a host nor $idx$. We want to process
* QRL_RESOLVE chores as FIFO, so we need to return 1.
*/
if (
aHostIsQrlResolve &&
bHostIsQrlResolve &&
(a.$target$ as QRLInternal<any>).$symbol$ !== (b.$target$ as QRLInternal<any>).$symbol$
) {
return 1;
}

const idxDiff = toNumber(a.$idx$) - toNumber(b.$idx$);
if (idxDiff !== 0) {
return idxDiff;
Expand All @@ -453,26 +470,6 @@ function choreComparator(a: Chore, b: Chore, shouldThrowOnHostMismatch: boolean)
return 0;
}

export const intraHostPredicate = (a: Chore, b: Chore): number => {
const idxDiff = toNumber(a.$idx$) - toNumber(b.$idx$);
if (idxDiff !== 0) {
return idxDiff;
}
const typeDiff = a.$type$ - b.$type$;
if (typeDiff !== 0) {
return typeDiff;
}
if (a.$payload$ !== b.$payload$) {
return 0;
}
if (a.$payload$ instanceof Task && b.$payload$ instanceof Task) {
const aHash = a.$payload$.$qrl$.$hash$;
const bHash = b.$payload$.$qrl$.$hash$;
return aHash === bHash ? 0 : aHash < bHash ? -1 : 1;
}
return 0;
};

function sortedFindIndex(sortedArray: Chore[], value: Chore): number {
/// We need to ensure that the `queue` is sorted by priority.
/// 1. Find a place where to insert into.
Expand Down Expand Up @@ -525,7 +522,8 @@ function debugChoreToString(chore: Chore): string {
} as any
)[chore.$type$] || 'UNKNOWN: ' + chore.$type$;
const host = String(chore.$host$).replaceAll(/\n.*/gim, '');
return `Chore(${type} ${host} ${chore.$idx$})`;
const qrlTarget = (chore.$target$ as QRLInternal<any>).$symbol$;
return `Chore(${type} ${chore.$type$ === ChoreType.QRL_RESOLVE ? qrlTarget : host} ${chore.$idx$})`;
}

function debugTrace(
Expand Down
3 changes: 3 additions & 0 deletions packages/qwik/src/core/v2/signal/v2-signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { type QRLInternal } from '../../qrl/qrl-class';
import type { QRL } from '../../qrl/qrl.public';
import { trackSignal2, tryGetInvokeContext } from '../../use/use-core';
import { Task, TaskFlags, isTask } from '../../use/use-task';
import { logError } from '../../util/log';
import { ELEMENT_PROPS, OnRenderProp, QSubscribers } from '../../util/markers';
import { isPromise } from '../../util/promises';
import { qDev } from '../../util/qdev';
Expand Down Expand Up @@ -311,6 +312,8 @@ export const triggerEffects = (
try {
signal = effect;
effect.$effects$?.forEach(scheduleEffect);
} catch (e: unknown) {
logError(e);
} finally {
signal = previousSignal;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/qwik/src/core/v2/tests/use-computed.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
useStore,
} from '@builder.io/qwik';
import { describe, expect, it } from 'vitest';
import { trigger } from '../../../testing/element-fixture';
import { domRender, ssrRenderToDom } from '../../../testing/rendering.unit-util';
import '../../../testing/vdom-diff.unit-util';
import { trigger, domRender, ssrRenderToDom } from '@builder.io/qwik/testing';

const debug = false; //true;
Error.stackTraceLimit = 100;
Expand Down
2 changes: 1 addition & 1 deletion starters/e2e/e2e.computed.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from "@playwright/test";

test.describe("slot", () => {
test.describe("computed", () => {
function tests() {
test("should implement basic computed values", async ({ page }) => {
const result = page.locator(".result");
Expand Down

0 comments on commit d10846b

Please sign in to comment.