refactor(change_detection): removed NO_CHANGED and replaced it with WrappedValue
This commit is contained in:
@ -156,7 +156,8 @@ if (${pipe} === ${UTIL}.unitialized()) {
|
||||
}
|
||||
|
||||
${newValue} = ${pipe}.transform(${context});
|
||||
if (! ${UTIL}.noChangeMarker(${newValue})) {
|
||||
if (${oldValue} !== ${newValue}) {
|
||||
${newValue} = ${UTIL}.unwrapValue(${newValue});
|
||||
${change} = true;
|
||||
${update}
|
||||
${addToChanges}
|
||||
|
@ -2,7 +2,7 @@ import {isPresent, isBlank, BaseException, Type} from 'angular2/src/facade/lang'
|
||||
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {ProtoRecord} from './proto_record';
|
||||
import {ExpressionChangedAfterItHasBeenChecked} from './exceptions';
|
||||
import {NO_CHANGE} from './pipes/pipe';
|
||||
import {WrappedValue} from './pipes/pipe';
|
||||
import {CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED, ON_PUSH} from './constants';
|
||||
|
||||
export var uninitialized = new Object();
|
||||
@ -109,8 +109,12 @@ export class ChangeDetectionUtil {
|
||||
return obj[args[0]];
|
||||
}
|
||||
|
||||
static noChangeMarker(value):boolean {
|
||||
return value === NO_CHANGE;
|
||||
static unwrapValue(value:any):any {
|
||||
if (value instanceof WrappedValue) {
|
||||
return value.wrapped;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
static throwOnChange(proto:ProtoRecord, change) {
|
||||
|
@ -233,11 +233,13 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||
_pipeCheck(proto:ProtoRecord) {
|
||||
var context = this._readContext(proto);
|
||||
var pipe = this._pipeFor(proto, context);
|
||||
var prevValue = this._readSelf(proto);
|
||||
|
||||
var newValue = pipe.transform(context);
|
||||
|
||||
if (! ChangeDetectionUtil.noChangeMarker(newValue)) {
|
||||
var prevValue = this._readSelf(proto);
|
||||
if (!isSame(prevValue, newValue)) {
|
||||
newValue = ChangeDetectionUtil.unwrapValue(newValue);
|
||||
|
||||
this._writeSelf(proto, newValue);
|
||||
this._setChanged(proto, true);
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import {Observable, ObservableWrapper} from 'angular2/src/facade/async';
|
||||
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
||||
import {Pipe, NO_CHANGE} from './pipe';
|
||||
import {Pipe, WrappedValue} from './pipe';
|
||||
import {ChangeDetectorRef} from '../change_detector_ref';
|
||||
|
||||
/**
|
||||
@ -67,10 +67,10 @@ export class AsyncPipe extends Pipe {
|
||||
}
|
||||
|
||||
if (this._latestValue === this._latestReturnedValue) {
|
||||
return NO_CHANGE;
|
||||
return this._latestReturnedValue;
|
||||
} else {
|
||||
this._latestReturnedValue = this._latestValue;
|
||||
return this._latestValue;
|
||||
return WrappedValue.wrap(this._latestValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ import {
|
||||
looseIdentical,
|
||||
} from 'angular2/src/facade/lang';
|
||||
|
||||
import {NO_CHANGE, Pipe} from './pipe';
|
||||
import {WrappedValue, Pipe} from './pipe';
|
||||
|
||||
export class IterableChangesFactory {
|
||||
supports(obj):boolean {
|
||||
@ -117,9 +117,9 @@ export class IterableChanges extends Pipe {
|
||||
|
||||
transform(collection){
|
||||
if (this.check(collection)) {
|
||||
return this;
|
||||
return WrappedValue.wrap(this);
|
||||
} else {
|
||||
return NO_CHANGE;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import {ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
import {stringify, looseIdentical, isJsObject} from 'angular2/src/facade/lang';
|
||||
|
||||
import {NO_CHANGE, Pipe} from './pipe';
|
||||
import {WrappedValue, Pipe} from './pipe';
|
||||
|
||||
/**
|
||||
* @exportedAs angular2/pipes
|
||||
@ -54,9 +54,9 @@ export class KeyValueChanges extends Pipe {
|
||||
|
||||
transform(map){
|
||||
if (this.check(map)) {
|
||||
return this;
|
||||
return WrappedValue.wrap(this);
|
||||
} else {
|
||||
return NO_CHANGE;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import {isBlank} from 'angular2/src/facade/lang';
|
||||
import {Pipe, NO_CHANGE} from './pipe';
|
||||
import {Pipe, WrappedValue} from './pipe';
|
||||
|
||||
/**
|
||||
* @exportedAs angular2/pipes
|
||||
@ -35,9 +35,9 @@ export class NullPipe extends Pipe {
|
||||
transform(value) {
|
||||
if (! this.called) {
|
||||
this.called = true;
|
||||
return null;
|
||||
return WrappedValue.wrap(null);
|
||||
} else {
|
||||
return NO_CHANGE;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,33 @@
|
||||
/**
|
||||
* Indicates that the result of a {@link Pipe} transformation has not changed since the last time the pipe was called.
|
||||
* Indicates that the result of a {@link Pipe} transformation has changed even though the reference has not changed.
|
||||
*
|
||||
* Suppose we have a pipe that computes changes in an array by performing a simple diff operation. If
|
||||
* we call this pipe with the same array twice, it will return `NO_CHANGE` the second time.
|
||||
* The wrapped value will be unwrapped by change detection, and the unwrapped value will be stored.
|
||||
*
|
||||
* @exportedAs angular2/pipes
|
||||
*/
|
||||
export class WrappedValue {
|
||||
wrapped:any;
|
||||
|
||||
export var NO_CHANGE = new Object();
|
||||
constructor(wrapped:any) {
|
||||
this.wrapped = wrapped;
|
||||
}
|
||||
|
||||
static wrap(value:any):WrappedValue {
|
||||
var w = _wrappedValues[_wrappedIndex++ % 5];
|
||||
w.wrapped = value;
|
||||
return w;
|
||||
}
|
||||
}
|
||||
|
||||
var _wrappedValues = [
|
||||
new WrappedValue(null),
|
||||
new WrappedValue(null),
|
||||
new WrappedValue(null),
|
||||
new WrappedValue(null),
|
||||
new WrappedValue(null)
|
||||
];
|
||||
|
||||
var _wrappedIndex = 0;
|
||||
|
||||
/**
|
||||
* An interface for extending the list of pipes known to Angular.
|
||||
|
Reference in New Issue
Block a user