Skip to content

Commit

Permalink
fix csr use-resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Varixo committed Aug 10, 2024
1 parent 9f3c4ee commit a4ae10a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
15 changes: 7 additions & 8 deletions packages/qwik/src/core/use/use-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {
type ResourceReturnInternal,
} from './use-task';

import type { Container2, fixMeAny } from '../../server/qwik-types';
import type { GetObjID } from '../container/container';
import type { JSXOutput } from '../render/jsx/types/jsx-node';
import { getProxyTarget } from '../state/common';
import { isSignal, type Signal } from '../state/signal';
import { createProxy, type StoreTracker } from '../state/store';
import { isPromise } from '../util/promises';
import { isObject } from '../util/types';
import { Store2Flags, createStore2, getStoreTarget2 } from '../v2/signal/v2-store';
import { useSequentialScope } from './use-sequential-scope';
import type { fixMeAny } from '../../server/qwik-types';

const DEBUG: boolean = false;

Expand Down Expand Up @@ -313,22 +312,22 @@ export const _createResourceReturn = <T>(opts?: ResourceOptions): ResourceReturn
};

export const createResourceReturn = <T>(
containerState: StoreTracker,
container: Container2,
opts?: ResourceOptions,
initialPromise?: Promise<T>
): ResourceReturnInternal<T> => {
const result = _createResourceReturn<T>(opts);
result.value = initialPromise as Promise<T>;
const resource = createProxy(result, containerState, undefined);
return resource;

return createStore2(container, result, Store2Flags.RECURSIVE);
};

export const getInternalResource = <T>(resource: ResourceReturn<T>): ResourceReturnInternal<T> => {
return getProxyTarget(resource) as any;
return getStoreTarget2(resource) as any;
};

export const isResourceReturn = (obj: any): obj is ResourceReturn<unknown> => {
return isObject(obj) && (getProxyTarget(obj as any) || obj).__brand === 'resource';
return isObject(obj) && (getStoreTarget2(obj as any) || obj).__brand === 'resource';
};

// TODO: to remove - serializers v1
Expand Down
53 changes: 27 additions & 26 deletions packages/qwik/src/core/use/use-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
SubscriptionType,
getSubscriptionManager,
noSerialize,
unwrapProxy,
type NoSerialize,
} from '../state/common';
import { QObjectManagerSymbol } from '../state/constants';
Expand All @@ -29,8 +28,14 @@ import { delay, isPromise, safeCall } from '../util/promises';
import { isFunction, isObject, type ValueOrPromise } from '../util/types';
import { ChoreType } from '../v2/shared/scheduler';
import { type Container2, type HostElement, type fixMeAny } from '../v2/shared/types';
import { ComputedSignal2, EffectProperty, throwIfQRLNotResolved } from '../v2/signal/v2-signal';
import {
ComputedSignal2,
EffectProperty,
isSignal2,
throwIfQRLNotResolved,
} from '../v2/signal/v2-signal';
import { type ReadonlySignal2, type Signal2 } from '../v2/signal/v2-signal.public';
import { unwrapStore2 } from '../v2/signal/v2-store';
import { invoke, newInvokeContext, untrack, waitAndRun } from './use-core';
import { useOn, useOnDocument } from './use-on';
import { useSequentialScope } from './use-sequential-scope';
Expand Down Expand Up @@ -178,7 +183,7 @@ export interface ResourceRejected<T> {
readonly loading: boolean;
}

export interface ResourceReturnInternal<T> {
export interface ResourceReturnInternal<T> extends Record<string, unknown> {
__brand: 'resource';
_state: 'pending' | 'resolved' | 'rejected';
_resolved: T | undefined;
Expand Down Expand Up @@ -357,7 +362,7 @@ export const runTask2 = (task: Task, container: Container2, host: HostElement) =
}
if (prop) {
return (obj as Record<string, unknown>)[prop];
} else if (isSignal(obj)) {
} else if (isSignal2(obj)) {
return obj.value;
} else {
return obj;
Expand Down Expand Up @@ -561,10 +566,9 @@ export const runResource = <T>(
cleanupTask(task);

const iCtx = newInvokeContext(container.$locale$, host as fixMeAny, undefined, ResourceEvent);
iCtx.$container2$ = container;

const taskFn = task.$qrl$.getFn(iCtx, () => {
container.$subsManager$.$clearSub$(task);
});
const taskFn = task.$qrl$.getFn(iCtx);

const resource = task.$state$;
assertDefined(
Expand All @@ -574,24 +578,21 @@ export const runResource = <T>(
);

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 (isSignal2(obj)) {
return obj.value;
} else {
return obj;
}
});
};

const handleError = (reason: unknown) => container.handleError(reason, host);
Expand All @@ -608,7 +609,7 @@ export const runResource = <T>(
done = true;
});

const resourceTarget = unwrapProxy(resource);
const resourceTarget = unwrapStore2(resource);
const opts: ResourceCtx<T> = {
track,
cleanup(fn) {
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/core/v2/shared/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const createScheduler = (
* @param waitForChore? = false
*/
function schedule(
type: ChoreType.TASK | ChoreType.VISIBLE | ChoreType.RESOURCE | ChoreType.COMPUTED,
type: ChoreType.TASK | ChoreType.VISIBLE | ChoreType.RESOURCE,
task: Task
): ValueOrPromise<void>;
function schedule(
Expand Down
11 changes: 7 additions & 4 deletions packages/qwik/src/core/v2/signal/v2-signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,13 @@ export const triggerEffects = (
if (isTask(effect)) {
effect.$flags$ |= TaskFlags.DIRTY;
DEBUG && log('schedule.effect.task', pad('\n' + String(effect), ' '));
container.$scheduler$(
effect.$flags$ & TaskFlags.VISIBLE_TASK ? ChoreType.VISIBLE : ChoreType.TASK,
effect
);
let choreType = ChoreType.TASK;
if (effect.$flags$ & TaskFlags.VISIBLE_TASK) {
choreType = ChoreType.VISIBLE;
} else if (effect.$flags$ & TaskFlags.RESOURCE) {
choreType = ChoreType.RESOURCE;
}
container.$scheduler$(choreType, effect);
} else if (effect instanceof Signal2) {
// we don't schedule ComputedSignal/DerivedSignal directly, instead we invalidate it and
// and schedule the signals effects (recursively)
Expand Down

0 comments on commit a4ae10a

Please sign in to comment.