|
8 | 8 |
|
9 | 9 | import {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit, Type} from '@angular/core'; |
10 | 10 |
|
11 | | -import {MapWrapper} from './facade/collection'; |
12 | 11 | import {LifecycleHooks, reflector} from './private_import_core'; |
13 | 12 |
|
14 | | -const LIFECYCLE_INTERFACES: Map<any, Type<any>> = MapWrapper.createFromPairs([ |
15 | | - [LifecycleHooks.OnInit, OnInit], |
16 | | - [LifecycleHooks.OnDestroy, OnDestroy], |
17 | | - [LifecycleHooks.DoCheck, DoCheck], |
18 | | - [LifecycleHooks.OnChanges, OnChanges], |
19 | | - [LifecycleHooks.AfterContentInit, AfterContentInit], |
20 | | - [LifecycleHooks.AfterContentChecked, AfterContentChecked], |
21 | | - [LifecycleHooks.AfterViewInit, AfterViewInit], |
22 | | - [LifecycleHooks.AfterViewChecked, AfterViewChecked], |
23 | | -]); |
24 | | - |
25 | | -const LIFECYCLE_PROPS: Map<any, string> = MapWrapper.createFromPairs([ |
26 | | - [LifecycleHooks.OnInit, 'ngOnInit'], |
27 | | - [LifecycleHooks.OnDestroy, 'ngOnDestroy'], |
28 | | - [LifecycleHooks.DoCheck, 'ngDoCheck'], |
29 | | - [LifecycleHooks.OnChanges, 'ngOnChanges'], |
30 | | - [LifecycleHooks.AfterContentInit, 'ngAfterContentInit'], |
31 | | - [LifecycleHooks.AfterContentChecked, 'ngAfterContentChecked'], |
32 | | - [LifecycleHooks.AfterViewInit, 'ngAfterViewInit'], |
33 | | - [LifecycleHooks.AfterViewChecked, 'ngAfterViewChecked'], |
34 | | -]); |
35 | 13 |
|
36 | 14 | export function hasLifecycleHook(hook: LifecycleHooks, token: any): boolean { |
37 | | - var lcInterface = LIFECYCLE_INTERFACES.get(hook); |
38 | | - var lcProp = LIFECYCLE_PROPS.get(hook); |
39 | | - return reflector.hasLifecycleHook(token, lcInterface, lcProp); |
| 15 | + return reflector.hasLifecycleHook(token, getInterface(hook), getHookName(hook)); |
| 16 | +} |
| 17 | + |
| 18 | +function getHookName(hook: LifecycleHooks): string { |
| 19 | + switch (hook) { |
| 20 | + case LifecycleHooks.OnInit: |
| 21 | + return 'ngOnInit'; |
| 22 | + case LifecycleHooks.OnDestroy: |
| 23 | + return 'ngOnDestroy'; |
| 24 | + case LifecycleHooks.DoCheck: |
| 25 | + return 'ngDoCheck'; |
| 26 | + case LifecycleHooks.OnChanges: |
| 27 | + return 'ngOnChanges'; |
| 28 | + case LifecycleHooks.AfterContentInit: |
| 29 | + return 'ngAfterContentInit'; |
| 30 | + case LifecycleHooks.AfterContentChecked: |
| 31 | + return 'ngAfterContentChecked'; |
| 32 | + case LifecycleHooks.AfterViewInit: |
| 33 | + return 'ngAfterViewInit'; |
| 34 | + case LifecycleHooks.AfterViewChecked: |
| 35 | + return 'ngAfterViewChecked'; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function getInterface(hook: LifecycleHooks): any { |
| 40 | + switch (hook) { |
| 41 | + case LifecycleHooks.OnInit: |
| 42 | + return OnInit; |
| 43 | + case LifecycleHooks.OnDestroy: |
| 44 | + return OnDestroy; |
| 45 | + case LifecycleHooks.DoCheck: |
| 46 | + return DoCheck; |
| 47 | + case LifecycleHooks.OnChanges: |
| 48 | + return OnChanges; |
| 49 | + case LifecycleHooks.AfterContentInit: |
| 50 | + return AfterContentInit; |
| 51 | + case LifecycleHooks.AfterContentChecked: |
| 52 | + return AfterContentChecked; |
| 53 | + case LifecycleHooks.AfterViewInit: |
| 54 | + return AfterViewInit; |
| 55 | + case LifecycleHooks.AfterViewChecked: |
| 56 | + return AfterViewChecked; |
| 57 | + } |
40 | 58 | } |
0 commit comments