Skip to content

Conversation

pkozlowski-opensource
Copy link
Member

The new asReadonly method on the WritableSignal interface makes it possible to create readonly instance of a writable signal.

Readonly signals can be accessed to read their value, but can't be changed using set, update or mutate methods.

@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Apr 12, 2023
@pkozlowski-opensource pkozlowski-opensource added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime target: rc This PR is targeted for the next release-candidate cross-cutting: signals and removed detected: feature PR contains a feature commit labels Apr 12, 2023
@ngbot ngbot bot added this to the Backlog milestone Apr 12, 2023
@pkozlowski-opensource pkozlowski-opensource marked this pull request as ready for review April 12, 2023 08:14
@pullapprove pullapprove bot requested review from alxhub, atscott and dylhunn April 12, 2023 08:15
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api, fw-core

@pullapprove pullapprove bot requested a review from jessicajaniuk April 13, 2023 16:22
const readOnlyCounter = counter.asReadonly();

// @ts-expect-error
expect(readOnlyCounter.set).toBeUndefined();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts-expect-error is clever here - we both assert that the type is right and that the value doesn't contain the method. 👍

@pullapprove pullapprove bot requested a review from alxhub April 13, 2023 16:22
Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api

@@ -137,6 +153,7 @@ export function signal<T>(initialValue: T, options?: CreateSignalOptions<T>): Wr
set: signalNode.set.bind(signalNode),
update: signalNode.update.bind(signalNode),
mutate: signalNode.mutate.bind(signalNode),
asReadonly: signalNode.asReadonly.bind(signalNode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth the code size to memoize things here? We could instead do:

Suggested change
asReadonly: signalNode.asReadonly.bind(signalNode)
asReadonly: () => createSignalFromFunction(signalNode, signalNode.signal.bind(signalNode)),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure! I could go either way. LMK if you've got a strong preference

@alxhub alxhub modified the milestones: v16-candidates, v16-final Apr 13, 2023
@pkozlowski-opensource pkozlowski-opensource removed the request for review from dylhunn April 13, 2023 17:18
@pkozlowski-opensource pkozlowski-opensource added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Apr 13, 2023
The new asReadonly method on the WritableSignal interface makes
it possible to create readonly instance of a writable signal.

Readonly signals can be accessed to read their value,
but can't be changed using set, update or mutate methods.
@pkozlowski-opensource pkozlowski-opensource changed the title feat(core): add asReadonly helper to writable signals refactor(core): add asReadonly helper to writable signals Apr 13, 2023
@jessicajaniuk
Copy link
Contributor

This PR was merged into the repository by commit 2d0fcd6.

jessicajaniuk pushed a commit that referenced this pull request Apr 13, 2023
The new asReadonly method on the WritableSignal interface makes
it possible to create readonly instance of a writable signal.

Readonly signals can be accessed to read their value,
but can't be changed using set, update or mutate methods.

PR Close #49802
@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 May 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime cross-cutting: signals target: rc This PR is targeted for the next release-candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants