From f872fb21a763c233af8c58f25eb0521c9dbb416f Mon Sep 17 00:00:00 2001 From: daishi Date: Mon, 13 Nov 2023 22:41:21 +0900 Subject: [PATCH] remove proxyWithComputed --- src/vanilla/utils.ts | 1 - src/vanilla/utils/proxyWithComputed.ts | 48 ------- tests/computed.test.tsx | 166 +------------------------ 3 files changed, 1 insertion(+), 214 deletions(-) delete mode 100644 src/vanilla/utils/proxyWithComputed.ts diff --git a/src/vanilla/utils.ts b/src/vanilla/utils.ts index 5063b8ba..ef806c55 100644 --- a/src/vanilla/utils.ts +++ b/src/vanilla/utils.ts @@ -3,7 +3,6 @@ export { watch } from './utils/watch.ts' export { devtools } from './utils/devtools.ts' export { derive, underive, unstable_deriveSubscriptions } from 'derive-valtio' export { addComputed_DEPRECATED as addComputed } from './utils/addComputed.ts' -export { proxyWithComputed_DEPRECATED as proxyWithComputed } from './utils/proxyWithComputed.ts' export { deepClone } from './utils/deepClone.ts' export { proxyWithHistory } from './utils/proxyWithHistory.ts' export { proxySet } from './utils/proxySet.ts' diff --git a/src/vanilla/utils/proxyWithComputed.ts b/src/vanilla/utils/proxyWithComputed.ts deleted file mode 100644 index a1f41dd4..00000000 --- a/src/vanilla/utils/proxyWithComputed.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { proxy, snapshot } from '../../vanilla.ts' -import type { INTERNAL_Snapshot as Snapshot } from '../../vanilla.ts' - -/** - * proxyWithComputed (DEPRECATED) - * - * @deprecated Please follow "Computed Properties" guide in docs. - */ -export function proxyWithComputed_DEPRECATED< - T extends object, - U extends object, ->( - initialObject: T, - computedFns: { - [K in keyof U]: - | ((snap: Snapshot) => U[K]) - | { - get: (snap: Snapshot) => U[K] - set?: (state: T, newValue: U[K]) => void - } - }, -) { - if (import.meta.env?.MODE !== 'production') { - console.warn( - 'proxyWithComputed is deprecated. Please follow "Computed Properties" guide in docs.', - ) - } - ;(Object.keys(computedFns) as (keyof U)[]).forEach((key) => { - if (Object.getOwnPropertyDescriptor(initialObject, key)) { - throw new Error('object property already defined') - } - const computedFn = computedFns[key] - const { get, set } = ( - typeof computedFn === 'function' ? { get: computedFn } : computedFn - ) as { - get: (snap: Snapshot) => U[typeof key] - set?: (state: T, newValue: U[typeof key]) => void - } - const desc: PropertyDescriptor = {} - desc.get = () => get(snapshot(proxyObject)) - if (set) { - desc.set = (newValue) => set(proxyObject, newValue) - } - Object.defineProperty(initialObject, key, desc) - }) - const proxyObject = proxy(initialObject) as T & U - return proxyObject -} diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index e223b7e9..624fda31 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -5,7 +5,7 @@ import { fireEvent, render } from '@testing-library/react' import { memoize } from 'proxy-memoize' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { proxy, snapshot, subscribe, useSnapshot } from 'valtio' -import { addComputed, proxyWithComputed, subscribeKey } from 'valtio/utils' +import { addComputed, subscribeKey } from 'valtio/utils' const { use } = ReactExports const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) @@ -28,152 +28,6 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) -describe('proxyWithComputed', () => { - it('simple computed getters', async () => { - const computeDouble = vi.fn((x: number) => x * 2) - const state = proxyWithComputed( - { - text: '', - count: 0, - }, - { - doubled: { get: memoize((snap) => computeDouble(snap.count)) }, - }, - ) - - const callback = vi.fn() - subscribe(state, callback) - - expect(snapshot(state)).toMatchObject({ text: '', count: 0, doubled: 0 }) - expect(computeDouble).toBeCalledTimes(1) - expect(callback).toBeCalledTimes(0) - - state.count += 1 - await Promise.resolve() - expect(snapshot(state)).toMatchObject({ text: '', count: 1, doubled: 2 }) - expect(computeDouble).toBeCalledTimes(2) - expect(callback).toBeCalledTimes(1) - - state.text = 'a' - await Promise.resolve() - expect(snapshot(state)).toMatchObject({ text: 'a', count: 1, doubled: 2 }) - expect(computeDouble).toBeCalledTimes(2) - expect(callback).toBeCalledTimes(2) - }) - - it('computed getters and setters', async () => { - const computeDouble = vi.fn((x: number) => x * 2) - const state = proxyWithComputed( - { - text: '', - count: 0, - }, - { - doubled: { - get: memoize((snap) => computeDouble(snap.count)), - set: (state, newValue: number) => { - state.count = newValue / 2 - }, - }, - }, - ) - - expect(snapshot(state)).toMatchObject({ text: '', count: 0, doubled: 0 }) - expect(computeDouble).toBeCalledTimes(1) - - state.count += 1 - await Promise.resolve() - expect(snapshot(state)).toMatchObject({ text: '', count: 1, doubled: 2 }) - expect(computeDouble).toBeCalledTimes(2) - - state.doubled = 1 - await Promise.resolve() - expect(snapshot(state)).toMatchObject({ text: '', count: 0.5, doubled: 1 }) - expect(computeDouble).toBeCalledTimes(3) - - state.text = 'a' - await Promise.resolve() - expect(snapshot(state)).toMatchObject({ text: 'a', count: 0.5, doubled: 1 }) - expect(computeDouble).toBeCalledTimes(3) - }) - - it('computed setters with object and array', async () => { - const state = proxyWithComputed( - { - obj: { a: 1 }, - arr: [2], - }, - { - object: { - get: memoize((snap) => snap.obj), - set: (state, newValue: any) => { - state.obj = newValue - }, - }, - array: { - get: (snap) => snap.arr, - set: (state, newValue: any) => { - state.arr = newValue - }, - }, - }, - ) - - expect(snapshot(state)).toMatchObject({ - obj: { a: 1 }, - arr: [2], - object: { a: 1 }, - array: [2], - }) - - state.object = { a: 2 } - state.array = [3] - await Promise.resolve() - expect(snapshot(state)).toMatchObject({ - obj: { a: 2 }, - arr: [3], - object: { a: 2 }, - array: [3], - }) - }) - - it('render computed getter with condition (#435)', async () => { - const state = proxyWithComputed( - { - texts: [] as string[], - filter: '', - }, - { - filtered: memoize((snap) => { - if (!snap.filter) return snap.texts - return snap.texts.filter((text) => !text.includes(snap.filter)) - }), - }, - ) - - const Component = () => { - const snap = useSnapshot(state) - return ( - <> -
filtered: [{snap.filtered.join(',')}]
- - - ) - } - - const { getByText, findByText } = render( - - - , - ) - - await findByText('filtered: []') - - fireEvent.click(getByText('button')) - await findByText('filtered: [foo]') - }) -}) - describe('DEPRECATED addComputed', () => { it('simple addComputed', async () => { const computeDouble = vi.fn((x: number) => x * 2) @@ -307,21 +161,3 @@ describe('DEPRECATED addComputed', () => { }) }) }) - -describe('proxyWithComputed and subscribeKey', () => { - it('should call subscribeKey subscription when computed value changes?', async () => { - const state = proxyWithComputed( - { - count: 1, - }, - { - doubled: (snap) => snap.count * 2, - }, - ) - const handler = vi.fn() - subscribeKey(state, 'doubled', handler) - state.count = 2 - await Promise.resolve() - expect(handler).toBeCalledTimes(1) - }) -})