[FIX] reactivity: don't react when we set the same object

The purpose of this commit is to not notify reactivity when an object is
replaced by the same object (same reference) in an reactive object.
This commit is contained in:
FrancoisGe
2023-10-12 09:47:44 +02:00
committed by Sam Degueldre
parent eec7cc4ea7
commit 6037018dd2
2 changed files with 10 additions and 9 deletions
+1 -1
View File
@@ -249,7 +249,7 @@ function basicProxyHandler<T extends Target>(callback: Callback): ProxyHandler<T
set(target, key, value, receiver) { set(target, key, value, receiver) {
const hadKey = objectHasOwnProperty.call(target, key); const hadKey = objectHasOwnProperty.call(target, key);
const originalValue = Reflect.get(target, key, receiver); const originalValue = Reflect.get(target, key, receiver);
const ret = Reflect.set(target, key, value, receiver); const ret = Reflect.set(target, key, toRaw(value), receiver);
if (!hadKey && objectHasOwnProperty.call(target, key)) { if (!hadKey && objectHasOwnProperty.call(target, key)) {
notifyReactives(target, KEYCHANGES); notifyReactives(target, KEYCHANGES);
} }
+9 -8
View File
@@ -993,32 +993,33 @@ describe("Reactivity", () => {
const obj2 = createReactive({ b: {} }, () => n2++); const obj2 = createReactive({ b: {} }, () => n2++);
const obj3 = createReactive({ c: {} }, () => n3++); const obj3 = createReactive({ c: {} }, () => n3++);
// assign the same object should'nt notify reactivity
obj2.b = obj2.b; obj2.b = obj2.b;
obj2.b; obj2.b;
obj3.c = obj3.c; obj3.c = obj3.c;
obj3.c; obj3.c;
expect(n1).toBe(0); expect(n1).toBe(0);
expect(n2).toBe(1); expect(n2).toBe(0);
expect(n3).toBe(1); expect(n3).toBe(0);
obj2.b = obj1; obj2.b = obj1;
obj2.b; obj2.b;
obj3.c = obj1; obj3.c = obj1;
obj3.c; obj3.c;
expect(n1).toBe(0); expect(n1).toBe(0);
expect(n2).toBe(2); expect(n2).toBe(1);
expect(n3).toBe(2); expect(n3).toBe(1);
obj1.a = obj1.a + 2; obj1.a = obj1.a + 2;
obj1.a; obj1.a;
expect(n1).toBe(1); expect(n1).toBe(1);
expect(n2).toBe(2); expect(n2).toBe(1);
expect(n3).toBe(2); expect(n3).toBe(1);
obj2.b.a = obj2.b.a + 1; obj2.b.a = obj2.b.a + 1;
expect(n1).toBe(2); expect(n1).toBe(2);
expect(n2).toBe(3); expect(n2).toBe(2);
expect(n3).toBe(2); expect(n3).toBe(1);
}); });
test("reactive inside other: reading the inner reactive from outer doesn't affect the inner's subscriptions", async () => { test("reactive inside other: reading the inner reactive from outer doesn't affect the inner's subscriptions", async () => {