From 3bcc0e6f765f86ce96dbb33edf4ba0cbc53b3322 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 13 Dec 2017 15:54:43 -0800 Subject: [PATCH] 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 --- .../src/change_detection/change_detection.ts | 2 +- .../change_detection/change_detection_util.ts | 34 ++++++++----------- packages/core/src/core_private_export.ts | 2 +- packages/core/src/view/provider.ts | 5 +-- packages/core/src/view/util.ts | 11 +++--- tools/public_api_guard/core/core.d.ts | 6 ++-- 6 files changed, 26 insertions(+), 34 deletions(-) diff --git a/packages/core/src/change_detection/change_detection.ts b/packages/core/src/change_detection/change_detection.ts index 5ad5ce00ad..f8dc1f18ac 100644 --- a/packages/core/src/change_detection/change_detection.ts +++ b/packages/core/src/change_detection/change_detection.ts @@ -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'; diff --git a/packages/core/src/change_detection/change_detection_util.ts b/packages/core/src/change_detection/change_detection_util.ts index 9f68ba4f7e..5384664e4b 100644 --- a/packages/core/src/change_detection/change_detection_util.ts +++ b/packages/core/src/change_detection/change_detection_util.ts @@ -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; } } /** diff --git a/packages/core/src/core_private_export.ts b/packages/core/src/core_private_export.ts index bf1aace410..bf0a7dbe54 100644 --- a/packages/core/src/core_private_export.ts +++ b/packages/core/src/core_private_export.ts @@ -8,7 +8,7 @@ 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 {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 {ChangeDetectorStatus as ɵChangeDetectorStatus, isDefaultChangeDetectionStrategy as ɵisDefaultChangeDetectionStrategy} from './change_detection/constants'; export {Console as ɵConsole} from './console'; diff --git a/packages/core/src/view/provider.ts b/packages/core/src/view/provider.ts index 65371c9d19..41a3605c03 100644 --- a/packages/core/src/view/provider.ts +++ b/packages/core/src/view/provider.ts @@ -437,10 +437,7 @@ function updateProp( providerData.instance[propName] = value; if (def.flags & NodeFlags.OnChanges) { changes = changes || {}; - let oldValue = view.oldValues[def.bindingIndex + bindingIdx]; - if (oldValue instanceof WrappedValue) { - oldValue = oldValue.wrapped; - } + const oldValue = WrappedValue.unwrap(view.oldValues[def.bindingIndex + bindingIdx]); const binding = def.bindings[bindingIdx]; changes[binding.nonMinifiedName !] = new SimpleChange(oldValue, value, (view.state & ViewState.FirstCheck) !== 0); diff --git a/packages/core/src/view/util.ts b/packages/core/src/view/util.ts index 832f05cdbe..dae387cb9f 100644 --- a/packages/core/src/view/util.ts +++ b/packages/core/src/view/util.ts @@ -28,13 +28,10 @@ export function tokenKey(token: any): string { } export function unwrapValue(view: ViewData, nodeIdx: number, bindingIdx: number, value: any): any { - if (value instanceof WrappedValue) { - value = value.wrapped; - let globalBindingIdx = view.def.nodes[nodeIdx].bindingIndex + bindingIdx; - let oldValue = view.oldValues[globalBindingIdx]; - if (oldValue instanceof WrappedValue) { - oldValue = oldValue.wrapped; - } + if (WrappedValue.isWrapped(value)) { + value = WrappedValue.unwrap(value); + const globalBindingIdx = view.def.nodes[nodeIdx].bindingIndex + bindingIdx; + const oldValue = WrappedValue.unwrap(view.oldValues[globalBindingIdx]); view.oldValues[globalBindingIdx] = new WrappedValue(oldValue); } return value; diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index b4edf322f4..160ad71095 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -1093,8 +1093,10 @@ export declare abstract class ViewRef extends ChangeDetectorRef { /** @stable */ export declare class WrappedValue { - wrapped: any; - constructor(wrapped: any); + /** @deprecated */ wrapped: any; + constructor(value: any); + static isWrapped(value: any): value is WrappedValue; + static unwrap(value: any): any; static wrap(value: any): WrappedValue; }