( task: ResourceDescriptor<T>, containerState: ContainerState, rCtx: RenderContext, waitOn?: Promise<unknown> )
| 540 | }; |
| 541 | |
| 542 | export 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; |
no test coverage detected
searching dependent graphs…