Skip to content

Can't set signal from within async piped Observable #50382

Closed

Description

@ChellappanRajan

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

core

Is this a regression?

I don't know

Description

When attempting to assign a signal value from within an Observable that is being piped to an async function, I encounter an below issue.

Error: NG0600: Writing to signals is not allowed in a computed or an effect by default. Use allowSignalWrites in the CreateEffectOptions to enable this inside effects.

However I am not using effect here.

SO:https://stackoverflow.com/questions/76291361/cant-set-signal-from-within-async-piped-observable

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-cehebt?file=src%2Fmain.ts

Please provide the exception or error you saw

Error: NG0600: Writing to signals is not allowed in a computed or an effect by default. Use allowSignalWrites in the CreateEffectOptions to enable this inside effects.

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

No response

Anything else?

No response

Activity

  1. JeanMeche commented on May 20, 2023

    @JeanMeche
    Member

    What I can notice is that the issue only happens when the observable is synchronous.

    This throws

    @Component({
      selector: 'my-app',
      standalone: true,
      imports: [CommonModule],
      template: `{{obs$ | async}}`,
    })
    export class App {
      sig = signal(3);
      obs$ = of(3).pipe(tap(this.sig.set));
    }
    

    while this doesn't.

    @Component({
      selector: 'my-app',
      standalone: true,
      imports: [CommonModule],
      template: `{{obs$ | async}}`,
    })
    export class App {
      sig = signal(3);
      obs$ = timer(0, 1000).pipe(tap(this.sig.set));
    }
    

    This seens to be related to #49967

  2. ChellappanRajan commented on May 20, 2023

    @ChellappanRajan
    ContributorAuthor

    @JeanMeche As a workaround I have provided example with delay operator. Do you think it will cause any issue?

  3. added
    area: core
    on May 22, 2023
  4. modified the milestone: needsTriage on May 22, 2023
  5. montella1507 commented on May 25, 2023

    @montella1507

    This Is really big breaking change, destroying advanced applications.

    Doing such a thing Is Common scenario, very often reconmended way how to "omit" manual Subscribe And unscubscribe in conponent And let async pipe do the work.

  6. added a commit that references this issue on May 31, 2023
    7e1bc51
  7. princemaple commented on Jun 14, 2023

    @princemaple

    This is still happening in 16.1?

    Or is this a different scenario:

    I have an async pipe that triggers an http request that goes through an interceptor which triggers a signal update.

    The observable is a cache stream, it sends the http request when the resource is not cached yet.
    The http request triggers a loading interceptor that updates a request counter signal on a service.
    This allows me to have a loading component that spins whenever there are requests in flight.

  8. princemaple commented on Jun 14, 2023

    @princemaple

    Actually I figured it out, was my bad migrating to signal but not fully understanding the constraints:

    @Injectable({providedIn: 'root'})
    export class LoadingInterceptor implements HttpInterceptor {
      constructor(private loadingService: LoadingService) {}
    
      intercept(req: HttpRequest<any>, next: HttpHandler): Observable<any> {
        // async pipe triggers cache loading -> triggers request -> triggers interceptor -> updates signal
        // 👇 the update is in the same sync task context, and is run during the template rendering
        this.loadingService.up();
        
        // All I had to do was to change it to use Promise or queueMicrotask:
        Promise.resolve().then(() => {
          this.loadingService.up();
        });
    
        return next.handle(req).pipe(
          finalize(() => {
            // this is async so all good 👌
            this.loadingService.down();
          }),
        );
      }
    }
  9. angular-automatic-lock-bot commented on Jul 15, 2023

    @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.

  10. locked and limited conversation to collaborators on Jul 15, 2023
  11. added a commit that references this issue on Jan 23, 2024
    7ca66ce
  12. added a commit that references this issue on Jun 6, 2024
    72b4ff4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecross-cutting: signals

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions