ng-auto-unsubscribe
Version:
A utility base class for Angular to auto-unsubscribe from observables
1 lines • 4.15 kB
Source Map (JSON)
{"version":3,"file":"ng-auto-unsubscribe.mjs","sources":["../../../projects/ng-auto-unsubscribe/src/lib/auto-unsub-base.ts","../../../projects/ng-auto-unsubscribe/src/public-api.ts","../../../projects/ng-auto-unsubscribe/src/ng-auto-unsubscribe.ts"],"sourcesContent":["import { DestroyRef, Directive, inject } from '@angular/core';\r\nimport { takeUntilDestroyed } from '@angular/core/rxjs-interop';\r\nimport { Observable, OperatorFunction } from 'rxjs';\r\n\r\n/**\r\n * Abstract base class for angular components.\r\n * Automatically unsubscribe from RxJS observables when components destroyed, using Angular's\r\n * built-in `DestroyRef` and `takeUntilDestroyed`.\r\n *\r\n * Extend this class in your component to gain access to\r\n * convenient helper methods for automatic unsubscription.\r\n *\r\n * Example usage:\r\n * ```ts\r\n * @Component({...})\r\n * export class MySpecialComponent extends NgAutoUnsubscribe {\r\n * ngOnInit() {\r\n * this.streamUntil(this.myObservable$)\r\n * .subscribe(value => {\r\n * // handle value here\r\n * });\r\n * this.myObservable$\r\n * .pipe(this.until())\r\n * .subscribe(value => {\r\n * // handle value here\r\n * });\r\n * }\r\n * }\r\n * ```\r\n *\r\n * Note: Requires Angular 16+ for `DestroyRef` and `takeUntilDestroyed`.\r\n */\r\n\r\n@Directive() // Enables Angular DI for this class, required to let Angular process dependency injection in this base class\r\nexport abstract class NgAutoUnsubscribe {\r\n /** Injected Angular DestroyRef for unsubscription */\r\n protected readonly destroyRef = inject(DestroyRef);\r\n\r\n /**\r\n * Returns an operator function that applies `takeUntilDestroyed`\r\n * for automatic unsubscription.\r\n *\r\n * Usage:\r\n * ```ts\r\n * observable$.pipe(this.until()).subscribe();\r\n * ```\r\n */\r\n protected until<T>(): OperatorFunction<T, T> {\r\n return takeUntilDestroyed(this.destroyRef);\r\n }\r\n\r\n /**\r\n * Takes an observable and returns a new observable\r\n * that automatically unsubscribes on destroy.\r\n *\r\n * Usage:\r\n * ```ts\r\n * this.streamUntil(observable$).subscribe();\r\n * ```\r\n */\r\n protected streamUntil<T>(observable$: Observable<T>): Observable<T> {\r\n return observable$.pipe(takeUntilDestroyed(this.destroyRef));\r\n }\r\n\r\n /**\r\n * Subscribes to an observable and invokes a callback for each emitted value,\r\n * automatically unsubscribing when the component or service is destroyed.\r\n *\r\n * This is a shorthand for `.pipe(takeUntilDestroyed()).subscribe(callback)`\r\n * to reduce boilerplate in components or services.\r\n *\r\n * @param observable$ The observable to subscribe to.\r\n * @param callback Function to handle emitted values.\r\n *\r\n * Usage:\r\n * ```ts\r\n * this.watchUntil(this.myObservable$, value => {\r\n * // handle value here\r\n * });\r\n * ```\r\n */\r\n protected watchUntil<T>(\r\n observable$: Observable<T>,\r\n callback: (value: T) => void\r\n ): void {\r\n observable$\r\n .pipe(takeUntilDestroyed(this.destroyRef))\r\n .subscribe((valueFromObs) => callback(valueFromObs));\r\n }\r\n}\r\n","/*\r\n * Public API Surface of ng-auto-unsubscribe\r\n */\r\n\r\nexport * from './lib/auto-unsub-base';\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;MAGmB,iBAAiB,CAAA;;AAElB,IAAA,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;AAElD;;;;;;;;AAQG;IACO,KAAK,GAAA;AACb,QAAA,OAAO,kBAAkB,CAAC,IAAI,CAAC,UAAU,CAAC;;AAG5C;;;;;;;;AAQG;AACO,IAAA,WAAW,CAAI,WAA0B,EAAA;QACjD,OAAO,WAAW,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;;AAG9D;;;;;;;;;;;;;;;;AAgBG;IACO,UAAU,CAClB,WAA0B,EAC1B,QAA4B,EAAA;QAE5B;AACG,aAAA,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,UAAU,CAAC;aACxC,SAAS,CAAC,CAAC,YAAY,KAAK,QAAQ,CAAC,YAAY,CAAC,CAAC;;wGArDpC,iBAAiB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,SAAA,EAAA,CAAA;4FAAjB,iBAAiB,EAAA,YAAA,EAAA,IAAA,EAAA,QAAA,EAAA,EAAA,EAAA,CAAA;;4FAAjB,iBAAiB,EAAA,UAAA,EAAA,CAAA;kBADtC;;;ACjCD;;AAEG;;ACFH;;AAEG;;;;"}