Title: Proposal: Provide Component Lifecycle Hooks as Observables · Issue #10185 · angular/angular · GitHub
Open Graph Title: Proposal: Provide Component Lifecycle Hooks as Observables · Issue #10185 · angular/angular
X Title: Proposal: Provide Component Lifecycle Hooks as Observables · Issue #10185 · angular/angular
Description: I'm submitting a ... [ ] bug report [x] feature request [ ] support request Current behavior Currently, component lifecycle hooks are handled by implementing one or more interfaces provided by angular. I find myself often wanting to subs...
Open Graph Description: I'm submitting a ... [ ] bug report [x] feature request [ ] support request Current behavior Currently, component lifecycle hooks are handled by implementing one or more interfaces provided by angu...
X Description: I'm submitting a ... [ ] bug report [x] feature request [ ] support request Current behavior Currently, component lifecycle hooks are handled by implementing one or more interfaces provided by ...
Opengraph URL: https://github.com/angular/angular/issues/10185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal: Provide Component Lifecycle Hooks as Observables","articleBody":"**I'm submitting a ...** \n\n```\n[ ] bug report\n[x] feature request\n[ ] support request\n```\n\n**Current behavior**\n Currently, component lifecycle hooks are handled by implementing one or more interfaces provided by angular. I find myself often wanting to subscribe to an observable until the component/directive is destroyed. To do this, I have to have a member for each subscription for later disposal in the `ngOnDestroy()` method.\nExample:\n\n``` typescript\n/* imports */\n\n@Directive({\n selector: '[onDrag]'\n})\nexport class OnDrag implements OnInit, OnDestroy {\n dragStart: Subscription;\n dragEnter: Subscription;\n dragOver: Subscription;\n dragLeave: Subscription;\n\n constructor(private element: ElementRef) { }\n\n ngOnInit(): any {\n let elem = this.element.nativeElement;\n\n this.dragStart = Observable.fromEvent(elem, 'dragstart')\n .do(() =>{ /*do stuff*/ })\n .subscribe();\n\n this.dragEnter = Observable.fromEvent(elem, 'dragenter')\n .do(() =>{ /*do stuff*/ })\n .subscribe();\n\n this.dragOver = Observable.fromEvent(elem, 'dragover')\n .do(() =>{ /*do stuff*/ })\n .subscribe();\n\n this.dragLeave = Observable.fromEvent(elem, 'dragleave')\n .do(() =>{ /*do stuff*/ })\n .subscribe();\n\n // etc...\n }\n\n ngOnDestroy(): any {\n this.dragStart.unsubscribe();\n this.dragEnter.unsubscribe();\n this.dragOver.unsubscribe();\n this.dragLeave.unsubscribe();\n }\n}\n```\n\n**Desired behavior**\nMy suggestion is to provide components with an observable for each lifecycle event (via injection perhaps?). The above example could turn into something like:\n\n``` typescript\n/* imports */\n\n@Directive({\n selector: '[onDrag]'\n})\nexport class OnDrag implements OnInit {\n constructor(\n private element: ElementRef,\n private lifecycle: ThingWithLifecycleObservablesForThisComponent\n ) { }\n\n ngOnInit(): any {\n let elem = this.element.nativeElement;\n\n Observable.fromEvent(elem, 'dragstart')\n .do(() =>{ /*do stuff*/ })\n .takeUntil(this.lifecycle.ngDestroy$)\n .subscribe();\n\n Observable.fromEvent(elem, 'dragenter')\n .do(() =>{ /*do stuff*/ })\n .takeUntil(this.lifecycle.ngDestroy$)\n .subscribe();\n\n Observable.fromEvent(elem, 'dragover')\n .do(() =>{ /*do stuff*/ })\n .takeUntil(this.lifecycle.ngDestroy$)\n .subscribe();\n\n Observable.fromEvent(elem, 'dragleave')\n .do(() =>{ /*do stuff*/ })\n .takeUntil(this.lifecycle.ngDestroy$)\n .subscribe();\n\n // etc...\n }\n}\n```\n\n**What is the motivation / use case for changing the behavior?**\nUltimately, I would like a way to not have to hold onto subscriptions as class members (perhaps there is a better way to achieve this than my suggestion). It could be convenient/concise to be able to handle lifecycle events as observables within components. As a workaround currently I have been extending my components with a base class that provides (some) of these events:\n\n``` typescript\nimport { OnInit, OnDestroy } from '@angular/core';\nimport { Subject } from 'rxjs/Subject';\n\nexport class ComponentLifeCycle implements OnInit, OnDestroy {\n protected ngInit$: Subject\u003cany> = new Subject\u003cany>();\n protected ngDestroy$: Subject\u003cany> = new Subject\u003cany>();\n\n ngOnInit(): any {\n this.ngInit$.next({});\n }\n\n ngOnDestroy(): any {\n this.ngDestroy$.next({});\n }\n}\n```\n\n**Please tell us about your environment:**\n- **Angular version:** 2.0.0-rc.4\n- **Browser:** all \n- **Language:** all\n","author":{"url":"https://github.com/Jeff-Cortese","@type":"Person","name":"Jeff-Cortese"},"datePublished":"2016-07-20T17:08:40Z","url":"https://github.com/angular/angular/issues/10185"}
| 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:c631d089-b18d-22f6-fbff-cef25516d975 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 97B8:1CEA6F:1BF293B:24CB321:6B014FB6 |
| html-safe-nonce | 788dc888d0bd8c7f86bb0d03719dfa3f995fc692896b898ce839ce113f573ef1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0I4OjFDRUE2RjoxQkYyOTNCOjI0Q0IzMjE6NkIwMTRGQjYiLCJ2aXNpdG9yX2lkIjoiNDE5ODM4NzU4MzY4NTg0MDgyMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a7afcb73f9e02e3b562fa53e7264ea60d1b4584ba2d71a7bbb3f46f1ab9bd355 |
| hovercard-subject-tag | issue:166633218 |
| 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/10185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5e2607eafb94e2d54ebe98e8c8c3a221482be584e06c1c589c2cea250339a9de/angular/angular/issues/10185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5e2607eafb94e2d54ebe98e8c8c3a221482be584e06c1c589c2cea250339a9de/angular/angular/issues/10185 |
| og:image:alt | I'm submitting a ... [ ] bug report [x] feature request [ ] support request Current behavior Currently, component lifecycle hooks are handled by implementing one or more interfaces provided by angu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Jeff-Cortese |
| 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