MCPcopy
hub / github.com/QwikDev/qwik / runResource

Function runResource

packages/qwik/src/core/use/use-task.ts:542–670  ·  view source on GitHub ↗
(
  task: ResourceDescriptor<T>,
  containerState: ContainerState,
  rCtx: RenderContext,
  waitOn?: Promise<unknown>
)

Source from the content-addressed store, hash-verified

540};
541
542export const runResource = <T>(
543 task: ResourceDescriptor<T>,
544 containerState: ContainerState,
545 rCtx: RenderContext,
546 waitOn?: Promise<unknown>
547): ValueOrPromise<void> => {
548 task.$flags$ &= ~TaskFlagsIsDirty;
549 cleanupTask(task);
550
551 const el = task.$el$;
552 const iCtx = newInvokeContext(rCtx.$static$.$locale$, el, undefined, TaskEvent);
553 const { $subsManager$: subsManager } = containerState;
554 iCtx.$renderCtx$ = rCtx;
555 const taskFn = task.$qrl$.getFn(iCtx, () => {
556 subsManager.$clearSub$(task);
557 });
558
559 const cleanups: (() => void)[] = [];
560 const resource = task.$state$;
561 assertDefined(
562 resource,
563 'useResource: when running a resource, "task.r" must be a defined.',
564 task
565 );
566
567 const track: Tracker = (obj: (() => unknown) | object | Signal, prop?: string) => {
568 if (isFunction(obj)) {
569 const ctx = newInvokeContext();
570 ctx.$renderCtx$ = rCtx;
571 ctx.$subscriber$ = [0, task];
572 return invoke(ctx, obj);
573 }
574 const manager = getSubscriptionManager(obj);
575 if (manager) {
576 manager.$addSub$([0, task], prop);
577 } else {
578 logErrorAndStop(codeToText(QError_trackUseStore), obj);
579 }
580 if (prop) {
581 return (obj as Record<string, unknown>)[prop];
582 } else if (isSignal(obj)) {
583 return obj.value;
584 } else {
585 return obj;
586 }
587 };
588 const resourceTarget = unwrapProxy(resource);
589 const opts: ResourceCtx<T> = {
590 track,
591 cleanup(callback) {
592 cleanups.push(callback);
593 },
594 cache(policy) {
595 let milliseconds = 0;
596 if (policy === 'immutable') {
597 milliseconds = Infinity;
598 } else {
599 milliseconds = policy;

Callers 2

useResourceQrlFunction · 0.90
runSubscriberFunction · 0.85

Calls 12

newInvokeContextFunction · 0.90
assertDefinedFunction · 0.90
unwrapProxyFunction · 0.90
invokeFunction · 0.90
isServerPlatformFunction · 0.90
noSerializeFunction · 0.90
safeCallFunction · 0.90
maybeThenFunction · 0.90
delayFunction · 0.90
cleanupTaskFunction · 0.85
setStateFunction · 0.85
fnFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…