ngx-observable-lifecycle
Advanced tools
Comparing version 2.2.2 to 2.3.0
@@ -7,5 +7,23 @@ import { AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit } from '@angular/core'; | ||
export declare type LifecycleHookKey = keyof AllHooks; | ||
export declare type DecoratedHooks = Record<Exclude<LifecycleHookKey, 'ngOnChanges'>, Observable<void>> & { | ||
ngOnChanges: Observable<Parameters<OnChanges['ngOnChanges']>[0]>; | ||
export interface TypedSimpleChange<Data> { | ||
previousValue: Data; | ||
currentValue: Data; | ||
firstChange: boolean; | ||
} | ||
/** | ||
* FIRST POINT: | ||
* the key is made optional because an ngOnChanges will only give keys of inputs that have changed | ||
* SECOND POINT: | ||
* the value is associated with `| null` as if an input value is defined but actually retrieved with | ||
* an `async` pipe, we'll initially get a `null` value | ||
* | ||
* For both point, feel free to check the following stackblitz that demo this | ||
* https://stackblitz.com/edit/stackblitz-starters-s5uphw?file=src%2Fmain.ts | ||
*/ | ||
export declare type TypedSimpleChanges<Component, Keys extends keyof Component> = { | ||
[Key in Keys]?: TypedSimpleChange<Component[Key]> | null; | ||
}; | ||
export declare type DecoratedHooks<Component = any, Keys extends keyof Component = any> = Record<Exclude<LifecycleHookKey, 'ngOnChanges'>, Observable<void>> & { | ||
ngOnChanges: Observable<TypedSimpleChanges<Component, Keys>>; | ||
}; | ||
export declare type DecoratedHooksSub = { | ||
@@ -17,2 +35,2 @@ [k in keyof DecoratedHooks]: DecoratedHooks[k] extends Observable<infer U> ? Subject<U> : never; | ||
*/ | ||
export declare function getObservableLifecycle(classInstance: any): DecoratedHooks; | ||
export declare function getObservableLifecycle<Component, Inputs extends keyof Component = never>(classInstance: Component): DecoratedHooks<Component, Inputs>; |
{ | ||
"name": "ngx-observable-lifecycle", | ||
"version": "2.2.2", | ||
"version": "2.3.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "tslib": "^2.0.0" |
@@ -97,3 +97,4 @@ # NgxObservableLifecycle | ||
export class ChildComponent { | ||
@Input() input: number; | ||
@Input() input1: number | undefined | null; | ||
@Input() input2: string | undefined | null; | ||
@@ -110,5 +111,8 @@ constructor() { | ||
ngOnDestroy, | ||
} = getObservableLifecycle(this); | ||
} = | ||
// specifying the generics is only needed if you intend to | ||
// use the `ngOnChanges` observable, this way you'll have | ||
// typed input values instead of just a `SimpleChange` | ||
getObservableLifecycle<ChildComponent, 'input1' | 'input2'>(this); | ||
ngOnChanges.subscribe(() => console.count('onChanges')); | ||
ngOnInit.subscribe(() => console.count('onInit')); | ||
@@ -121,2 +125,15 @@ ngDoCheck.subscribe(() => console.count('doCheck')); | ||
ngOnDestroy.subscribe(() => console.count('onDestroy')); | ||
ngOnChanges.subscribe(changes => { | ||
console.count('onChanges'); | ||
// do note that we have a type safe object here for `changes` | ||
// with the inputs from our component and their associated values typed accordingly | ||
changes.input1?.currentValue; // `number | null | undefined` | ||
changes.input1?.previousValue; // `number | null | undefined` | ||
changes.input2?.currentValue; // `string | null | undefined` | ||
changes.input2?.previousValue; // `string | null | undefined` | ||
}); | ||
} | ||
@@ -123,0 +140,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36389
204
142