diff --git a/packages/qwik/src/core/use/use-resource.ts b/packages/qwik/src/core/use/use-resource.ts index d32a3ba8349..0226290f75f 100644 --- a/packages/qwik/src/core/use/use-resource.ts +++ b/packages/qwik/src/core/use/use-resource.ts @@ -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; @@ -313,22 +312,22 @@ export const _createResourceReturn = (opts?: ResourceOptions): ResourceReturn }; export const createResourceReturn = ( - containerState: StoreTracker, + container: Container2, opts?: ResourceOptions, initialPromise?: Promise ): ResourceReturnInternal => { const result = _createResourceReturn(opts); result.value = initialPromise as Promise; - const resource = createProxy(result, containerState, undefined); - return resource; + + return createStore2(container, result, Store2Flags.RECURSIVE); }; export const getInternalResource = (resource: ResourceReturn): ResourceReturnInternal => { - return getProxyTarget(resource) as any; + return getStoreTarget2(resource) as any; }; export const isResourceReturn = (obj: any): obj is ResourceReturn => { - 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 diff --git a/packages/qwik/src/core/use/use-task.ts b/packages/qwik/src/core/use/use-task.ts index 66ee94772dd..50486bacf16 100644 --- a/packages/qwik/src/core/use/use-task.ts +++ b/packages/qwik/src/core/use/use-task.ts @@ -12,7 +12,6 @@ import { SubscriptionType, getSubscriptionManager, noSerialize, - unwrapProxy, type NoSerialize, } from '../state/common'; import { QObjectManagerSymbol } from '../state/constants'; @@ -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'; @@ -178,7 +183,7 @@ export interface ResourceRejected { readonly loading: boolean; } -export interface ResourceReturnInternal { +export interface ResourceReturnInternal extends Record { __brand: 'resource'; _state: 'pending' | 'resolved' | 'rejected'; _resolved: T | undefined; @@ -357,7 +362,7 @@ export const runTask2 = (task: Task, container: Container2, host: HostElement) = } if (prop) { return (obj as Record)[prop]; - } else if (isSignal(obj)) { + } else if (isSignal2(obj)) { return obj.value; } else { return obj; @@ -561,10 +566,9 @@ export const runResource = ( 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( @@ -574,24 +578,21 @@ export const runResource = ( ); 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)[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)[prop]; + } else if (isSignal2(obj)) { + return obj.value; + } else { + return obj; + } + }); }; const handleError = (reason: unknown) => container.handleError(reason, host); @@ -608,7 +609,7 @@ export const runResource = ( done = true; }); - const resourceTarget = unwrapProxy(resource); + const resourceTarget = unwrapStore2(resource); const opts: ResourceCtx = { track, cleanup(fn) { diff --git a/packages/qwik/src/core/v2/shared/scheduler.ts b/packages/qwik/src/core/v2/shared/scheduler.ts index 581be53845b..183e6848b30 100644 --- a/packages/qwik/src/core/v2/shared/scheduler.ts +++ b/packages/qwik/src/core/v2/shared/scheduler.ts @@ -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; function schedule( diff --git a/packages/qwik/src/core/v2/signal/v2-signal.ts b/packages/qwik/src/core/v2/signal/v2-signal.ts index 6acb51a41b1..9ee6cfc1b27 100644 --- a/packages/qwik/src/core/v2/signal/v2-signal.ts +++ b/packages/qwik/src/core/v2/signal/v2-signal.ts @@ -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)