Skip to content

Calling resource.set(value) does not abort in-progress loaders if the given value is equal to the value before the resource was reloaded #59272

Closed

Description

@daiscog

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

I'm not sure whether this is a bug or whether this is intended, but it feels unintuitive to me. Therefore "expected" below is what I would have expected.

Expected behaviour

Calling resouce.set(value) should always abort in-progress async loading

Actual behaviour

Calling resouce.reload() then calling resouce.set(value) before the async reload is complete, with the same value that was set for the resource before reload was called, does not abort the reload operation.

This creates an inconsistent behaviour. E.g.:

resource.set({});
resources.reload();
resource.set(undefined); // cancels the reload


resource.set(undefined);
resources.reload();
resource.set(undefined); // does not cancel the reload

Example reproduction

In the following component, click the "Load it" button to trigger the reload, then click the "Cancel it" button to set the resource explicitly to undefined before the async load operation has completed.

import {Component, resource, ResourceStatus} from '@angular/core';
import {JsonPipe} from '@angular/common';

@Component({
  selector: 'app-root',
  imports: [JsonPipe],
  template: `
    <p>
      @if (something.value()) {
        {{something.value() | json}}
      } @else if (something.isLoading()) {
        Loading...
      } @else {
        Load it?
      }
    </p>
    @if (something.isLoading()) {
      <p><button type="button" (click)="cancelIt()">Cancel it</button></p>
    } @else {
      <p><button type="button" (click)="loadIt()">Load it</button></p>
    }
  `
})
export class AppComponent {
  readonly something = resource({
    loader: async ({previous, abortSignal}) => {
      if (previous.status === ResourceStatus.Idle) {
        return undefined;
      }
      return await someLongRunningTask(abortSignal);
    }
  });

  loadIt() {
    this.something.reload();
  }

  cancelIt() {
    // Setting a value after reload is called that is the same as the value
    // which was set before reload was called does not cancel the reload action
    this.something.set(undefined);

    // whereas setting a different value to the previous one always aborts in-progress reload operations
    // this.something.set({});
  }
}

function someLongRunningTask(abortSignal: AbortSignal) {
  return new Promise<unknown>((resolve, reject) => {
    if (abortSignal.aborted) {
      reject(abortSignal.reason);
      return;
    }

    let timeout: number | undefined = undefined;
    const abort = () => {
      clearTimeout(timeout);
      reject(abortSignal.reason);
    };
    abortSignal.addEventListener('abort', abort);
    timeout = setTimeout(() => {
      abortSignal.removeEventListener('abort', abort);
      resolve({success: true});
    }, 2_000)
  })
}

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 19.0.6
Node: 20.18.1
Package Manager: npm 10.8.2
OS: darwin arm64

Angular: 19.0.5
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.6
@angular-devkit/build-angular   19.0.6
@angular-devkit/core            19.0.6
@angular-devkit/schematics      19.0.6
@angular/cli                    19.0.6
@schematics/angular             19.0.6
rxjs                            7.8.1
typescript                      5.6.3
zone.js                         0.15.0

Anything else?

No response

Activity

  1. added
    area: core
    on Dec 20, 2024
  2. modified the milestone: needsTriage on Dec 20, 2024
  3. alxhub commented on Dec 21, 2024

    @alxhub
    Member

    I agree this feels counterintuitive. Sounds like something we could fix

  4. self-assigned this
    on Dec 22, 2024
  5. daiscog commented on Dec 28, 2024

    @daiscog
    Author

    Also, calling resource.set(undefined) after the previous loader threw an error does not clear the Resource's error state.

  6. added a commit that references this issue on Feb 7, 2025
    994f680
  7. angular-automatic-lock-bot commented on Mar 1, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corebugcross-cutting: signalsstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions