Title: Allow lifecycle hooks to be defined on a directive instance and not only on a prototype · Issue #38241 · angular/angular · GitHub
Open Graph Title: Allow lifecycle hooks to be defined on a directive instance and not only on a prototype · Issue #38241 · angular/angular
X Title: Allow lifecycle hooks to be defined on a directive instance and not only on a prototype · Issue #38241 · angular/angular
Description: We tend to get repeated requests for Angular to invoke lifecycle hooks defined on a directive / component instance (and not only ones defined on a prototype). Examples of such requests (actually, reported as bugs) would be #38043 or #380...
Open Graph Description: We tend to get repeated requests for Angular to invoke lifecycle hooks defined on a directive / component instance (and not only ones defined on a prototype). Examples of such requests (actually, r...
X Description: We tend to get repeated requests for Angular to invoke lifecycle hooks defined on a directive / component instance (and not only ones defined on a prototype). Examples of such requests (actually, r...
Opengraph URL: https://github.com/angular/angular/issues/38241
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow lifecycle hooks to be defined on a directive instance and not only on a prototype","articleBody":"We tend to get repeated requests for Angular to invoke lifecycle hooks defined on a directive / component instance (and not only ones defined on a prototype). Examples of such requests (actually, reported as bugs) would be #38043 or #38087 (but I'm sure that there are more as I've seen similar issues reported in the past.\r\n\r\nTo start with, the current Angular behaviour is the result of *deliberate design choices* and not a simple bug / omission that we can \"just fix\". The current design of taking lifecycle hooks from a prototype is based on the following:\r\n* performance considerations;\r\n* inheritance support.\r\n\r\n### Performance considerations\r\n\r\nAs of today Angular assumes that all directives created from a given class have the same set of lifecycle functions (taken from a prototype). This assumptions allows us to make 2 significant optimisations:\r\n* store lifecycle hooks only once for a given directive class, regardless of how many times it is instantiated (imagine a ngFor loop over hundreds of components / directives - as of today we need to store info about lifecycle hooks only one, regardless of the number of instances - if each instance could have different lifecycle hooks we would have to allocate memory for each instance, adding to GC pressure, consuming more memory and making the app slower overall);\r\n* invoke lifecycle hooks on all instances without worrying about checking if a given hook exists - basically we _know_ that all components / directives share the same set so we can iterate over all instances and call hooks, without doing any checks.\r\n\r\nThose things might sound like micr-optimisations but when you profile applications it has significant impact. Angular always worked this way as we made the assumption that the user experience (performance of a final app for the users) is more important that the developers experience here. \r\n \r\n### Inheritance\r\n\r\nWith the assumptions of a fixed set of lifecycle hooks for a directive it is easy for us to compute a new set of lifecycle hooks in the directives inheritance tree. Again, we can do those calculations only once (per directive class), without checking each individual instance.\r\n\r\n## Possible work-arrounds \r\n\r\n@petebacondarwin [suggested](https://github.com/angular/angular/issues/38043#issuecomment-663475929) that one might work-arround the current design choices like so:\r\n\r\n```ts\r\n@Component({selector: 'app-root', template: ''})\r\nexport class AppComponent {\r\n title = 'test-app';\r\n}\r\n\r\n(AppComponent.prototype as any).ngOnInit = () => {\r\n console.log('init!');\r\n};\r\n```\r\n\r\nI'm opening this issue as a feature request, mostly to track interest and ideas for another implementation that wouldn't negatively impact performance. \r\n\r\n \r\n\r\n","author":{"url":"https://github.com/pkozlowski-opensource","@type":"Person","name":"pkozlowski-opensource"},"datePublished":"2020-07-27T10:27:24Z","url":"https://github.com/angular/angular/issues/38241"}
| 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:297de747-12ac-ced4-d3e1-032720ab7183 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C976:2D40DE:F6AA5C:1391194:6B014A58 |
| html-safe-nonce | 95e99200f7e7acd97041b744c912ece2f095231db2dcf194393c79c4391cda04 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOTc2OjJENDBERTpGNkFBNUM6MTM5MTE5NDo2QjAxNEE1OCIsInZpc2l0b3JfaWQiOiI0MzEyMTI3MDQ5MzUyNDMyMjE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9e555c84b62417cd8da6597d90699b0feb12e4d9d724324484859eef4b845cb4 |
| hovercard-subject-tag | issue:666178075 |
| 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/38241/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4ad8938a44864addae369f3655a52cc14e14154c43422330a16815f1fff93cfe/angular/angular/issues/38241 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4ad8938a44864addae369f3655a52cc14e14154c43422330a16815f1fff93cfe/angular/angular/issues/38241 |
| og:image:alt | We tend to get repeated requests for Angular to invoke lifecycle hooks defined on a directive / component instance (and not only ones defined on a prototype). Examples of such requests (actually, r... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pkozlowski-opensource |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width