Repository navigation
Description
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
I know that Signal API is still developer-preview. If it should be go to discussion part, let me know.
I read all Signal RFC & trying to use Signal for our translation library.
We forked @ngx-translate/core and customize its structure into Signal-based pipe.
https://gist.github.com/leo6104/1d8da771f1cb3da237ca3ba06fe4fdea#file-translate-pipe-ts
And it shows error message that is out of control range from developer. It would be some point that can be improved (show other error message or support WritableSignal.set in Pipe transform function.)
Please provide a link to a minimal reproduction of the bug
Here is minimal reproduction code from the library code.
import { ChangeDetectorRef, effect, EnvironmentInjector, Pipe, runInInjectionContext, signal } from '@angular/core';
@Pipe({
name: 'test',
standalone: true,
})
export class TestPipe {
private value = signal<string>('');
private lastKey = signal<string>(null);
constructor() {
effect((onCleanup) => {
const key = this.lastKey();
this.value.set(key + ' world');
}, { allowSignalWrites: true });
}
transform(_query: string | number, ...args: any[]): any {
const query = (typeof _query === 'number') ? `${_query}` : _query;
this.lastKey.set(query);
return this.value();
}
}
@Component({
selector: 'app',
template: `{{ 'hello' | test }}`,
})
export AppComponent {
}
With above code, i thought it should print hello world but it raise error. we cannot give allowSignalWrites to true because we didn't use computed/effect in transform function.
Please provide the exception or error you saw
ERROR 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.
at core.mjs:26566:15
at throwInvalidWriteToSignalError (core.mjs:2549:5)
at WritableSignalImpl.set (core.mjs:2584:13)
at TestPipe.transform (test.pipe.ts:37:18)
at pureFunction1Internal (core.mjs:22308:65)
at ɵɵpipeBind1 (core.mjs:22494:9)
at RootCmp_Template (template.html:15:1)
at ReactiveLViewConsumer.runInContext (core.mjs:10310:13)
at executeTemplate (core.mjs:11069:22)
at refreshView (core.mjs:10952:13)
h
### Please provide the environment you discovered this bug in (run `ng version`)
```true
Angular CLI: 16.0.0-rc.1
Node: 16.19.0
Package Manager: npm 8.19.3
OS: darwin x64
Angular: 16.1.0-next.0+sha-7ba01b4
... core
Package Version
-------------------------------------------------------------
@angular-devkit/architect 0.1600.0-rc.1
@angular-devkit/build-angular 16.0.0-rc.1
@angular-devkit/core 16.0.0-rc.1
@angular-devkit/schematics 16.0.0-rc.1
@angular/animations 16.0.0-rc.2
@angular/bazel 15.2.6
@angular/cdk 16.0.0-rc.1
@angular/cli 16.0.0-rc.1
@angular/common 16.0.0-rc.2
@angular/compiler 16.0.0-rc.2
@angular/compiler-cli 16.0.0-rc.2
@angular/elements 16.0.0-rc.2
@angular/forms 16.0.0-rc.2
@angular/localize 16.0.0-rc.2
@angular/material 16.0.0-rc.1
@angular/platform-browser 16.0.0-rc.2
@angular/platform-browser-dynamic 16.0.0-rc.2
@angular/platform-server 16.0.0-rc.2
@angular/router 16.0.0-rc.2
@angular/service-worker 16.0.0-rc.2
@angular/youtube-player 16.0.0-next.2
@nguniversal/builders 16.0.0-next.0
@nguniversal/common 16.0.0-next.0
@nguniversal/express-engine 16.0.0-next.0
@schematics/angular 16.0.0-rc.1
ng-packagr 16.0.0-rc.1
rxjs 7.8.0
typescript 5.0.3
Anything else?
Tested in platformBrowserDynamic().bootstrapModule. Not bootstrapApplication with standalone AppComponent
Activity
pkozlowski-opensource commentedon Apr 24, 2023
I'm sorry but it is not clear to me what you are trying to achieve here.
Generally speaking:
Could you please explain what you are trying to achieve and share a minimal, runnable example in stackblitz? Thnx!
pkozlowski-opensource commentedon Apr 24, 2023
Moreover, it might be the cases that effect is not needed here at all, why not using
computed?Not sure what value a pipe adds here but again, I don't think I understand the scenario here...
JeanMeche commentedon Apr 24, 2023
@pkozlowski-opensource I tried to narrow it down. Just writting to a signal into a pipe throws the error : https://stackblitz.com/edit/angular-next?file=src%2Fmain.ts
pkozlowski-opensource commentedon Apr 24, 2023
Thnx @JeanMeche - I think I know what is going on. This is the use-case that requires a bit more thinking IMO.
The thing is that pipe is normally used as a data synchronization mechanism. Basically a "smarter getter" called in a template. So the question here is: should we allow such use-cases in the transform part of the pipe?
It is really equivalent to writing to signal from a getter called in a template and it makes reasoning about data flow very hard. My initial reaction here is that the error thrown here actually is catching a problematic usage. But as I've said it needs more deliberation.
JeanMeche commentedon Apr 24, 2023
That being said, the error message probably needs to be rephrased as it can happen outside of effect/computed !
pkozlowski-opensource commentedon Apr 24, 2023
It actually does happen in an effect (one created by the framework to detect signal reads in a template). But I do agree that the error is not obvious and the error needs improvement.
leo6104 commentedon Apr 24, 2023
@JeanMeche @pkozlowski-opensource Thanks for comment.
I didn't catch the thing that signal was discouraged in effect. Thanks for the information.
The pipe i tried to implement was
translatepipe which supportruntime translation function.When
translatepipe called, it lazily load i18n files from network and transform its value to localized text.https://gist.github.com/leo6104/1d8da771f1cb3da237ca3ba06fe4fdea#file-translate-pipe-ts-L37-L41
It needs to call Observable and wait for json response. It is not sync behavior so i use
effects.It can be one example that someone can thinking like this implementation through
signal/effect/computed3 functions.I fully understand that it is difficult to support in framework-level.
As i mentioned in first section in this issue, feel free to close the issue if it should be go to discussion part. 😅
Thanks for having discussion about this implementation.
pkozlowski-opensource commentedon Apr 24, 2023
Thnx for sharing more info @leo6104 , appreciate it.
This is an interesting use-case but somehow specialized so I'm not sure if this is the best educational example. In any case we are going to discuss it more in the team.
leo6104 commentedon Apr 24, 2023
I close this issue because of my misunderstanding about current state of Signal + zone-based change detection.
I thought it is possible to use pipe with returning signal value in zone-based component and i expect that pipe/component will tracking the changes of signals. But it wasn't. After i tested it with this stackblitz i made, i realize that it doesn't trigger change detection when signal value changed.
I found the discussion #49682 (reply in thread) and i guess it will be only working on Signal-based component. They already have a question about pipe which return
signalvalue in transform function. I will follow-up in the RFC comment. The questions exactly same what i have. So, this issue is duplicate.I really look forward to improving rendering performance/code complexity/learning curves with signal.
Really thanks for making great things in Angular. @pkozlowski-opensource @JeanMeche and the teams.
angular-automatic-lock-bot commentedon May 25, 2023
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.