Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular Signals' 'this' context usage prevents passing the .set() function #52007

Closed
chrisrocco opened this issue Oct 4, 2023 · 4 comments
Closed
Labels
area: core Issues related to the framework runtime cross-cutting: signals
Milestone

Comments

@chrisrocco
Copy link

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

core

Is this a regression?

No

Description

The Signals library makes clever usage of the this context to avoid creating extra functions when creating new Signals.

signalFn.set = signalSetFn;   // 👈 Depends on 'this'
// vs
signalFn.set = signalSetFn.bind(node);
// or (fastest?)
signalFn.set = signalSetFn(node);  // 👈 Returns a bound closure

The problem is it's easy to lose the this context by using the function as a value.

const current = signal(0);
dataStream.subscribe(current.set);  // 👈 Wrong 'this' context leads to obscure errors

This surfaces a minor trade off between performance and correctness.

Microbenchmarks suggest optimal performance would be creating a closure like so -

const signalSetFn = (node) => (newValue) => ...;
signalFn.set = signalSetFn(node);  // 👈 In the factory function

^ https://measurethat.net/Benchmarks/Show/1328/0/bind-vs-closure-declaration

That's what I'd propose.

Alternatively, we should at least add a descriptive error message since this will come up a lot.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

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

No response

Anything else?

No response

@pkozlowski-opensource pkozlowski-opensource added area: core Issues related to the framework runtime cross-cutting: signals labels Oct 4, 2023
@ngbot ngbot bot added this to the needsTriage milestone Oct 4, 2023
@pkozlowski-opensource
Copy link
Member

That's what I'd propose.

That's sounds reasonable to me. @chrisrocco would you like to crate a PR or do we want me to send a fix?

@chrisrocco
Copy link
Author

Yep, I can send the PR.

@alxhub
Copy link
Member

alxhub commented Oct 8, 2023

Fixed by #51986!

@alxhub alxhub closed this as completed Oct 8, 2023
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime cross-cutting: signals
Projects
None yet
Development

No branches or pull requests

3 participants