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:
parent
75f8522b8d
commit
d3333f04ba
@ -12,7 +12,7 @@ import {IterableDifferFactory, IterableDiffers} from './differs/iterable_differs
|
|||||||
import {KeyValueDifferFactory, KeyValueDiffers} from './differs/keyvalue_differs';
|
import {KeyValueDifferFactory, KeyValueDiffers} from './differs/keyvalue_differs';
|
||||||
|
|
||||||
export {SimpleChanges} from '../metadata/lifecycle_hooks';
|
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 {ChangeDetectorRef} from './change_detector_ref';
|
||||||
export {ChangeDetectionStrategy, ChangeDetectorStatus, isDefaultChangeDetectionStrategy} from './constants';
|
export {ChangeDetectionStrategy, ChangeDetectorStatus, isDefaultChangeDetectionStrategy} from './constants';
|
||||||
export {DefaultIterableDifferFactory} from './differs/default_iterable_differ';
|
export {DefaultIterableDifferFactory} from './differs/default_iterable_differ';
|
||||||
|
@ -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
|
* Indicates that the result of a {@link Pipe} transformation has changed even though the
|
||||||
* reference
|
* reference has not changed.
|
||||||
* 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:
|
* Example:
|
||||||
*
|
*
|
||||||
@ -44,26 +44,22 @@ export function devModeEqual(a: any, b: any): boolean {
|
|||||||
* @stable
|
* @stable
|
||||||
*/
|
*/
|
||||||
export class WrappedValue {
|
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); }
|
static wrap(value: any): WrappedValue { return new WrappedValue(value); }
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class for unwrapping WrappedValue s
|
* Returns the underlying value of a wrapped value.
|
||||||
*/
|
* Returns the given `value` when it is not wrapped.
|
||||||
export class ValueUnwrapper {
|
**/
|
||||||
public hasWrappedValue = false;
|
static unwrap(value: any): any { return WrappedValue.isWrapped(value) ? value.wrapped : value; }
|
||||||
|
|
||||||
unwrap(value: any): any {
|
/** Returns true if `value` is a wrapped value. */
|
||||||
if (value instanceof WrappedValue) {
|
static isWrapped(value: any): value is WrappedValue { return value instanceof WrappedValue; }
|
||||||
this.hasWrappedValue = true;
|
|
||||||
return value.wrapped;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
reset() { this.hasWrappedValue = false; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS} from './application_ref';
|
export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS} from './application_ref';
|
||||||
export {APP_ID_RANDOM_PROVIDER as ɵAPP_ID_RANDOM_PROVIDER} from './application_tokens';
|
export {APP_ID_RANDOM_PROVIDER as ɵAPP_ID_RANDOM_PROVIDER} from './application_tokens';
|
||||||
export {ValueUnwrapper as ɵValueUnwrapper, devModeEqual as ɵdevModeEqual} from './change_detection/change_detection_util';
|
export {devModeEqual as ɵdevModeEqual} from './change_detection/change_detection_util';
|
||||||
export {isListLikeIterable as ɵisListLikeIterable} from './change_detection/change_detection_util';
|
export {isListLikeIterable as ɵisListLikeIterable} from './change_detection/change_detection_util';
|
||||||
export {ChangeDetectorStatus as ɵChangeDetectorStatus, isDefaultChangeDetectionStrategy as ɵisDefaultChangeDetectionStrategy} from './change_detection/constants';
|
export {ChangeDetectorStatus as ɵChangeDetectorStatus, isDefaultChangeDetectionStrategy as ɵisDefaultChangeDetectionStrategy} from './change_detection/constants';
|
||||||
export {Console as ɵConsole} from './console';
|
export {Console as ɵConsole} from './console';
|
||||||
|
@ -437,10 +437,7 @@ function updateProp(
|
|||||||
providerData.instance[propName] = value;
|
providerData.instance[propName] = value;
|
||||||
if (def.flags & NodeFlags.OnChanges) {
|
if (def.flags & NodeFlags.OnChanges) {
|
||||||
changes = changes || {};
|
changes = changes || {};
|
||||||
let oldValue = view.oldValues[def.bindingIndex + bindingIdx];
|
const oldValue = WrappedValue.unwrap(view.oldValues[def.bindingIndex + bindingIdx]);
|
||||||
if (oldValue instanceof WrappedValue) {
|
|
||||||
oldValue = oldValue.wrapped;
|
|
||||||
}
|
|
||||||
const binding = def.bindings[bindingIdx];
|
const binding = def.bindings[bindingIdx];
|
||||||
changes[binding.nonMinifiedName !] =
|
changes[binding.nonMinifiedName !] =
|
||||||
new SimpleChange(oldValue, value, (view.state & ViewState.FirstCheck) !== 0);
|
new SimpleChange(oldValue, value, (view.state & ViewState.FirstCheck) !== 0);
|
||||||
|
@ -28,13 +28,10 @@ export function tokenKey(token: any): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function unwrapValue(view: ViewData, nodeIdx: number, bindingIdx: number, value: any): any {
|
export function unwrapValue(view: ViewData, nodeIdx: number, bindingIdx: number, value: any): any {
|
||||||
if (value instanceof WrappedValue) {
|
if (WrappedValue.isWrapped(value)) {
|
||||||
value = value.wrapped;
|
value = WrappedValue.unwrap(value);
|
||||||
let globalBindingIdx = view.def.nodes[nodeIdx].bindingIndex + bindingIdx;
|
const globalBindingIdx = view.def.nodes[nodeIdx].bindingIndex + bindingIdx;
|
||||||
let oldValue = view.oldValues[globalBindingIdx];
|
const oldValue = WrappedValue.unwrap(view.oldValues[globalBindingIdx]);
|
||||||
if (oldValue instanceof WrappedValue) {
|
|
||||||
oldValue = oldValue.wrapped;
|
|
||||||
}
|
|
||||||
view.oldValues[globalBindingIdx] = new WrappedValue(oldValue);
|
view.oldValues[globalBindingIdx] = new WrappedValue(oldValue);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
|
6
tools/public_api_guard/core/core.d.ts
vendored
6
tools/public_api_guard/core/core.d.ts
vendored
@ -1093,8 +1093,10 @@ export declare abstract class ViewRef extends ChangeDetectorRef {
|
|||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
export declare class WrappedValue {
|
export declare class WrappedValue {
|
||||||
wrapped: any;
|
/** @deprecated */ wrapped: any;
|
||||||
constructor(wrapped: any);
|
constructor(value: any);
|
||||||
|
static isWrapped(value: any): value is WrappedValue;
|
||||||
|
static unwrap(value: any): any;
|
||||||
static wrap(value: any): WrappedValue;
|
static wrap(value: any): WrappedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user