Skip to content

Commit

Permalink
d68cf38 refactor(pipes): use angular lifecycle hooks instead of PipeO…
Browse files Browse the repository at this point in the history
…nDestroy
  • Loading branch information
jeffbcross committed Dec 1, 2015
1 parent 58b96c4 commit a6aa3e9
Show file tree
Hide file tree
Showing 52 changed files with 618 additions and 609 deletions.
4 changes: 2 additions & 2 deletions BUILD_INFO
@@ -1,2 +1,2 @@
Tue Dec 1 00:25:22 UTC 2015
f0b8ce1291154ab673f694a71d94c44025e2d347
Tue Dec 1 00:37:39 UTC 2015
d68cf380fda403cb97a069a7e035666af84831b0
22 changes: 11 additions & 11 deletions docs/cheatsheet/lifecycle hooks.md
Expand Up @@ -10,40 +10,40 @@ The class constructor is called before any other lifecycle hook. Use it to injec


@cheatsheetItem
`onChanges(changeRecord) { ... }`|`onChanges(changeRecord)`
`ngOnChanges(changeRecord) { ... }`|`ngOnChanges(changeRecord)`
Called after every change to input properties and before processing content or child views.


@cheatsheetItem
`onInit() { ... }`|`onInit()`
Called after the constructor, initializing input properties, and the first call to onChanges.
`ngOnInit() { ... }`|`ngOnInit()`
Called after the constructor, initializing input properties, and the first call to ngOnChanges.


@cheatsheetItem
`doCheck() { ... }`|`doCheck()`
`ngDoCheck() { ... }`|`ngDoCheck()`
Called every time that the input properties of a component or a directive are checked. Use it to extend change detection by performing a custom check.


@cheatsheetItem
`afterContentInit() { ... }`|`afterContentInit()`
Called after onInit when the component's or directive's content has been initialized.
`ngAfterContentInit() { ... }`|`ngAfterContentInit()`
Called after ngOnInit when the component's or directive's content has been initialized.


@cheatsheetItem
`afterContentChecked() { ... }`|`afterContentChecked()`
`ngAfterContentChecked() { ... }`|`ngAfterContentChecked()`
Called after every check of the component's or directive's content.


@cheatsheetItem
`afterViewInit() { ... }`|`afterViewInit()`
Called after afterContentInit when the component's view has been initialized. Applies to components only.
`ngAfterViewInit() { ... }`|`ngAfterViewInit()`
Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.


@cheatsheetItem
`afterViewChecked() { ... }`|`afterViewChecked()`
`ngAfterViewChecked() { ... }`|`ngAfterViewChecked()`
Called after every check of the component's view. Applies to components only.


@cheatsheetItem
`onDestroy() { ... }`|`onDestroy()`
`ngOnDestroy() { ... }`|`ngOnDestroy()`
Called once, before the instance is destroyed.
18 changes: 9 additions & 9 deletions docs/cheatsheet/routing.md
Expand Up @@ -31,25 +31,25 @@ A component decorator defining a function that the router should call first to d


@cheatsheetItem
`onActivate(nextInstruction, prevInstruction) { ... }`|`onActivate`
After navigating to a component, the router calls component's onActivate method (if defined).
`routerOnActivate(nextInstruction, prevInstruction) { ... }`|`routerOnActivate`
After navigating to a component, the router calls component's routerOnActivate method (if defined).


@cheatsheetItem
`canReuse(nextInstruction, prevInstruction) { ... }`|`canReuse`
The router calls a component's canReuse method (if defined) to determine whether to reuse the instance or destroy it and create a new instance. Should return a boolean or a promise.
`routerCanReuse(nextInstruction, prevInstruction) { ... }`|`routerCanReuse`
The router calls a component's routerCanReuse method (if defined) to determine whether to reuse the instance or destroy it and create a new instance. Should return a boolean or a promise.


@cheatsheetItem
`onReuse(nextInstruction, prevInstruction) { ... }`|`onReuse`
The router calls the component's onReuse method (if defined) when it re-uses a component instance.
`routerOnReuse(nextInstruction, prevInstruction) { ... }`|`routerOnReuse`
The router calls the component's routerOnReuse method (if defined) when it re-uses a component instance.


@cheatsheetItem
`canDeactivate(nextInstruction, prevInstruction) { ... }`|`canDeactivate`
The router calls the canDeactivate methods (if defined) of every component that would be removed after a navigation. The navigation proceeds if and only if all such methods return true or a promise that is resolved.
`routerCanDeactivate(nextInstruction, prevInstruction) { ... }`|`routerCanDeactivate`
The router calls the routerCanDeactivate methods (if defined) of every component that would be removed after a navigation. The navigation proceeds if and only if all such methods return true or a promise that is resolved.


