From 3c1dd326f642577b2c48307c5e7d3fa1d3c28096 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 11 Sep 2015 14:23:24 -0700 Subject: [PATCH 1/3] 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 b028ef8b2e71..bd5e3992ce36 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 c79bfeb8b2c4..4dbd1800bd0f 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 adfec2167c9c..aef765fe232c 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 7961cceb6d2c..1a46d1a1392f 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(); } From c0d5fcc62a681e867f69aeef6a8b3a8d2fd46a84 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 11 Sep 2015 14:51:43 -0700 Subject: [PATCH 2/3] chore(docs): typo --- modules/angular2/src/test_lib/test_lib.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/angular2/src/test_lib/test_lib.ts b/modules/angular2/src/test_lib/test_lib.ts index 2d60b23a1455..8b0e328a2baf 100644 --- a/modules/angular2/src/test_lib/test_lib.ts +++ b/modules/angular2/src/test_lib/test_lib.ts @@ -367,7 +367,7 @@ export class SpyObject { } } } - // Noop so that SpyObject has the smae interface as in Dart + // Noop so that SpyObject has the same interface as in Dart noSuchMethod(args) {} spy(name) { From f340b9abc703563d45684c160240742ab68b9c4a Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 11 Sep 2015 14:53:49 -0700 Subject: [PATCH 3/3] feat(change_detection): allow triggering CD form ChangeDetectorRef --- .../change_detection/change_detector_ref.ts | 2 ++ .../change_detector_ref_spec.ts | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 modules/angular2/test/core/change_detection/change_detector_ref_spec.ts diff --git a/modules/angular2/src/core/change_detection/change_detector_ref.ts b/modules/angular2/src/core/change_detection/change_detector_ref.ts index 8e5bf2f17a9f..40bfbaf71082 100644 --- a/modules/angular2/src/core/change_detection/change_detector_ref.ts +++ b/modules/angular2/src/core/change_detection/change_detector_ref.ts @@ -25,6 +25,8 @@ export class ChangeDetectorRef { */ detach(): void { this._cd.mode = ChangeDetectionStrategy.Detached; } + detectChanges(): void { this._cd.detectChanges(); } + /** * Reattach the change detector to the change detector tree. * diff --git a/modules/angular2/test/core/change_detection/change_detector_ref_spec.ts b/modules/angular2/test/core/change_detection/change_detector_ref_spec.ts new file mode 100644 index 000000000000..681e7d9fb10f --- /dev/null +++ b/modules/angular2/test/core/change_detection/change_detector_ref_spec.ts @@ -0,0 +1,29 @@ +import { + ddescribe, + describe, + it, + iit, + xit, + expect, + beforeEach, + afterEach, + tick, + fakeAsync +} from 'angular2/test_lib'; + +import {ChangeDetectorRef} from 'angular2/src/core/change_detection/change_detector_ref'; +import {SpyChangeDetector} from 'angular2/test/core/spies'; +import {ChangeDetector} from 'angular2/src/core/change_detection/interfaces'; + + +export function main() { + describe('ChangeDetectorRef', () => { + it('should delegate detectChanges()', () => { + var changeDetector = new SpyChangeDetector(); + changeDetector.spy('detectChanges'); + var changeDetectorRef = new ChangeDetectorRef(changeDetector); + changeDetectorRef.detectChanges(); + expect(changeDetector.spy('detectChanges')).toHaveBeenCalledWith(); + }); + }); +}