Skip to content

Commit 3bcc0e6

Browse files
vicbalexeagle
authored andcommitted
refactor(core): refactor WrappedValue (angular#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 angular#20997
1 parent 54bf179 commit 3bcc0e6

File tree

6 files changed

+26
-34
lines changed

6 files changed

+26
-34
lines changed

packages/core/src/change_detection/change_detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {IterableDifferFactory, IterableDiffers} from './differs/iterable_differs
1212
import {KeyValueDifferFactory, KeyValueDiffers} from './differs/keyvalue_differs';
1313

1414
export {SimpleChanges} from '../metadata/lifecycle_hooks';
15-
export {SimpleChange, ValueUnwrapper, WrappedValue, devModeEqual} from './change_detection_util';
15+
export {SimpleChange, WrappedValue, devModeEqual} from './change_detection_util';
1616
export {ChangeDetectorRef} from './change_detector_ref';
1717
export {ChangeDetectionStrategy, ChangeDetectorStatus, isDefaultChangeDetectionStrategy} from './constants';
1818
export {DefaultIterableDifferFactory} from './differs/default_iterable_differ';

packages/core/src/change_detection/change_detection_util.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export function devModeEqual(a: any, b: any): boolean {
2626

2727
/**
2828
* Indicates that the result of a {@link Pipe} transformation has changed even though the
29-
* reference
30-
* has not changed.
29+
* reference has not changed.
3130
*
32-
* The wrapped value will be unwrapped by change detection, and the unwrapped value will be stored.
31+
* Wrapped values are unwrapped automatically during the change detection, and the unwrapped value
32+
* is stored.
3333
*
3434
* Example:
3535
*
@@ -44,26 +44,22 @@ export function devModeEqual(a: any, b: any): boolean {
4444
* @stable
4545
*/
4646
export class WrappedValue {
47-
constructor(public wrapped: any) {}
47+
/** @deprecated from 5.3, use `unwrap()` instead - will switch to protected */
48+
wrapped: any;
4849

49-
static wrap(value: any): WrappedValue { return new WrappedValue(value); }
50-
}
50+
constructor(value: any) { this.wrapped = value; }
5151

52-
/**
53-
* Helper class for unwrapping WrappedValue s
54-
*/
55-
export class ValueUnwrapper {
56-
public hasWrappedValue = false;
52+
/** Creates a wrapped value. */
53+
static wrap(value: any): WrappedValue { return new WrappedValue(value); }
5754

58-
unwrap(value: any): any {
59-
if (value instanceof WrappedValue) {
60-
this.hasWrappedValue = true;
61-
return value.wrapped;
62-
}
63-
return value;
64-
}
55+
/**
56+
* Returns the underlying value of a wrapped value.
57+
* Returns the given `value` when it is not wrapped.
58+
**/
59+
static unwrap(value: any): any { return WrappedValue.isWrapped(value) ? value.wrapped : value; }
6560

66-
reset() { this.hasWrappedValue = false; }
61+
/** Returns true if `value` is a wrapped value. */
62+
static isWrapped(value: any): value is WrappedValue { return value instanceof WrappedValue; }
6763
}
6864

6965
/**

packages/core/src/core_private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS} from './application_ref';
1010
export {APP_ID_RANDOM_PROVIDER as ɵAPP_ID_RANDOM_PROVIDER} from './application_tokens';
11-
export {ValueUnwrapper as ɵValueUnwrapper, devModeEqual as ɵdevModeEqual} from './change_detection/change_detection_util';
11+
export {devModeEqual as ɵdevModeEqual} from './change_detection/change_detection_util';
1212
export {isListLikeIterable as ɵisListLikeIterable} from './change_detection/change_detection_util';
1313
export {ChangeDetectorStatus as ɵChangeDetectorStatus, isDefaultChangeDetectionStrategy as ɵisDefaultChangeDetectionStrategy} from './change_detection/constants';
1414
export {Console as ɵConsole} from './console';

packages/core/src/view/provider.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,7 @@ function updateProp(
437437
providerData.instance[propName] = value;
438438
if (def.flags & NodeFlags.OnChanges) {
439439
changes = changes || {};
440-
let oldValue = view.oldValues[def.bindingIndex + bindingIdx];
441-
if (oldValue instanceof WrappedValue) {
442-
oldValue = oldValue.wrapped;
443-
}
440+
const oldValue = WrappedValue.unwrap(view.oldValues[def.bindingIndex + bindingIdx]);
444441
const binding = def.bindings[bindingIdx];
445442
changes[binding.nonMinifiedName !] =
446443
new SimpleChange(oldValue, value, (view.state & ViewState.FirstCheck) !== 0);

packages/core/src/view/util.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ export function tokenKey(token: any): string {
2828
}
2929

3030
export function unwrapValue(view: ViewData, nodeIdx: number, bindingIdx: number, value: any): any {
31-
if (value instanceof WrappedValue) {
32-
value = value.wrapped;
33-
let globalBindingIdx = view.def.nodes[nodeIdx].bindingIndex + bindingIdx;
34-
let oldValue = view.oldValues[globalBindingIdx];
35-
if (oldValue instanceof WrappedValue) {
36-
oldValue = oldValue.wrapped;
37-
}
31+
if (WrappedValue.isWrapped(value)) {
32+
value = WrappedValue.unwrap(value);
33+
const globalBindingIdx = view.def.nodes[nodeIdx].bindingIndex + bindingIdx;
34+
const oldValue = WrappedValue.unwrap(view.oldValues[globalBindingIdx]);
3835
view.oldValues[globalBindingIdx] = new WrappedValue(oldValue);
3936
}
4037
return value;

tools/public_api_guard/core/core.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,10 @@ export declare abstract class ViewRef extends ChangeDetectorRef {
10931093

10941094
/** @stable */
10951095
export declare class WrappedValue {
1096-
wrapped: any;
1097-
constructor(wrapped: any);
1096+
/** @deprecated */ wrapped: any;
1097+
constructor(value: any);
1098+
static isWrapped(value: any): value is WrappedValue;
1099+
static unwrap(value: any): any;
10981100
static wrap(value: any): WrappedValue;
10991101
}
11001102

0 commit comments

Comments
 (0)