Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalidate snap cache when property is removed from object #736

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion tests/snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { createProxy, getUntracked } from 'proxy-compare'
import { TypeEqual, expectType } from 'ts-expect'

import { describe, expect, it } from 'vitest'
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) => {
Expand Down Expand Up @@ -69,6 +75,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 the array
* [2]. Remove target item should be nested object
* [3]. The array should be subscribed
* [4]. The array should pass to snapshot() before the target item removed
*/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi I added test and reverted the previous commit.
And wrote the error occurrence conditions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Can you resolve conflicts and merge the main branch?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! And I will look in to it about another approach that you suggested. 😀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, are you still around?

Copy link
Author

@Juyeong-Byeon Juyeong-Byeon Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some tries after this, but it didn't worked. (ex- force version update if child object removed)
I think It will be hard to fix without breaking something, as you said.

Do you have any idea about approach to fix it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#712 (comment) is my current understanding.


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<
Expand Down