refactor(core): refactor WrappedValue (#20997)

- Improve `WrappedValue` by adding `unwrap` symetrical to `wrap`.
- remove dead code - `ValueUnwrapper`

The property `wrapped` is an implementation details and should never be accessed
directly - use `unwrap(wrappedValue)`. Will change to protected in Angular 7.

PR Close #20997
This commit is contained in:
Victor Berchet
2017-12-13 15:54:43 -08:00
committed by Alex Eagle
parent 54bf179888
commit 3bcc0e6f76
6 changed files with 26 additions and 34 deletions

View File

@ -12,7 +12,7 @@ import {IterableDifferFactory, IterableDiffers} from './differs/iterable_differs
import {KeyValueDifferFactory, KeyValueDiffers} from './differs/keyvalue_differs';
export {SimpleChanges} from '../metadata/lifecycle_hooks';
export {SimpleChange, ValueUnwrapper, WrappedValue, devModeEqual} from './change_detection_util';
export {SimpleChange, WrappedValue, devModeEqual} from './change_detection_util';
export {ChangeDetectorRef} from './change_detector_ref';
export {ChangeDetectionStrategy, ChangeDetectorStatus, isDefaultChangeDetectionStrategy} from './constants';
export {DefaultIterableDifferFactory} from './differs/default_iterable_differ';

View File

@ -26,10 +26,10 @@ export function devModeEqual(a: any, b: any): boolean {
/**
* Indicates that the result of a {@link Pipe} transformation has changed even though the
* reference
* has not changed.
* reference has not changed.
*
* The wrapped value will be unwrapped by change detection, and the unwrapped value will be stored.
* Wrapped values are unwrapped automatically during the change detection, and the unwrapped value
* is stored.
*
* Example:
*
@ -44,26 +44,22 @@ export function devModeEqual(a: any, b: any): boolean {
* @stable
*/
export class WrappedValue {
constructor(public wrapped: any) {}
/** @deprecated from 5.3, use `unwrap()` instead - will switch to protected */
wrapped: any;
constructor(value: any) { this.wrapped = value; }
/** Creates a wrapped value. */
static wrap(value: any): WrappedValue { return new WrappedValue(value); }
}
/**
* Helper class for unwrapping WrappedValue s
*/
export class ValueUnwrapper {
public hasWrappedValue = false;
/**
* Returns the underlying value of a wrapped value.
* Returns the given `value` when it is not wrapped.
**/
static unwrap(value: any): any { return WrappedValue.isWrapped(value) ? value.wrapped : value; }
unwrap(value: any): any {
if (value instanceof WrappedValue) {
this.hasWrappedValue = true;
return value.wrapped;
}
return value;
}
reset() { this.hasWrappedValue = false; }
/** Returns true if `value` is a wrapped value. */
static isWrapped(value: any): value is WrappedValue { return value instanceof WrappedValue; }
}
/**