From b42b9fc42d894e7488f64be3f432f600ffcbc767 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 11 Sep 2015 14:23:24 -0700 Subject: [PATCH] refactor(hooks): change to intrefaces --- .../directive_lifecycle_reflector.dart | 18 ++++++-- .../compiler/directive_lifecycle_reflector.ts | 24 +++++----- .../src/core/compiler/element_injector.ts | 2 +- .../angular2/src/core/compiler/interfaces.ts | 45 +++++++++---------- 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart index b028ef8b2e..bd5e3992ce 100644 --- a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart +++ b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart @@ -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); } diff --git a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts index c79bfeb8b2..4dbd1800bd 100644 --- a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts +++ b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts @@ -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 = (type).prototype; + var proto = (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; diff --git a/modules/angular2/src/core/compiler/element_injector.ts b/modules/angular2/src/core/compiler/element_injector.ts index d1b74c52fd..8c4c913bfa 100644 --- a/modules/angular2/src/core/compiler/element_injector.ts +++ b/modules/angular2/src/core/compiler/element_injector.ts @@ -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; diff --git a/modules/angular2/src/core/compiler/interfaces.ts b/modules/angular2/src/core/compiler/interfaces.ts index 7961cceb6d..1a46d1a139 100644 --- a/modules/angular2/src/core/compiler/interfaces.ts +++ b/modules/angular2/src/core/compiler/interfaces.ts @@ -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): void {} -} +export interface OnChanges { onChanges(changes: StringMap); } /** * 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(); }