Skip to content

Commit

Permalink
Fixed a bug when a new value to set is equal by a link with the previous
Browse files Browse the repository at this point in the history
  • Loading branch information
kobezzza committed Sep 18, 2020
1 parent 32ab235 commit dc61102
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/core/object/watch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
> - :house: [Internal]
> - :nail_care: [Polish]
## v3.27.2 (2020-09-18)

#### :bug: Bug Fix

* Fixed a bug when a new value to set is equal by a link with the previous

## v3.22.5 (2020-07-31)

#### :bug: Bug Fix
Expand Down
7 changes: 4 additions & 3 deletions src/core/object/watch/engines/accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,12 @@ export function setWatchAccessors(

set(val: unknown): void {
let
fromProto = opts?.fromProto ?? false;

const
fromProto = opts?.fromProto ?? false,
oldVal = obj[key];

val = unwrap(val) ?? val;
oldVal = unwrap(oldVal) ?? oldVal;

if (oldVal !== val) {
try {
obj[key] = val;
Expand Down
6 changes: 4 additions & 2 deletions src/core/object/watch/engines/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ export function watch<T extends object>(
return false;
}

val = unwrap(val) ?? val;

const
isArray = Object.isArray(target),
isCustomObj = isArray || Object.isCustomObject(target),
Expand All @@ -275,8 +277,8 @@ export function watch<T extends object>(
key = Number(key);
}

const
oldVal = Reflect.get(target, key, isCustomObj ? receiver : target);
let oldVal = Reflect.get(target, key, isCustomObj ? receiver : target);
oldVal = unwrap(oldVal) ?? oldVal;

if (oldVal !== val && set()) {
if (!opts!.withProto && (fromProto || !isArray && !Object.hasOwnProperty(target, <string>key))) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/object/watch/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ describe('core/object/watch', () => {
spy(value, oldValue);
});

// eslint-disable-next-line no-self-assign
proxy.a = proxy.a;
expect(spy).not.toHaveBeenCalled();

proxy.a.b = [1, 2, 3];
expect(spy).toHaveBeenCalledWith([1, 2, 3], []);

Expand Down

0 comments on commit dc61102

Please sign in to comment.