Skip to content

Commit

Permalink
remove proxyWithComputed
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Nov 13, 2023
1 parent 856d052 commit f872fb2
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 214 deletions.
1 change: 0 additions & 1 deletion src/vanilla/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
48 changes: 0 additions & 48 deletions src/vanilla/utils/proxyWithComputed.ts

This file was deleted.

166 changes: 1 addition & 165 deletions tests/computed.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fireEvent, render } from '@testing-library/react'
import { memoize } from 'proxy-memoize'

Check warning on line 5 in tests/computed.test.tsx

View workflow job for this annotation

GitHub Actions / lint

'memoize' is defined but never used. Allowed unused vars must match /^_/u
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'

Check warning on line 8 in tests/computed.test.tsx

View workflow job for this annotation

GitHub Actions / lint

'subscribeKey' is defined but never used. Allowed unused vars must match /^_/u

const { use } = ReactExports
const use2 = <T,>(x: T): Awaited<T> => (x instanceof Promise ? use(x) : x)
Expand All @@ -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 (
<>
<div>filtered: [{snap.filtered.join(',')}]</div>
<button onClick={() => (state.texts = ['foo'])}>button</button>
</>
)
}

const { getByText, findByText } = render(
<StrictMode>
<Component />
</StrictMode>,
)

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)
Expand Down Expand Up @@ -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)
})
})

0 comments on commit f872fb2

Please sign in to comment.