LegendApp/legend-state

useComputed deep array object set v2/v3

Opened this issue · 1 comments

Computed is not reactive to deep observable array object change.
And it depends how it being set.
But observable is reactive.
Here is a test, last one fails:

    test.only('useComputed vs observable deep object set', () => {
        const o$ = observable([{ hotspot: { position: { x: 0 } } }]);
        let numRenders = 0;
        const Test = observer(function Test() {
            const c$ = useComputed(() => {
                return o$.get();
            }, []);
            if (numRenders === 0) {
                o$.get();
            } else {
                c$.get();
            }
            numRenders++;

            return createElement('div', undefined);
        });
        function App() {
            return createElement(Test);
        }
        render(createElement(App));

        act(() => {
            o$[0].hotspot.position.x.set(2);
        });

        expect(numRenders).toEqual(2);

        act(() => {
            o$.set([{ hotspot: { position: { x: 1 } } }]);
        });

        expect(numRenders).toEqual(3);

        act(() => {
            o$[0].hotspot.position.x.set(2);
        });

        expect(numRenders).toEqual(4);
    });

Oops, I fixed this in alpha.20 but forgot to reply. Is this working better for you in the latest?