From 3d38ec8aacaaaeb5a4b1e563243ed927bb4d0f03 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 2 Sep 2015 16:43:39 -0700 Subject: [PATCH] refactor(Lifecycle hooks): move the hooks to their own module (lifecycle_hooks) BREAKING CHANGE Lifecycle hooks now live in the `angular2/lifecycle_hooks` module. They previously lived in the `metadata` module. --- docs/public-docs-package/index.js | 1 + modules/angular2/angular2.ts | 1 + modules/angular2/angular2_exports.ts | 1 + modules/angular2/lifecycle_hooks.ts | 17 +++++++++++++++++ modules/angular2/metadata.dart | 1 - modules/angular2/metadata.ts | 19 +++---------------- .../directive_lifecycle_reflector.dart | 2 +- .../compiler/directive_lifecycle_reflector.ts | 2 +- .../angular2/src/core/compiler/interfaces.ts | 19 ++++++++----------- .../angular2/src/core/directives/ng_class.ts | 3 ++- .../angular2/src/core/directives/ng_for.ts | 3 ++- .../angular2/src/core/directives/ng_style.ts | 3 ++- .../angular2/src/core/metadata/directives.ts | 9 +++++++++ .../src/forms/directives/ng_control_group.ts | 3 ++- .../src/forms/directives/ng_control_name.ts | 3 ++- .../src/forms/directives/ng_form_control.ts | 3 ++- .../src/forms/directives/ng_form_model.ts | 3 ++- .../angular2/src/forms/directives/ng_model.ts | 3 ++- .../compiler/dynamic_component_loader_spec.ts | 3 ++- .../core/compiler/element_injector_spec.ts | 4 ++-- .../directive_lifecycle_integration_spec.ts | 7 ++----- modules/angular2/web_worker/worker.ts | 7 ++++--- .../transform/common/interface_matcher.dart | 16 ++++++++-------- 23 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 modules/angular2/lifecycle_hooks.ts diff --git a/docs/public-docs-package/index.js b/docs/public-docs-package/index.js index 6a4782d6d1..54a3ae6665 100644 --- a/docs/public-docs-package/index.js +++ b/docs/public-docs-package/index.js @@ -5,6 +5,7 @@ module.exports = new Package('angular-v2-public-docs', [basePackage]) .config(function(readTypeScriptModules) { readTypeScriptModules.sourceFiles = [ + 'angular2/lifecycle_hooks.ts', 'angular2/metadata.ts', 'angular2/change_detection.ts', 'angular2/core.ts', diff --git a/modules/angular2/angular2.ts b/modules/angular2/angular2.ts index d59b9ad594..5bec573b44 100644 --- a/modules/angular2/angular2.ts +++ b/modules/angular2/angular2.ts @@ -15,3 +15,4 @@ export * from './forms'; export * from './render'; export * from './profile'; export {bootstrap} from 'angular2/src/core/application'; +export * from './lifecycle_hooks'; diff --git a/modules/angular2/angular2_exports.ts b/modules/angular2/angular2_exports.ts index b286e10836..088ab78d42 100644 --- a/modules/angular2/angular2_exports.ts +++ b/modules/angular2/angular2_exports.ts @@ -7,3 +7,4 @@ export * from './forms'; export * from './render'; export * from './profile'; export {bootstrap} from 'angular2/src/core/application'; +export * from './lifecycle_hooks'; diff --git a/modules/angular2/lifecycle_hooks.ts b/modules/angular2/lifecycle_hooks.ts new file mode 100644 index 0000000000..ce78f771f9 --- /dev/null +++ b/modules/angular2/lifecycle_hooks.ts @@ -0,0 +1,17 @@ +/** + * @module + * @description + * Defines interfaces to be implemented by directives when they need to hook into the change + * detection mechanism. + */ + +export { + AfterContentInit, + AfterContentChecked, + AfterViewInit, + AfterViewChecked, + OnChanges, + OnDestroy, + OnInit, + DoCheck +} from './src/core/compiler/interfaces'; diff --git a/modules/angular2/metadata.dart b/modules/angular2/metadata.dart index 81484884f2..eb4c1f4399 100644 --- a/modules/angular2/metadata.dart +++ b/modules/angular2/metadata.dart @@ -1,4 +1,3 @@ library angular2.metadata; export 'package:angular2/src/core/metadata.dart'; -export 'package:angular2/src/core/compiler/interfaces.dart'; diff --git a/modules/angular2/metadata.ts b/modules/angular2/metadata.ts index 544d759b04..e0c8f2113c 100644 --- a/modules/angular2/metadata.ts +++ b/modules/angular2/metadata.ts @@ -3,10 +3,9 @@ * @description * * Annotations provide the additional information that Angular requires in order to run your - * application. This module - * contains {@link ComponentMetadata}, {@link DirectiveMetadata}, and {@link ViewMetadata} - * annotations, as well as - * the {@link Host} annotation that is used by Angular to resolve dependencies. + * application. This module contains {@link ComponentMetadata}, {@link DirectiveMetadata}, and + * {@link ViewMetadata} annotations, as well as the {@link Host} annotation that is used by Angular + * to resolve dependencies. * */ @@ -48,17 +47,5 @@ export { HostListenerMetadata } from './src/core/metadata'; -export { - // todo(vbe): LifecycleHook should not be exposed (fails test.typings) - LifecycleHook, - AfterContentInit, - AfterContentChecked, - AfterViewInit, - AfterViewChecked, - OnChanges, - OnDestroy, - OnInit, - DoCheck -} from './src/core/compiler/interfaces'; export {Class, ClassDefinition, TypeDecorator} from './src/core/util/decorators'; diff --git a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart index 4f71a1fcec..b028ef8b2e 100644 --- a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart +++ b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.dart @@ -2,7 +2,7 @@ library angular2.src.core.compiler.directive_lifecycle_reflector; import 'package:angular2/src/core/reflection/reflection.dart'; -bool hasLifecycleHook(/*LifecycleHook*/ interface, type) { +bool hasLifecycleHook(interface, type) { if (type is! Type) return false; return reflector.interfaces(type).contains(interface); diff --git a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts index 5956e6f8da..c79bfeb8b2 100644 --- a/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts +++ b/modules/angular2/src/core/compiler/directive_lifecycle_reflector.ts @@ -1,7 +1,7 @@ import {Type} from 'angular2/src/core/facade/lang'; import * as Interfaces from './interfaces'; -export function hasLifecycleHook(lcInterface: Interfaces.LifecycleHook, type): boolean { +export function hasLifecycleHook(lcInterface, type): boolean { if (!(type instanceof Type)) return false; var proto = (type).prototype; diff --git a/modules/angular2/src/core/compiler/interfaces.ts b/modules/angular2/src/core/compiler/interfaces.ts index 85acc8c57d..7961cceb6d 100644 --- a/modules/angular2/src/core/compiler/interfaces.ts +++ b/modules/angular2/src/core/compiler/interfaces.ts @@ -8,10 +8,7 @@ import {StringMap} from 'angular2/src/core/facade/collection'; * - `AfterContentInit`, * - `AfterContentChecked`, * - `OnDestroy` (at the very end before destruction) - * - * // todo(vicb): describe Dart & TS vs JS */ -export interface LifecycleHook {} /** * Notify a directive when any of its bindings have changed. @@ -41,7 +38,7 @@ export interface LifecycleHook {} * } * ``` */ -export class OnChanges implements LifecycleHook { +export class OnChanges { onChanges(changes: StringMap): void {} } @@ -63,7 +60,7 @@ export class OnChanges implements LifecycleHook { * } * ``` */ -export class OnInit implements LifecycleHook { +export class OnInit { onInit(): void {} } @@ -86,7 +83,7 @@ export class OnInit implements LifecycleHook { * } * ``` */ -export class DoCheck implements LifecycleHook { +export class DoCheck { doCheck(): void {} } @@ -104,7 +101,7 @@ export class DoCheck implements LifecycleHook { * } * ``` */ -export class OnDestroy implements LifecycleHook { +export class OnDestroy { onDestroy(): void {} } @@ -122,7 +119,7 @@ export class OnDestroy implements LifecycleHook { * } * ``` */ -export class AfterContentInit implements LifecycleHook { +export class AfterContentInit { afterContentInit(): void {} } @@ -140,7 +137,7 @@ export class AfterContentInit implements LifecycleHook { * } * ``` */ -export class AfterContentChecked implements LifecycleHook { +export class AfterContentChecked { afterContentChecked(): void {} } @@ -158,7 +155,7 @@ export class AfterContentChecked implements LifecycleHook { * } * ``` */ -export class AfterViewInit implements LifecycleHook { +export class AfterViewInit { afterViewInit(): void {} } @@ -176,6 +173,6 @@ export class AfterViewInit implements LifecycleHook { * } * ``` */ -export class AfterViewChecked implements LifecycleHook { +export class AfterViewChecked { afterViewChecked(): void {} } diff --git a/modules/angular2/src/core/directives/ng_class.ts b/modules/angular2/src/core/directives/ng_class.ts index 8c8bdf71b7..2a5fe65ec5 100644 --- a/modules/angular2/src/core/directives/ng_class.ts +++ b/modules/angular2/src/core/directives/ng_class.ts @@ -1,5 +1,6 @@ import {isPresent, isString, StringWrapper, isBlank} from 'angular2/src/core/facade/lang'; -import {Directive, DoCheck, OnDestroy} from 'angular2/metadata'; +import {Directive} from 'angular2/metadata'; +import {DoCheck, OnDestroy} from 'angular2/lifecycle_hooks'; import {ElementRef} from 'angular2/core'; import {Renderer} from 'angular2/src/core/render/api'; import { diff --git a/modules/angular2/src/core/directives/ng_for.ts b/modules/angular2/src/core/directives/ng_for.ts index 5bd763b08d..a2b74c638e 100644 --- a/modules/angular2/src/core/directives/ng_for.ts +++ b/modules/angular2/src/core/directives/ng_for.ts @@ -1,4 +1,5 @@ -import {Directive, DoCheck} from 'angular2/metadata'; +import {Directive} from 'angular2/metadata'; +import {DoCheck} from 'angular2/lifecycle_hooks'; import {ViewContainerRef, ViewRef, TemplateRef} from 'angular2/core'; import {ChangeDetectorRef, IterableDiffer, IterableDiffers} from 'angular2/change_detection'; import {isPresent, isBlank} from 'angular2/src/core/facade/lang'; diff --git a/modules/angular2/src/core/directives/ng_style.ts b/modules/angular2/src/core/directives/ng_style.ts index 75ddfafa5b..97d7ed9fb6 100644 --- a/modules/angular2/src/core/directives/ng_style.ts +++ b/modules/angular2/src/core/directives/ng_style.ts @@ -1,4 +1,5 @@ -import {Directive, DoCheck} from 'angular2/metadata'; +import {Directive} from 'angular2/metadata'; +import {DoCheck} from 'angular2/lifecycle_hooks'; import {ElementRef} from 'angular2/core'; import {KeyValueDiffer, KeyValueDiffers} from 'angular2/change_detection'; import {isPresent, isBlank, print} from 'angular2/src/core/facade/lang'; diff --git a/modules/angular2/src/core/metadata/directives.ts b/modules/angular2/src/core/metadata/directives.ts index 5f1f330ed7..4cbdb171df 100644 --- a/modules/angular2/src/core/metadata/directives.ts +++ b/modules/angular2/src/core/metadata/directives.ts @@ -318,6 +318,10 @@ import {ChangeDetectionStrategy} from 'angular2/change_detection'; * the directive * controller is correctly instantiated on the `