Skip to content

Commit b42b9fc

Browse files
committed
refactor(hooks): change to intrefaces
1 parent 39ce9d3 commit b42b9fc

4 files changed

Lines changed: 48 additions & 41 deletions

File tree

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
library angular2.src.core.compiler.directive_lifecycle_reflector;
22

33
import 'package:angular2/src/core/reflection/reflection.dart';
4+
import 'package:angular2/src/core/compiler/interfaces.dart';
45

5-
bool hasLifecycleHook(interface, type) {
6-
if (type is! Type) return false;
6+
const INTERFACES = const {
7+
LifecycleHooks.OnInit: OnInit,
8+
LifecycleHooks.OnDestroy: OnDestroy,
9+
LifecycleHooks.DoCheck: DoCheck,
10+
LifecycleHooks.OnChanges: OnChanges,
11+
LifecycleHooks.AfterContentInit: AfterContentInit,
12+
LifecycleHooks.AfterContentChecked: AfterContentChecked,
13+
LifecycleHooks.AfterViewInit: AfterViewInit,
14+
LifecycleHooks.AfterViewChecked: AfterViewChecked,
15+
};
716

8-
return reflector.interfaces(type).contains(interface);
17+
bool hasLifecycleHook(LifecycleHooks interface, token) {
18+
if (token is! Type) return false;
19+
Type interfaceType = INTERFACES[interface];
20+
return reflector.interfaces(token).contains(interfaceType);
921
}

modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import {Type} from 'angular2/src/core/facade/lang';
2-
import * as Interfaces from './interfaces';
2+
import {LifecycleHooks} from './interfaces';
33

4-
export function hasLifecycleHook(lcInterface, type): boolean {
5-
if (!(type instanceof Type)) return false;
4+
export function hasLifecycleHook(lcInterface: LifecycleHooks, token): boolean {
5+
if (!(token instanceof Type)) return false;
66

7-
var proto = (<any>type).prototype;
7+
var proto = (<any>token).prototype;
88

99
switch (lcInterface) {
10-
case Interfaces.AfterContentInit:
10+
case LifecycleHooks.AfterContentInit:
1111
return !!proto.afterContentInit;
12-
case Interfaces.AfterContentChecked:
12+
case LifecycleHooks.AfterContentChecked:
1313
return !!proto.afterContentChecked;
14-
case Interfaces.AfterViewInit:
14+
case LifecycleHooks.AfterViewInit:
1515
return !!proto.afterViewInit;
16-
case Interfaces.AfterViewChecked:
16+
case LifecycleHooks.AfterViewChecked:
1717
return !!proto.afterViewChecked;
18-
case Interfaces.OnChanges:
18+
case LifecycleHooks.OnChanges:
1919
return !!proto.onChanges;
20-
case Interfaces.DoCheck:
20+
case LifecycleHooks.DoCheck:
2121
return !!proto.doCheck;
22-
case Interfaces.OnDestroy:
22+
case LifecycleHooks.OnDestroy:
2323
return !!proto.onDestroy;
24-
case Interfaces.OnInit:
24+
case LifecycleHooks.OnInit:
2525
return !!proto.onInit;
2626
default:
2727
return false;

modules/angular2/src/core/compiler/element_injector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {RenderDirectiveMetadata} from 'angular2/src/core/render/api';
5151
import {EventConfig} from 'angular2/src/core/render/event_config';
5252
import {PipeBinding} from '../pipes/pipe_binding';
5353

54-
import * as LifecycleHooks from './interfaces';
54+
import {LifecycleHooks} from './interfaces';
5555

5656
var _staticKeys;
5757

modules/angular2/src/core/compiler/interfaces.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import {StringMap} from 'angular2/src/core/facade/collection';
22

3+
export enum LifecycleHooks {
4+
OnInit,
5+
OnDestroy,
6+
DoCheck,
7+
OnChanges,
8+
AfterContentInit,
9+
AfterContentChecked,
10+
AfterViewInit,
11+
AfterViewChecked
12+
}
13+
314
/**
415
* Lifecycle hooks are guaranteed to be called in the following order:
516
* - `OnChanges` (if any bindings have changed),
@@ -38,9 +49,7 @@ import {StringMap} from 'angular2/src/core/facade/collection';
3849
* }
3950
* ```
4051
*/
41-
export class OnChanges {
42-
onChanges(changes: StringMap<string, any>): void {}
43-
}
52+
export interface OnChanges { onChanges(changes: StringMap<string, any>); }
4453

4554
/**
4655
* Notify a directive when it has been checked the first time.
@@ -54,15 +63,13 @@ export class OnChanges {
5463
*
5564
* ```
5665
* @Component(...)
57-
* class MyComponent @implements OnInit {
66+
* class MyComponent implements OnInit {
5867
* onInit(): void {
5968
* }
6069
* }
6170
* ```
6271
*/
63-
export class OnInit {
64-
onInit(): void {}
65-
}
72+
export interface OnInit { onInit(); }
6673

6774
/**
6875
* Overrides the default change detection.
@@ -83,9 +90,7 @@ export class OnInit {
8390
* }
8491
* ```
8592
*/
86-
export class DoCheck {
87-
doCheck(): void {}
88-
}
93+
export interface DoCheck { doCheck(); }
8994

9095
/**
9196
* Notify a directive whenever a {@link ViewMetadata} that contains it is destroyed.
@@ -101,9 +106,7 @@ export class DoCheck {
101106
* }
102107
* ```
103108
*/
104-
export class OnDestroy {
105-
onDestroy(): void {}
106-
}
109+
export interface OnDestroy { onDestroy(); }
107110

108111
/**
109112
* Notify a directive when the bindings of all its content children have been checked the first
@@ -119,9 +122,7 @@ export class OnDestroy {
119122
* }
120123
* ```
121124
*/
122-
export class AfterContentInit {
123-
afterContentInit(): void {}
124-
}
125+
export interface AfterContentInit { afterContentInit(); }
125126

126127
/**
127128
* Notify a directive when the bindings of all its content children have been checked (whether
@@ -137,9 +138,7 @@ export class AfterContentInit {
137138
* }
138139
* ```
139140
*/
140-
export class AfterContentChecked {
141-
afterContentChecked(): void {}
142-
}
141+
export interface AfterContentChecked { afterContentChecked(); }
143142

144143
/**
145144
* Notify a directive when the bindings of all its view children have been checked the first time
@@ -155,9 +154,7 @@ export class AfterContentChecked {
155154
* }
156155
* ```
157156
*/
158-
export class AfterViewInit {
159-
afterViewInit(): void {}
160-
}
157+
export interface AfterViewInit { afterViewInit(); }
161158

162159
/**
163160
* Notify a directive when the bindings of all its view children have been checked (whether they
@@ -173,6 +170,4 @@ export class AfterViewInit {
173170
* }
174171
* ```
175172
*/
176-
export class AfterViewChecked {
177-
afterViewChecked(): void {}
178-
}
173+
export interface AfterViewChecked { afterViewChecked(); }

0 commit comments

Comments
 (0)