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;
import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/core/compiler/interfaces.dart';
bool hasLifecycleHook(interface, type) {
if (type is! Type) return false;
const INTERFACES = const {
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 * as Interfaces from './interfaces';
import {LifecycleHooks} from './interfaces';
export function hasLifecycleHook(lcInterface, type): boolean {
if (!(type instanceof Type)) return false;
export function hasLifecycleHook(lcInterface: LifecycleHooks, token): boolean {
if (!(token instanceof Type)) return false;
var proto = (<any>type).prototype;
var proto = (<any>token).prototype;
switch (lcInterface) {
case Interfaces.AfterContentInit:
case LifecycleHooks.AfterContentInit:
return !!proto.afterContentInit;
case Interfaces.AfterContentChecked:
case LifecycleHooks.AfterContentChecked:
return !!proto.afterContentChecked;
case Interfaces.AfterViewInit:
case LifecycleHooks.AfterViewInit:
return !!proto.afterViewInit;
case Interfaces.AfterViewChecked:
case LifecycleHooks.AfterViewChecked:
return !!proto.afterViewChecked;
case Interfaces.OnChanges:
case LifecycleHooks.OnChanges:
return !!proto.onChanges;
case Interfaces.DoCheck:
case LifecycleHooks.DoCheck:
return !!proto.doCheck;
case Interfaces.OnDestroy:
case LifecycleHooks.OnDestroy:
return !!proto.onDestroy;
case Interfaces.OnInit:
case LifecycleHooks.OnInit:
return !!proto.onInit;
default:
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 {PipeBinding} from '../pipes/pipe_binding';
import * as LifecycleHooks from './interfaces';
import {LifecycleHooks} from './interfaces';
var _staticKeys;

View File

@ -1,5 +1,16 @@
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:
* - `OnChanges` (if any bindings have changed),
@ -38,9 +49,7 @@ import {StringMap} from 'angular2/src/core/facade/collection';
* }
* ```
*/
export class OnChanges {
onChanges(changes: StringMap<string, any>): void {}
}
export interface OnChanges { onChanges(changes: StringMap<string, any>); }
/**
* Notify a directive when it has been checked the first time.
@ -54,15 +63,13 @@ export class OnChanges {
*
* ```
* @Component(...)
* class MyComponent @implements OnInit {
* class MyComponent implements OnInit {
* onInit(): void {
* }
* }
* ```
*/
export class OnInit {
onInit(): void {}
}
export interface OnInit { onInit(); }
/**
* Overrides the default change detection.
@ -83,9 +90,7 @@ export class OnInit {
* }
* ```
*/
export class DoCheck {
doCheck(): void {}
}
export interface DoCheck { doCheck(); }
/**
* Notify a directive whenever a {@link ViewMetadata} that contains it is destroyed.
@ -101,9 +106,7 @@ export class DoCheck {
* }
* ```
*/
export class OnDestroy {
onDestroy(): void {}
}
export interface OnDestroy { onDestroy(); }
/**
* 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 {
afterContentInit(): void {}
}
export interface AfterContentInit { afterContentInit(); }
/**
* 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 {
afterContentChecked(): void {}
}
export interface AfterContentChecked { afterContentChecked(); }
/**
* 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 {
afterViewInit(): void {}
}
export interface AfterViewInit { afterViewInit(); }
/**
* 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 {
afterViewChecked(): void {}
}
export interface AfterViewChecked { afterViewChecked(); }