Replies: 4 comments 12 replies
|
That is to say, we give up the dependency on |
|
If I understand it correctly, this has been suggested before. It's just a different design decision. There's a trade off and we chose what it is now. |
|
Now I found #1162 conflict with O(1) property, because if the proxy object reference changed, we need re-render to re-subscribe properties inside. This is the test this PR can't pass, no matter use it('re-subscribe after nested object re-assigned', async () => {
const state = proxy({ nested: { a: 0 } })
const renderFn = vi.fn()
const Component = () => {
const snap = useSnapshot(state)
renderFn()
return (
<>
<div>Nested a: {snap.nested.a}</div>
<button
onClick={() => {
state.nested = { a: 0, b: 0 } as any
}}
>
re-assign nested
</button>
<button
onClick={() => {
state.nested.a++
}}
>
increment a
</button>
</>
)
}
render(<Component />)
expect(screen.getByText('Nested a: 0')).toBeInTheDocument()
expect(renderFn).toBeCalledTimes(1)
fireEvent.click(screen.getByText('re-assign nested'))
await act(() => vi.advanceTimersByTimeAsync(0))
expect(renderFn).toBeCalledTimes(1)
fireEvent.click(screen.getByText('increment a'))
await act(() => vi.advanceTimersByTimeAsync(0))
expect(screen.getByText('Nested a: 1')).toBeInTheDocument()
expect(renderFn).toBeCalledTimes(2)
})This is another trade off need to make. I also feel the use case in #1162 can be avoid by user if use |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
After days migration trying from my own proxy to valtio, I found some performance degradation due to current subscribe logic in
useSnapshot.Current logic in
useSnapshotsubscribe to a big proxy, and useisChangedto check if it should update.If
proxyObjectis a bigMapfor N Items, and all items has a seperate component that calluseSnapshot(proxyObject)[uid], it would be O(N) time complexity for change comparison if any one of the item change its own property.My suggestion (also my old proxy implementation) is register a new listerner to the
proxyObjectwhen a snapshot propertygetterinvoked, and broadcast all listeners when the specify property setter inproxyObjectbeing called, then we will get O(1) time complexity on nested object property setting.All reactions