From a45caf3884096be2dd8f690df0fa3fb7cbc421ec Mon Sep 17 00:00:00 2001 From: MatthewByeon Date: Sat, 10 Jun 2023 00:54:05 +0900 Subject: [PATCH 1/4] fix: invalidate snap cache when property is removed from object --- src/vanilla.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/vanilla.ts b/src/vanilla.ts index 78449945..8f056e1c 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -244,6 +244,7 @@ const buildProxyFunction = ( const handler: ProxyHandler = { deleteProperty(target: T, prop: string | symbol) { const prevValue = Reflect.get(target, prop) + snapCache.delete(target as Object) removePropListener(prop) const deleted = Reflect.deleteProperty(target, prop) if (deleted) { From 22c185bcef7d3ed85ead1695777d4f6d5e5e79d5 Mon Sep 17 00:00:00 2001 From: Juyeong-Byeon Date: Sat, 10 Jun 2023 23:10:46 +0900 Subject: [PATCH 2/4] Revert "fix: invalidate snap cache when property is removed from object" This reverts commit a45caf3884096be2dd8f690df0fa3fb7cbc421ec. --- src/vanilla.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/vanilla.ts b/src/vanilla.ts index 8f056e1c..78449945 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -244,7 +244,6 @@ const buildProxyFunction = ( const handler: ProxyHandler = { deleteProperty(target: T, prop: string | symbol) { const prevValue = Reflect.get(target, prop) - snapCache.delete(target as Object) removePropListener(prop) const deleted = Reflect.deleteProperty(target, prop) if (deleted) { From 9023d2737f00cabfb839a3b557650432e8381093 Mon Sep 17 00:00:00 2001 From: Juyeong-Byeon Date: Sat, 10 Jun 2023 23:11:43 +0900 Subject: [PATCH 3/4] test: add snapshot test related to #712 --- tests/snapshot.test.ts | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/snapshot.test.ts b/tests/snapshot.test.ts index 11b757c5..b6da73bf 100644 --- a/tests/snapshot.test.ts +++ b/tests/snapshot.test.ts @@ -1,7 +1,12 @@ import { describe, expect, it } from '@jest/globals' import { createProxy, getUntracked } from 'proxy-compare' import { TypeEqual, expectType } from 'ts-expect' -import { INTERNAL_Snapshot as Snapshot, proxy, snapshot } from 'valtio' +import { + INTERNAL_Snapshot as Snapshot, + proxy, + snapshot, + subscribe, +} from 'valtio' const sleep = (ms: number) => new Promise((resolve) => { @@ -69,6 +74,41 @@ it('should create a new proxy from a snapshot', async () => { expect(state2.c).toBe(0) }) +it('should return updated property snapshot when property is updated after removed from Array and added to array again', async () => { + /** + * error occurs when + * [1]. Remove target item should be in array + * [2]. Remove target item should be nested object + * [3]. Array should subscribed + * [4]. Array should pass to snapshot() before target item removed + */ + + const array = proxy([ + //[1] + { + //[2] + nested: { + value: 'before', + }, + }, + ]) + + //[4] + subscribe(array, () => {}) + //[3] + expect(snapshot(array)[0]?.nested.value).toEqual('before') + + const property = array[0] + array.splice(0, 1) + + if (property) { + property.nested.value = 'after' + array.push(property) + } + + expect(snapshot(array)[0]?.nested.value).toEqual('after') +}) + describe('snapsoht typings', () => { it('converts object properties to readonly', () => { expectType< From 37d7e4da62d141800b511cfe64681e23f72add91 Mon Sep 17 00:00:00 2001 From: Juyeong-Byeon Date: Sat, 10 Jun 2023 23:18:38 +0900 Subject: [PATCH 4/4] test: improve comment --- tests/snapshot.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/snapshot.test.ts b/tests/snapshot.test.ts index b6da73bf..5af8fc1f 100644 --- a/tests/snapshot.test.ts +++ b/tests/snapshot.test.ts @@ -77,10 +77,10 @@ it('should create a new proxy from a snapshot', async () => { it('should return updated property snapshot when property is updated after removed from Array and added to array again', async () => { /** * error occurs when - * [1]. Remove target item should be in array + * [1]. Remove target item should be in the array * [2]. Remove target item should be nested object - * [3]. Array should subscribed - * [4]. Array should pass to snapshot() before target item removed + * [3]. The array should be subscribed + * [4]. The array should pass to snapshot() before the target item removed */ const array = proxy([