Title: Add WritableComputed to allow computed with write operations · Issue #55673 · angular/angular · GitHub
Open Graph Title: Add WritableComputed to allow computed with write operations · Issue #55673 · angular/angular
X Title: Add WritableComputed to allow computed with write operations · Issue #55673 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Revival with use case of #50498 Currently signal produces a writable signal. Currently computed produces a non writable signal. What I am missing i...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Revival with use case of #50498 Currently signal produces a writable signal. Currently computed produces a ...
X Description: Which @angular/* package(s) are relevant/related to the feature request? core Description Revival with use case of #50498 Currently signal produces a writable signal. Currently computed produces a ...
Opengraph URL: https://github.com/angular/angular/issues/55673
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add WritableComputed to allow computed with write operations","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\n\ncore\n\n### Description\n\nRevival with use case of #50498\r\n\r\nCurrently `signal` produces a writable signal.\r\nCurrently `computed` produces a non writable signal.\r\nWhat I am missing is a `writableComputed`.\r\n\r\nUse case example:\r\nI have a required input called `value` but inside the component I can edit this value without affecting the outside. At the end an apply button is clicked and I emit the new value via an output.\r\nSomething like this:\r\n```ts\r\nclass Test {\r\n value = input.required\u003cstring>();\r\n tempValue = signal(/*??*/);\r\n commit = output\u003cstring>()\r\n\r\n onEdit(newValue: string) {\r\n this.tempValue.set(newValue);\r\n }\r\n\r\n onApply() {\r\n this.commit.emit(this.tempValue());\r\n }\r\n}\r\n``` \r\nThe first problem is `tempValue` isn't based on `value`.\r\nThe second problem is when `value` is changed from outside. At this point I wish to discard `tempValue` and start fresh with the new `value`.\r\nIf I had `writableComputed` I could do:\r\n```ts\r\ntempValue = writableComputed(() => this.value());\r\n```\r\nWhich means anytime `value` changes it becomes the new `tempValue` value. If `tempValue` is edited the new edit is the `tempValue` new value until something gets changed in `value`.\n\n### Proposed solution\n\nA new `writableComputed` which tracks the value inside based on the writeable part and will get reset each time the computed function runs to what it creates.\r\nExamples:\r\n```ts\r\nconst a = signal(7);\r\nconst b = writableComputed(() => a() + 1);\r\n// b is 8\r\n```\r\n```ts\r\nconst a = signal(7);\r\nconst b = writableComputed(() => a() + 1);\r\nb.set(9);\r\n// A design question - 8 or 9\r\n```\r\n```ts\r\nconst a = signal(7);\r\nconst b = writableComputed(() => a() + 1);\r\na.set(11);\r\nb.set(9);\r\n// A design question - 12 or 9\r\n```\r\n```ts\r\nconst a = signal(7);\r\nconst b = writableComputed(() => a() + 1);\r\n// b is 8\r\n// Later\r\nb.set(10)\r\n// b is 10\r\n// Later\r\na.set(20);\r\n// b is 21\r\n```\n\n### Alternatives considered\n\nUsing effect. The downside is with required inputs.\r\n```ts\r\ntempValue = signal\u003cstring | undefined>(undefined);\r\neffect(() => this.tempValue.set(this.value()), {allowSignalWrites: true});\r\n```\r\nThis will result in `tempValue` having `undefined` initially and for example in `ngOnInit` it will still have `undefined` although `value` already have the actual value set. Also a computed will have its value set too.\r\nusing `writableComputed` in `ngOnInit` will have the correct computed value.\r\n\r\nAnother option is that scenario hints that something I am doing is wrong from the basis and I would like to hear better suggestions how to handle it. ","author":{"url":"https://github.com/Harpush","@type":"Person","name":"Harpush"},"datePublished":"2024-05-04T13:46:02Z","url":"https://github.com/angular/angular/issues/55673"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c8c689d9-921c-0747-9947-8e6ab26d1ac4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D7E:19E4BB:354A249:43FE3AA:6B01E890 |
| html-safe-nonce | 93cbe7acdd21cbca435f68a1609df45be728d7789fe868974c01346f6939a26d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDdFOjE5RTRCQjozNTRBMjQ5OjQzRkUzQUE6NkIwMUU4OTAiLCJ2aXNpdG9yX2lkIjoiMTk4OTY3NjM0NTQ3NTY2NzM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3bd1fbf79efe540321292338e8f2d49fb0561b432dc4e5b95797443890a213ab |
| hovercard-subject-tag | issue:2279005463 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/55673/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d59ae591ae207749a8bc80c2099f11c62eb870ab434c179ee885b9a646e4ad10/angular/angular/issues/55673 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d59ae591ae207749a8bc80c2099f11c62eb870ab434c179ee885b9a646e4ad10/angular/angular/issues/55673 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? core Description Revival with use case of #50498 Currently signal produces a writable signal. Currently computed produces a ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Harpush |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width