-
Notifications
You must be signed in to change notification settings - Fork 26.6k
refactor(core): add asReadonly helper to writable signals #49802
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
refactor(core): add asReadonly helper to writable signals #49802
Conversation
There was a problem hiding this 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
const readOnlyCounter = counter.asReadonly(); | ||
|
||
// @ts-expect-error | ||
expect(readOnlyCounter.set).toBeUndefined(); |
There was a problem hiding this comment.
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. 👍
There was a problem hiding this 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) |
There was a problem hiding this comment.
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:
asReadonly: signalNode.asReadonly.bind(signalNode) | |
asReadonly: () => createSignalFromFunction(signalNode, signalNode.signal.bind(signalNode)), |
There was a problem hiding this comment.
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
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.
eab0a2b
to
745e9a1
Compare
This PR was merged into the repository by commit 2d0fcd6. |
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
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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.