@cheatsheetItem
`onDeactivate(nextInstruction, prevInstruction) { ... }`|`onDeactivate`
`routerOnDeactivate(nextInstruction, prevInstruction) { ... }`|`routerOnDeactivate`
Called before the directive is removed as the result of a route change. May return a promise that pauses removing the directive until the promise resolves.
4 changes: 2 additions & 2 deletions lib/src/common/directives/ng_class.dart
Expand Up @@ -114,7 +114,7 @@ class NgClass implements DoCheck, OnDestroy {
}
}

void doCheck() {
void ngDoCheck() {
if (isPresent(this._differ)) {
var changes = this._differ.diff(this._rawClass);
if (isPresent(changes)) {
Expand All @@ -127,7 +127,7 @@ class NgClass implements DoCheck, OnDestroy {
}
}

void onDestroy() {
void ngOnDestroy() {
this._cleanupClasses(this._rawClass);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/directives/ng_for.dart
Expand Up @@ -86,7 +86,7 @@ class NgFor implements DoCheck {
}
}

doCheck() {
ngDoCheck() {
if (isPresent(this._differ)) {
var changes = this._differ.diff(this._ngForOf);
if (isPresent(changes)) this._applyChanges(changes);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/directives/ng_style.dart
Expand Up @@ -77,7 +77,7 @@ class NgStyle implements DoCheck {
}
}

doCheck() {
ngDoCheck() {
if (isPresent(this._differ)) {
var changes = this._differ.diff(this._rawStyle);
if (isPresent(changes)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/common/forms/directives/ng_control_group.dart
Expand Up @@ -87,11 +87,11 @@ class NgControlGroup extends ControlContainer implements OnInit, OnDestroy {
/* super call moved to initializer */;
this._parent = parent;
}
void onInit() {
void ngOnInit() {
this.formDirective.addControlGroup(this);
}

void onDestroy() {
void ngOnDestroy() {
this.formDirective.removeControlGroup(this);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/common/forms/directives/ng_control_name.dart
Expand Up @@ -113,7 +113,7 @@ class NgControlName extends NgControl implements OnChanges, OnDestroy {
/* super call moved to initializer */;
this.valueAccessor = selectValueAccessor(this, valueAccessors);
}
onChanges(Map<String, SimpleChange> changes) {
ngOnChanges(Map<String, SimpleChange> changes) {
if (!this._added) {
this.formDirective.addControl(this);
this._added = true;
Expand All @@ -124,7 +124,7 @@ class NgControlName extends NgControl implements OnChanges, OnDestroy {
}
}

void onDestroy() {
void ngOnDestroy() {
this.formDirective.removeControl(this);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/forms/directives/ng_form_control.dart
Expand Up @@ -100,7 +100,7 @@ class NgFormControl extends NgControl implements OnChanges {
/* super call moved to initializer */;
this.valueAccessor = selectValueAccessor(this, valueAccessors);
}
void onChanges(Map<String, SimpleChange> changes) {
void ngOnChanges(Map<String, SimpleChange> changes) {
if (this._isControlChanged(changes)) {
setUpControl(this.form, this);
this.form.updateValueAndValidity(emitEvent: false);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/forms/directives/ng_form_model.dart
Expand Up @@ -112,7 +112,7 @@ class NgFormModel extends ControlContainer implements Form, OnChanges {
: super() {
/* super call moved to initializer */;
}
void onChanges(Map<String, SimpleChange> changes) {
void ngOnChanges(Map<String, SimpleChange> changes) {
if (StringMapWrapper.contains(changes, "form")) {
var sync = composeValidators(this._validators);
this.form.validator = Validators.compose([this.form.validator, sync]);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/forms/directives/ng_model.dart
Expand Up @@ -75,7 +75,7 @@ class NgModel extends NgControl implements OnChanges {
/* super call moved to initializer */;
this.valueAccessor = selectValueAccessor(this, valueAccessors);
}
onChanges(Map<String, SimpleChange> changes) {
ngOnChanges(Map<String, SimpleChange> changes) {
if (!this._added) {
setUpControl(this._control, this);
this._control.updateValueAndValidity(emitEvent: false);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/common/pipes/async_pipe.dart
Expand Up @@ -9,7 +9,7 @@ import "package:angular2/core.dart"
Pipe,
Injectable,
ChangeDetectorRef,
PipeOnDestroy,
OnDestroy,
PipeTransform,
WrappedValue;
import "invalid_pipe_argument_exception.dart" show InvalidPipeArgumentException;
Expand Down Expand Up @@ -62,7 +62,7 @@ var _observableStrategy = new ObservableStrategy();
*/
@Pipe(name: "async", pure: false)
@Injectable()
class AsyncPipe implements PipeTransform, PipeOnDestroy {
class AsyncPipe implements PipeTransform, OnDestroy {
/** @internal */
Object _latestValue = null;
/** @internal */
Expand All @@ -78,7 +78,7 @@ class AsyncPipe implements PipeTransform, PipeOnDestroy {
AsyncPipe(ChangeDetectorRef _ref) {
this._ref = _ref;
}
void onDestroy() {
void ngOnDestroy() {
if (isPresent(this._subscription)) {
this._dispose();
}
Expand Down
1 change: 0 additions & 1 deletion lib/src/core/change_detection.dart
Expand Up @@ -14,7 +14,6 @@ export "change_detection/change_detection.dart"
WrappedValue,
SimpleChange,
PipeTransform,
PipeOnDestroy,
IterableDiffers,
IterableDiffer,
IterableDifferFactory,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/change_detection/change_detection.dart
Expand Up @@ -43,7 +43,7 @@ export "differs/iterable_differs.dart"
show IterableDiffers, IterableDiffer, IterableDifferFactory;
export "differs/keyvalue_differs.dart"
show KeyValueDiffers, KeyValueDiffer, KeyValueDifferFactory;
export "pipe_transform.dart" show PipeTransform, PipeOnDestroy;
export "pipe_transform.dart" show PipeTransform;
export "change_detection_util.dart" show WrappedValue, SimpleChange;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/change_detection/change_detection_util.dart
Expand Up @@ -269,7 +269,7 @@ class ChangeDetectionUtil {

static void callPipeOnDestroy(SelectedPipe selectedPipe) {
if (implementsOnDestroy(selectedPipe.pipe)) {
((selectedPipe.pipe as dynamic)).onDestroy();
((selectedPipe.pipe as dynamic)).ngOnDestroy();
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/core/change_detection/codegen_logic_util.dart
Expand Up @@ -192,11 +192,11 @@ class CodegenLogicUtil {
var dir = directiveRecords[i];
if (dir.callAfterContentInit) {
res.add(
'''if(${ this . _names . getStateName ( )} ${ eq} ${ this . _changeDetectorStateName}.NeverChecked) ${ this . _names . getDirectiveName ( dir . directiveIndex )}.afterContentInit();''');
'''if(${ this . _names . getStateName ( )} ${ eq} ${ this . _changeDetectorStateName}.NeverChecked) ${ this . _names . getDirectiveName ( dir . directiveIndex )}.ngAfterContentInit();''');
}
if (dir.callAfterContentChecked) {
res.add(
'''${ this . _names . getDirectiveName ( dir . directiveIndex )}.afterContentChecked();''');
'''${ this . _names . getDirectiveName ( dir . directiveIndex )}.ngAfterContentChecked();''');
}
}
return res;
Expand All @@ -211,11 +211,11 @@ class CodegenLogicUtil {
var dir = directiveRecords[i];
if (dir.callAfterViewInit) {
res.add(
'''if(${ this . _names . getStateName ( )} ${ eq} ${ this . _changeDetectorStateName}.NeverChecked) ${ this . _names . getDirectiveName ( dir . directiveIndex )}.afterViewInit();''');
'''if(${ this . _names . getStateName ( )} ${ eq} ${ this . _changeDetectorStateName}.NeverChecked) ${ this . _names . getDirectiveName ( dir . directiveIndex )}.ngAfterViewInit();''');
}
if (dir.callAfterViewChecked) {
res.add(
'''${ this . _names . getDirectiveName ( dir . directiveIndex )}.afterViewChecked();''');
'''${ this . _names . getDirectiveName ( dir . directiveIndex )}.ngAfterViewChecked();''');
}
}
return res;
Expand Down
14 changes: 7 additions & 7 deletions lib/src/core/change_detection/dynamic_change_detector.dart
Expand Up @@ -160,17 +160,17 @@ class DynamicChangeDetector extends AbstractChangeDetector<dynamic> {
}
if (proto.isLifeCycleRecord()) {
if (identical(proto.name, "DoCheck") && !throwOnChange) {
this._getDirectiveFor(directiveRecord.directiveIndex).doCheck();
this._getDirectiveFor(directiveRecord.directiveIndex).ngDoCheck();
} else if (identical(proto.name, "OnInit") &&
!throwOnChange &&
this.state == ChangeDetectorState.NeverChecked) {
this._getDirectiveFor(directiveRecord.directiveIndex).onInit();
this._getDirectiveFor(directiveRecord.directiveIndex).ngOnInit();
} else if (identical(proto.name, "OnChanges") &&
isPresent(changes) &&
!throwOnChange) {
this
._getDirectiveFor(directiveRecord.directiveIndex)
.onChanges(changes);
.ngOnChanges(changes);
}
} else if (proto.isSkipRecord()) {
protoIdx += this._computeSkipLength(protoIdx, proto, this.values);
Expand Down Expand Up @@ -207,10 +207,10 @@ class DynamicChangeDetector extends AbstractChangeDetector<dynamic> {
var dir = dirs[i];
if (dir.callAfterContentInit &&
this.state == ChangeDetectorState.NeverChecked) {
this._getDirectiveFor(dir.directiveIndex).afterContentInit();
this._getDirectiveFor(dir.directiveIndex).ngAfterContentInit();
}
if (dir.callAfterContentChecked) {
this._getDirectiveFor(dir.directiveIndex).afterContentChecked();
this._getDirectiveFor(dir.directiveIndex).ngAfterContentChecked();
}
}
}
Expand All @@ -221,10 +221,10 @@ class DynamicChangeDetector extends AbstractChangeDetector<dynamic> {
var dir = dirs[i];
if (dir.callAfterViewInit &&
this.state == ChangeDetectorState.NeverChecked) {
this._getDirectiveFor(dir.directiveIndex).afterViewInit();
this._getDirectiveFor(dir.directiveIndex).ngAfterViewInit();
}
if (dir.callAfterViewChecked) {
this._getDirectiveFor(dir.directiveIndex).afterViewChecked();
this._getDirectiveFor(dir.directiveIndex).ngAfterViewChecked();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/change_detection/pipe_lifecycle_reflector.dart
@@ -1,5 +1,5 @@
library angular2.core.compiler.pipe_lifecycle_reflector;

import 'package:angular2/src/core/change_detection/pipe_transform.dart';
import 'package:angular2/src/core/linker/interfaces.dart';

bool implementsOnDestroy(Object pipe) => pipe is PipeOnDestroy;
bool implementsOnDestroy(Object pipe) => pipe is OnDestroy;
54 changes: 0 additions & 54 deletions lib/src/core/change_detection/pipe_transform.dart
Expand Up @@ -35,57 +35,3 @@ library angular2.src.core.change_detection.pipe_transform;
abstract class PipeTransform {
dynamic transform(dynamic value, List<dynamic> args);
}

/**
* To create a stateful Pipe, you should implement this interface and set the `pure`
* parameter to `false` in the [PipeMetadata].
*
* A stateful pipe may produce different output, given the same input. It is
* likely that a stateful pipe may contain state that should be cleaned up when
* a binding is destroyed. For example, a subscription to a stream of data may need to
* be disposed, or an interval may need to be cleared.
*
* ### Example ([live demo](http://plnkr.co/edit/i8pm5brO4sPaLxBx56MR?p=preview))
*
* In this example, a pipe is created to countdown its input value, updating it every
* 50ms. Because it maintains an internal interval, it automatically clears
* the interval when the binding is destroyed or the countdown completes.
*
* ```
* import {Pipe, PipeTransform} from 'angular2/angular2'
* @Pipe({name: 'countdown', pure: false})
* class CountDown implements PipeTransform, PipeOnDestroy {
* remainingTime:Number;
* interval:SetInterval;
* onDestroy() {
* if (this.interval) {
* clearInterval(this.interval);
* }
* }
* transform(value: any, args: any[] = []) {
* if (!parseInt(value, 10)) return null;
* if (typeof this.remainingTime !== 'number') {
* this.remainingTime = parseInt(value, 10);
* }
* if (!this.interval) {
* this.interval = setInterval(() => {
* this.remainingTime-=50;
* if (this.remainingTime <= 0) {
* this.remainingTime = 0;
* clearInterval(this.interval);
* delete this.interval;
* }
* }, 50);
* }
* return this.remainingTime;
* }
* }
* ```
*
* Invoking `{{ 10000 | countdown }}` would cause the value to be decremented by 50,
* every 50ms, until it reaches 0.
*
*/
abstract class PipeOnDestroy {
void onDestroy();
}

0 comments on commit a6aa3e9

Please sign in to comment.