MCPcopy Create free account
hub / github.com/angular/angular / loadEffect

Method loadEffect

packages/core/src/resource/resource.ts:430–542  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

428 }
429
430 private async loadEffect(): Promise<void> {
431 const extRequest = this.extRequest();
432
433 // Capture the previous status before any state transitions. Note that this is `untracked` since
434 // we do not want the effect to depend on the state of the resource, only on the request.
435 const {status: currentStatus, previousStatus} = untracked(this.state);
436
437 // A changed request invalidates the previous load even when the new params are unavailable
438 // or errored. Cancel it before returning so its pending task cannot keep the app unstable.
439 this.abortInProgressLoad();
440
441 if (extRequest.request === undefined) {
442 // Nothing to load (and we should already be in a non-loading state).
443 return;
444 } else if (currentStatus !== 'loading') {
445 // We're not in a loading or reloading state, so this loading request is stale.
446 return;
447 }
448
449 // Capturing _this_ load's pending task in a local variable is important here. We may attempt to
450 // resolve it twice:
451 //
452 // 1. when the loading function promise resolves/rejects
453 // 2. when cancelling the loading operation
454 //
455 // After the loading operation is cancelled, `this.resolvePendingTask` no longer represents this
456 // particular task, but this `await` may eventually resolve/reject. Thus, when we cancel in
457 // response to (1) below, we need to cancel the locally saved task.
458 let resolvePendingTask: (() => void) | undefined = (this.resolvePendingTask =
459 this.pendingTasks.add());
460
461 const {signal: abortSignal} = (this.pendingController = new AbortController());
462
463 try {
464 // The actual loading is run through `untracked` - only the request side of `resource` is
465 // reactive. This avoids any confusion with signals tracking or not tracking depending on
466 // which side of the `await` they are.
467 const stream = untracked(() => {
468 return this.loaderFn({
469 params: extRequest.request as Exclude<R, undefined>,
470 abortSignal,
471 previous: {
472 status: previousStatus,
473 },
474 });
475 });
476
477 // If this request has been aborted, or the current request no longer
478 // matches this load, then we should ignore this resolution.
479 const shouldDiscard = () => abortSignal.aborted || untracked(this.extRequest) !== extRequest;
480
481 if (isSignal(stream)) {
482 if (shouldDiscard()) {
483 return;
484 }
485
486 this.state.set({
487 extRequest,

Callers

nothing calls this directly

Calls 10

abortInProgressLoadMethod · 0.95
untrackedFunction · 0.90
isSignalFunction · 0.90
signalFunction · 0.90
saveToTransferStateFunction · 0.85
rethrowFatalErrorsFunction · 0.85
encapsulateResourceErrorFunction · 0.85
createDebugNameObjectFunction · 0.70
addMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected