refactor(hooks): change to intrefaces

This commit is contained in:
Misko Hevery 2015-09-11 14:23:24 -07:00
parent 39ce9d3397
commit b42b9fc42d
4 changed files with 48 additions and 41 deletions

View File

@ -1,9 +1,21 @@
library angular2.src.core.compiler.directive_lifecycle_reflector; library angular2.src.core.compiler.directive_lifecycle_reflector;
import 'package:angular2/src/core/reflection/reflection.dart'; import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/core/compiler/interfaces.dart';
bool hasLifecycleHook(interface, type) { const INTERFACES = const {
if (type is! Type) return false; LifecycleHooks.OnInit: OnInit,
LifecycleHooks.OnDestroy: OnDestroy,
LifecycleHooks.DoCheck: DoCheck,
LifecycleHooks.OnChanges: OnChanges,
LifecycleHooks.AfterContentInit: AfterContentInit,
LifecycleHooks.AfterContentChecked: AfterContentChecked,
LifecycleHooks.AfterViewInit: AfterViewInit,
LifecycleHooks.AfterViewChecked: AfterViewChecked,
};
return reflector.interfaces(type).contains(interface); bool hasLifecycleHook(LifecycleHooks interface, token) {
if (token is! Type) return false;
Type interfaceType = INTERFACES[interface];
return reflector.interfaces(token).contains(interfaceType);
} }

View File

@ -1,27 +1,27 @@
import {Type} from 'angular2/src/core/facade/lang'; import {Type} from 'angular2/src/core/facade/lang';
import * as Interfaces from './interfaces'; import {LifecycleHooks} from './interfaces';
export function hasLifecycleHook(lcInterface, type): boolean { export function hasLifecycleHook(lcInterface: LifecycleHooks, token): boolean {
if (!(type instanceof Type)) return false; if (!(token instanceof Type)) return false;
var proto = (<any>type).prototype; var proto = (<any>token).prototype;
switch (lcInterface) { switch (lcInterface) {
case Interfaces.AfterContentInit: case LifecycleHooks.AfterContentInit:
return !!proto.afterContentInit; return !!proto.afterContentInit;
case Interfaces.AfterContentChecked: case LifecycleHooks.AfterContentChecked:
return !!proto.afterContentChecked; return !!proto.afterContentChecked;
case Interfaces.AfterViewInit: case LifecycleHooks.AfterViewInit:
return !!proto.afterViewInit; return !!proto.afterViewInit;
case Interfaces.AfterViewChecked: case LifecycleHooks.AfterViewChecked:
return !!proto.afterViewChecked; return !!proto.afterViewChecked;
case Interfaces.OnChanges: case LifecycleHooks.OnChanges:
return !!proto.onChanges; return !!proto.onChanges;
case Interfaces.DoCheck: case LifecycleHooks.DoCheck:
return !!proto.doCheck; return !!proto.doCheck;
case Interfaces.OnDestroy: case LifecycleHooks.OnDestroy:
return !!proto.onDestroy; return !!proto.onDestroy;
case Interfaces.OnInit: case LifecycleHooks.OnInit:
return !!proto.onInit; return !!proto.onInit;
default: default:
return false; return false;

View File

@ -51,7 +51,7 @@ import {RenderDirectiveMetadata} from 'angular2/src/core/render/api';
import {EventConfig} from 'angular2/src/core/render/event_config'; import {EventConfig} from 'angular2/src/core/render/event_config';
import {PipeBinding} from '../pipes/pipe_binding'; import {PipeBinding} from '../pipes/pipe_binding';
import * as LifecycleHooks from './interfaces'; import {LifecycleHooks} from './interfaces';
var _staticKeys; var _staticKeys;

View File

@ -1,5 +1,16 @@
import {StringMap} from 'angular2/src/core/facade/collection'; import {StringMap} from 'angular2/src/core/facade/collection';
export enum LifecycleHooks {
OnInit,
OnDestroy,
DoCheck,
OnChanges,
AfterContentInit,
AfterContentChecked,
AfterViewInit,
AfterViewChecked
}
/** /**
* Lifecycle hooks are guaranteed to be called in the following order: * Lifecycle hooks are guaranteed to be called in the following order:
* - `OnChanges` (if any bindings have changed), * - `OnChanges` (if any bindings have changed),
@ -38,9 +49,7 @@ import {StringMap} from 'angular2/src/core/facade/collection';
* } * }
* ``` * ```
*/ */
export class OnChanges { export interface OnChanges { onChanges(changes: StringMap<string, any>); }
onChanges(changes: StringMap<string, any>): void {}
}
/** /**
* Notify a directive when it has been checked the first time. * Notify a directive when it has been checked the first time.
@ -54,15 +63,13 @@ export class OnChanges {
* *
* ``` * ```
* @Component(...) * @Component(...)
* class MyComponent @implements OnInit { * class MyComponent implements OnInit {
* onInit(): void { * onInit(): void {
* } * }
* } * }
* ``` * ```
*/ */
export class OnInit { export interface OnInit { onInit(); }
onInit(): void {}
}
/** /**
* Overrides the default change detection. * Overrides the default change detection.
@ -83,9 +90,7 @@ export class OnInit {
* } * }
* ``` * ```
*/ */
export class DoCheck { export interface DoCheck { doCheck(); }
doCheck(): void {}
}
/** /**
* Notify a directive whenever a {@link ViewMetadata} that contains it is destroyed. * Notify a directive whenever a {@link ViewMetadata} that contains it is destroyed.
@ -101,9 +106,7 @@ export class DoCheck {
* } * }
* ``` * ```
*/ */
export class OnDestroy { export interface OnDestroy { onDestroy(); }
onDestroy(): void {}
}
/** /**
* Notify a directive when the bindings of all its content children have been checked the first * Notify a directive when the bindings of all its content children have been checked the first
@ -119,9 +122,7 @@ export class OnDestroy {
* } * }
* ``` * ```
*/ */
export class AfterContentInit { export interface AfterContentInit { afterContentInit(); }
afterContentInit(): void {}
}
/** /**
* Notify a directive when the bindings of all its content children have been checked (whether * Notify a directive when the bindings of all its content children have been checked (whether
@ -137,9 +138,7 @@ export class AfterContentInit {
* } * }
* ``` * ```
*/ */
export class AfterContentChecked { export interface AfterContentChecked { afterContentChecked(); }
afterContentChecked(): void {}
}
/** /**
* Notify a directive when the bindings of all its view children have been checked the first time * Notify a directive when the bindings of all its view children have been checked the first time
@ -155,9 +154,7 @@ export class AfterContentChecked {
* } * }
* ``` * ```
*/ */
export class AfterViewInit { export interface AfterViewInit { afterViewInit(); }
afterViewInit(): void {}
}
/** /**
* Notify a directive when the bindings of all its view children have been checked (whether they * Notify a directive when the bindings of all its view children have been checked (whether they
@ -173,6 +170,4 @@ export class AfterViewInit {
* } * }
* ``` * ```
*/ */
export class AfterViewChecked { export interface AfterViewChecked { afterViewChecked(); }
afterViewChecked(): void {}
}