From 40636a24d7c3e3b128ee20731280ecb573bd01f4 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 10:34:33 +0900 Subject: [PATCH 1/8] [v2] breaking: do not throw promises --- package.json | 4 ++-- src/react.ts | 13 +++--------- src/vanilla.ts | 47 +++++------------------------------------- tests/async.test.tsx | 14 +++++++------ tests/snapshot.test.ts | 31 ---------------------------- yarn.lock | 26 +++++++++++------------ 6 files changed, 31 insertions(+), 104 deletions(-) diff --git a/package.json b/package.json index acd29c98..443c9676 100644 --- a/package.json +++ b/package.json @@ -158,8 +158,8 @@ "postinstall-postinstall": "^2.1.0", "prettier": "^3.0.3", "proxy-memoize": "^2.0.4", - "react": "^18.2.0", - "react-dom": "^18.2.0", + "react": "18.3.0-canary-c47c306a7-20231109", + "react-dom": "18.3.0-canary-c47c306a7-20231109", "redux": "^4.2.1", "rollup": "^4.2.0", "rollup-plugin-esbuild": "^6.1.0", diff --git a/src/react.ts b/src/react.ts index ec8dc009..39557c87 100644 --- a/src/react.ts +++ b/src/react.ts @@ -1,12 +1,6 @@ /// -import ReactExports, { - useCallback, - useDebugValue, - useEffect, - useMemo, - useRef, -} from 'react' +import { useCallback, useDebugValue, useEffect, useMemo, useRef } from 'react' import { affectedToPathList, createProxy as createProxyToCompare, @@ -21,7 +15,6 @@ import useSyncExternalStoreExports from 'use-sync-external-store/shim' import { snapshot, subscribe } from './vanilla.ts' import type { INTERNAL_Snapshot as Snapshot } from './vanilla.ts' -const { use } = ReactExports const { useSyncExternalStore } = useSyncExternalStoreExports const useAffectedDebugValue = ( @@ -133,7 +126,7 @@ export function useSnapshot( [proxyObject, notifyInSync] ), () => { - const nextSnapshot = snapshot(proxyObject, use) + const nextSnapshot = snapshot(proxyObject) try { if ( !inRender && @@ -154,7 +147,7 @@ export function useSnapshot( } return nextSnapshot }, - () => snapshot(proxyObject, use) + () => snapshot(proxyObject) ) inRender = false const currAffected = new WeakMap() diff --git a/src/vanilla.ts b/src/vanilla.ts index 2685c7d7..0c829ef5 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -33,8 +33,6 @@ type SnapshotIgnore = type Snapshot = T extends SnapshotIgnore ? T - : T extends Promise - ? Awaited : T extends object ? { readonly [K in keyof T]: Snapshot } : T @@ -45,13 +43,7 @@ type Snapshot = T extends SnapshotIgnore */ export type INTERNAL_Snapshot = Snapshot -type HandlePromise =

>(promise: P) => Awaited

- -type CreateSnapshot = ( - target: T, - version: number, - handlePromise?: HandlePromise -) => T +type CreateSnapshot = (target: T, version: number) => T type RemoveListener = () => void type AddListener = (listener: Listener) => RemoveListener @@ -86,29 +78,11 @@ const buildProxyFunction = ( !(x instanceof RegExp) && !(x instanceof ArrayBuffer), - defaultHandlePromise =

>( - promise: P & { - status?: 'pending' | 'fulfilled' | 'rejected' - value?: Awaited

- reason?: unknown - } - ) => { - switch (promise.status) { - case 'fulfilled': - return promise.value as Awaited

- case 'rejected': - throw promise.reason - default: - throw promise - } - }, - snapCache = new WeakMap(), createSnapshot: CreateSnapshot = ( target: T, - version: number, - handlePromise: HandlePromise = defaultHandlePromise + version: number ): T => { const cache = snapCache.get(target) if (cache?.[0] === version) { @@ -138,18 +112,11 @@ const buildProxyFunction = ( } if (refSet.has(value as object)) { markToTrack(value as object, false) // mark not to track - } else if (value instanceof Promise) { - delete desc.value - desc.get = () => handlePromise(value) } else if (proxyStateMap.has(value as object)) { const [target, ensureVersion] = proxyStateMap.get( value as object ) as ProxyState - desc.value = createSnapshot( - target, - ensureVersion(), - handlePromise - ) as Snapshot + desc.value = createSnapshot(target, ensureVersion()) as Snapshot } Object.defineProperty(snap, key, desc) }) @@ -337,7 +304,6 @@ const buildProxyFunction = ( objectIs, newProxy, canProxy, - defaultHandlePromise, snapCache, createSnapshot, proxyCache, @@ -391,16 +357,13 @@ export function subscribe( } } -export function snapshot( - proxyObject: T, - handlePromise?: HandlePromise -): Snapshot { +export function snapshot(proxyObject: T): Snapshot { const proxyState = proxyStateMap.get(proxyObject as object) if (import.meta.env?.MODE !== 'production' && !proxyState) { console.warn('Please use proxy object') } const [target, ensureVersion, createSnapshot] = proxyState as ProxyState - return createSnapshot(target, ensureVersion(), handlePromise) as Snapshot + return createSnapshot(target, ensureVersion()) as Snapshot } export function ref(obj: T): T & AsRef { diff --git a/tests/async.test.tsx b/tests/async.test.tsx index ef4ef888..454f72be 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -1,4 +1,4 @@ -import { StrictMode, Suspense } from 'react' +import { StrictMode, Suspense, use } from 'react' import { fireEvent, render, waitFor } from '@testing-library/react' import { it } from 'vitest' import { proxy, useSnapshot } from 'valtio' @@ -8,6 +8,8 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) +const use2 = (x: T): T => (x instanceof Promise ? use(x) : x) + it('delayed increment', async () => { const state = proxy({ count: 0 }) const delayedIncrement = () => { @@ -19,7 +21,7 @@ it('delayed increment', async () => { const snap = useSnapshot(state) return ( <> -

count: {snap.count}
+
count: {use2(snap.count)}
) @@ -50,7 +52,7 @@ it('delayed object', async () => { const snap = useSnapshot(state) return ( <> -
text: {snap.object.text}
+
text: {use2(snap.object).text}
) @@ -85,8 +87,8 @@ it('delayed object update fulfilled', async () => { const snap = useSnapshot(state) return ( <> -
text: {snap.object.text}
-
count: {snap.object.count}
+
text: {use2(snap.object).text}
+
count: {use2(snap.object).count}
) @@ -125,7 +127,7 @@ it('delayed falsy value', async () => { const snap = useSnapshot(state) return ( <> -
value: {String(snap.value)}
+
value: {String(use2(snap.value))}
) diff --git a/tests/snapshot.test.ts b/tests/snapshot.test.ts index 6a6494d9..57f4e091 100644 --- a/tests/snapshot.test.ts +++ b/tests/snapshot.test.ts @@ -3,28 +3,6 @@ import { TypeEqual, expectType } from 'ts-expect' import { describe, expect, it } from 'vitest' import { INTERNAL_Snapshot as Snapshot, proxy, snapshot } from 'valtio' -const sleep = (ms: number) => - new Promise((resolve) => { - setTimeout(resolve, ms) - }) - -it('getter returns value after promise is resolved', async () => { - const state = proxy({ status: sleep(10).then(() => 'done') }) - const snap = snapshot(state) - - await new Promise((resolve) => { - resolve(snap.status) - }) - .catch((thrown) => { - expect(thrown).toBeInstanceOf(Promise) - return thrown - }) - .then((value) => { - expect(value).toBe('done') - expect(snap.status).toBe('done') - }) -}) - it('should return correct snapshots without subscribe', async () => { const child = proxy({ count: 0 }) const state = proxy({ child }) @@ -119,15 +97,6 @@ describe('snapsoht typings', () => { >(true) }) - it('infers Promise result from property value', () => { - expectType< - TypeEqual< - Snapshot<{ promise: Promise }>, - { readonly promise: string } - > - >(true) - }) - it('converts arrays to readonly arrays', () => { expectType, readonly number[]>>(true) }) diff --git a/yarn.lock b/yarn.lock index 70e6fa51..a4a7addd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4259,13 +4259,13 @@ randombytes@^2.1.0: dependencies: safe-buffer "^5.1.0" -react-dom@^18.2.0: - version "18.2.0" - resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz#22aaf38708db2674ed9ada224ca4aa708d821e3d" - integrity sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g== +react-dom@18.3.0-canary-c47c306a7-20231109: + version "18.3.0-canary-c47c306a7-20231109" + resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.3.0-canary-c47c306a7-20231109.tgz#a5b881218f760a44e09469f7c1d38c8354b6e0b9" + integrity sha512-COjHi7Ve6fCDStLinhvKxUpAVRDwkHIXtH9m8crIc0d3KjE+PMTmmIwQQj3ceypDAARQ1VFwHOhMQgXcdRAxlA== dependencies: loose-envify "^1.1.0" - scheduler "^0.23.0" + scheduler "0.24.0-canary-c47c306a7-20231109" react-is@^16.13.1: version "16.13.1" @@ -4282,10 +4282,10 @@ react-is@^18.0.0: resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b" integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w== -react@^18.2.0: - version "18.2.0" - resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" - integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ== +react@18.3.0-canary-c47c306a7-20231109: + version "18.3.0-canary-c47c306a7-20231109" + resolved "https://registry.yarnpkg.com/react/-/react-18.3.0-canary-c47c306a7-20231109.tgz#53878c332f829d5b25f6df6c8c3746cd12a3b922" + integrity sha512-LtL67Bc+Mkuhwud559dUCU+QXL6mmtgKTGEyT41m5bDiEQdGAyYCxXo5/pigAx4p8mQb+KIT2AvCWlEDKut+PQ== dependencies: loose-envify "^1.1.0" @@ -4522,10 +4522,10 @@ saxes@^6.0.0: dependencies: xmlchars "^2.2.0" -scheduler@^0.23.0: - version "0.23.0" - resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.0.tgz#ba8041afc3d30eb206a487b6b384002e4e61fdfe" - integrity sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw== +scheduler@0.24.0-canary-c47c306a7-20231109: + version "0.24.0-canary-c47c306a7-20231109" + resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.24.0-canary-c47c306a7-20231109.tgz#9d2f0329d603279093b38999f78eac04bf64f8a0" + integrity sha512-VGLhOyPt1EIAsoGqu7DMxnVPdGAoxTmSjNJlGX31exX0stiZsvb3QuK5bKT1BaTpDOJD4VnNFoonMRN7BNh3Cg== dependencies: loose-envify "^1.1.0" From 5c98de741648b32bb3f384c1bca1bdcfffa8d160 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 10:49:07 +0900 Subject: [PATCH 2/8] use use --- src/react.ts | 2 -- tests/async.test.tsx | 4 +++- tests/computed.test.tsx | 8 ++++++-- tests/derive.test.tsx | 8 ++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/react.ts b/src/react.ts index 39557c87..7f529fb6 100644 --- a/src/react.ts +++ b/src/react.ts @@ -1,5 +1,3 @@ -/// - import { useCallback, useDebugValue, useEffect, useMemo, useRef } from 'react' import { affectedToPathList, diff --git a/tests/async.test.tsx b/tests/async.test.tsx index 454f72be..1c6b59df 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -1,3 +1,5 @@ +/// + import { StrictMode, Suspense, use } from 'react' import { fireEvent, render, waitFor } from '@testing-library/react' import { it } from 'vitest' @@ -8,7 +10,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) -const use2 = (x: T): T => (x instanceof Promise ? use(x) : x) +const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) it('delayed increment', async () => { const state = proxy({ count: 0 }) diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index 1d0e61c1..ed1a6f87 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -1,10 +1,14 @@ -import { StrictMode, Suspense } from 'react' +/// + +import { StrictMode, Suspense, use } from 'react' 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' +const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) + const consoleWarn = console.warn beforeEach(() => { console.warn = vi.fn((message: string) => { @@ -217,7 +221,7 @@ describe('DEPRECATED addComputed', () => { return ( <>
- count: {snap.count}, delayedCount: {snap.delayedCount} + count: {snap.count}, delayedCount: {use2(snap.delayedCount)}
diff --git a/tests/derive.test.tsx b/tests/derive.test.tsx index 89a01951..f8bd88f7 100644 --- a/tests/derive.test.tsx +++ b/tests/derive.test.tsx @@ -1,4 +1,6 @@ -import { StrictMode, Suspense, useEffect, useRef } from 'react' +/// + +import { StrictMode, Suspense, use, useEffect, useRef } from 'react' import { fireEvent, render } from '@testing-library/react' import { describe, expect, it, vi } from 'vitest' import { proxy, snapshot, subscribe, useSnapshot } from 'valtio' @@ -11,6 +13,8 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) +const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) + it('basic derive', async () => { const computeDouble = vi.fn((x: number) => x * 2) const state = proxy({ @@ -168,7 +172,7 @@ it('async derive', async () => { return ( <>
- count: {snap.count}, delayedCount: {snap.delayedCount} + count: {snap.count}, delayedCount: {use2(snap.delayedCount)}
From b1d6a7d32ad10990c06efc1f9b7b5f4001adbcb6 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 10:55:42 +0900 Subject: [PATCH 3/8] fix CI hopefully --- .github/workflows/test-multiple-builds.yml | 4 ---- tests/async.test.tsx | 1 + tests/computed.test.tsx | 1 + tests/derive.test.tsx | 1 + 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-multiple-builds.yml b/.github/workflows/test-multiple-builds.yml index ea5e80e8..d149ede2 100644 --- a/.github/workflows/test-multiple-builds.yml +++ b/.github/workflows/test-multiple-builds.yml @@ -23,10 +23,6 @@ jobs: - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - run: yarn install --frozen-lockfile --check-files - run: yarn build - - name: Use React 17 for production test - if: ${{ matrix.env == 'production' }} - run: | - yarn add -D react@17.0.2 react-dom@17.0.2 @testing-library/react@12.1.4 - name: Patch for DEV-ONLY if: ${{ matrix.env == 'development' }} run: | diff --git a/tests/async.test.tsx b/tests/async.test.tsx index 1c6b59df..20a69d54 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -10,6 +10,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) +type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) it('delayed increment', async () => { diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index ed1a6f87..ccf1323e 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -7,6 +7,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { proxy, snapshot, subscribe, useSnapshot } from 'valtio' import { addComputed, proxyWithComputed, subscribeKey } from 'valtio/utils' +type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) const consoleWarn = console.warn diff --git a/tests/derive.test.tsx b/tests/derive.test.tsx index f8bd88f7..785c3dc6 100644 --- a/tests/derive.test.tsx +++ b/tests/derive.test.tsx @@ -13,6 +13,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) +type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) it('basic derive', async () => { From 2e2dda63042b2983fb5b28934664ba278dfd0c65 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 11:04:17 +0900 Subject: [PATCH 4/8] fix CI hopefully 2 --- .github/workflows/test-multiple-builds.yml | 4 ++++ tests/async.test.tsx | 2 +- tests/computed.test.tsx | 2 +- tests/derive.test.tsx | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-multiple-builds.yml b/.github/workflows/test-multiple-builds.yml index d149ede2..ea5e80e8 100644 --- a/.github/workflows/test-multiple-builds.yml +++ b/.github/workflows/test-multiple-builds.yml @@ -23,6 +23,10 @@ jobs: - run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - run: yarn install --frozen-lockfile --check-files - run: yarn build + - name: Use React 17 for production test + if: ${{ matrix.env == 'production' }} + run: | + yarn add -D react@17.0.2 react-dom@17.0.2 @testing-library/react@12.1.4 - name: Patch for DEV-ONLY if: ${{ matrix.env == 'development' }} run: | diff --git a/tests/async.test.tsx b/tests/async.test.tsx index 20a69d54..8f3798f9 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -13,7 +13,7 @@ const sleep = (ms: number) => type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) -it('delayed increment', async () => { +it.skipIf(typeof use === 'undefined')('delayed increment', async () => { const state = proxy({ count: 0 }) const delayedIncrement = () => { const nextCount = state.count + 1 diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index ccf1323e..83d6edc7 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -206,7 +206,7 @@ describe('DEPRECATED addComputed', () => { expect(callback).toBeCalledTimes(2) }) - it('async addComputed', async () => { + it.skipIf(typeof use === 'undefined')('async addComputed', async () => { const state = proxy({ count: 0 }) addComputed(state, { delayedCount: async (snap) => { diff --git a/tests/derive.test.tsx b/tests/derive.test.tsx index 785c3dc6..b39d8b9f 100644 --- a/tests/derive.test.tsx +++ b/tests/derive.test.tsx @@ -154,7 +154,7 @@ it('derive with two dependencies', async () => { expect(callback).toBeCalledTimes(2) }) -it('async derive', async () => { +it.skipIf(typeof use === 'undefined')('async derive', async () => { const state = proxy({ count: 0 }) derive( { From eaa2e9c9fb366f1cdf92a858848b9fc6d795c411 Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 11:08:57 +0900 Subject: [PATCH 5/8] fix CI hopefully 3 --- tests/async.test.tsx | 3 ++- tests/computed.test.tsx | 3 ++- tests/derive.test.tsx | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/async.test.tsx b/tests/async.test.tsx index 8f3798f9..c939ef9f 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -1,6 +1,6 @@ /// -import { StrictMode, Suspense, use } from 'react' +import ReactExports, { StrictMode, Suspense } from 'react' import { fireEvent, render, waitFor } from '@testing-library/react' import { it } from 'vitest' import { proxy, useSnapshot } from 'valtio' @@ -10,6 +10,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) +const { use } = ReactExports type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index 83d6edc7..ce4892d0 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -1,12 +1,13 @@ /// -import { StrictMode, Suspense, use } from 'react' +import ReactExports, { StrictMode, Suspense } from 'react' 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' +const { use } = ReactExports type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) diff --git a/tests/derive.test.tsx b/tests/derive.test.tsx index b39d8b9f..a6240684 100644 --- a/tests/derive.test.tsx +++ b/tests/derive.test.tsx @@ -1,6 +1,6 @@ /// -import { StrictMode, Suspense, use, useEffect, useRef } from 'react' +import ReactExports, { StrictMode, Suspense, useEffect, useRef } from 'react' import { fireEvent, render } from '@testing-library/react' import { describe, expect, it, vi } from 'vitest' import { proxy, snapshot, subscribe, useSnapshot } from 'valtio' @@ -13,6 +13,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) +const { use } = ReactExports type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) From 3fecf89aaf743a8e45c423f8f22da06390613cdf Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 11:14:45 +0900 Subject: [PATCH 6/8] fix CI hopefully 4 --- tests/async.test.tsx | 83 +++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/tests/async.test.tsx b/tests/async.test.tsx index c939ef9f..9b5eb2db 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -46,7 +46,7 @@ it.skipIf(typeof use === 'undefined')('delayed increment', async () => { await findByText('count: 1') }) -it('delayed object', async () => { +it.skipIf(typeof use === 'undefined')('delayed object', async () => { const state = proxy({ object: { text: 'none' } }) const delayedObject = () => { state.object = sleep(300).then(() => ({ text: 'hello' })) @@ -77,49 +77,52 @@ it('delayed object', async () => { await findByText('text: hello') }) -it('delayed object update fulfilled', async () => { - const state = proxy({ - object: sleep(300).then(() => ({ text: 'counter', count: 0 })), - }) - const updateObject = () => { - state.object = state.object.then((v: any) => - sleep(300).then(() => ({ ...v, count: v.count + 1 })) +it.skipIf(typeof use === 'undefined')( + 'delayed object update fulfilled', + async () => { + const state = proxy({ + object: sleep(300).then(() => ({ text: 'counter', count: 0 })), + }) + const updateObject = () => { + state.object = state.object.then((v: any) => + sleep(300).then(() => ({ ...v, count: v.count + 1 })) + ) + } + + const Counter = () => { + const snap = useSnapshot(state) + return ( + <> +
text: {use2(snap.object).text}
+
count: {use2(snap.object).count}
+ + + ) + } + + const { getByText, findByText } = render( + + + + + ) - } - - const Counter = () => { - const snap = useSnapshot(state) - return ( - <> -
text: {use2(snap.object).text}
-
count: {use2(snap.object).count}
- - - ) - } - - const { getByText, findByText } = render( - - - - - - ) - await findByText('loading') - await waitFor(() => { - getByText('text: counter') - getByText('count: 0') - }) + await findByText('loading') + await waitFor(() => { + getByText('text: counter') + getByText('count: 0') + }) - fireEvent.click(getByText('button')) + fireEvent.click(getByText('button')) - await findByText('loading') - await waitFor(() => { - getByText('text: counter') - getByText('count: 1') - }) -}) + await findByText('loading') + await waitFor(() => { + getByText('text: counter') + getByText('count: 1') + }) + } +) it('delayed falsy value', async () => { const state = proxy({ value: true }) From 3f41975079b9551a6725033add68f322bdfdebae Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 12:17:53 +0900 Subject: [PATCH 7/8] fix CI hopefully 5 --- tests/async.test.tsx | 4 ++-- tests/computed.test.tsx | 2 +- tests/derive.test.tsx | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/async.test.tsx b/tests/async.test.tsx index 9b5eb2db..d5401619 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -10,7 +10,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) -const { use } = ReactExports +const { use } = ReactExports as any // for TS < 4.3 FIXME later type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) @@ -124,7 +124,7 @@ it.skipIf(typeof use === 'undefined')( } ) -it('delayed falsy value', async () => { +it.skipIf(typeof use === 'undefined')('delayed falsy value', async () => { const state = proxy({ value: true }) const delayedValue = () => { state.value = sleep(300).then(() => null) diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index ce4892d0..7b7d0e33 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -7,7 +7,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { proxy, snapshot, subscribe, useSnapshot } from 'valtio' import { addComputed, proxyWithComputed, subscribeKey } from 'valtio/utils' -const { use } = ReactExports +const { use } = ReactExports as any // for TS < 4.3 FIXME later type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) diff --git a/tests/derive.test.tsx b/tests/derive.test.tsx index a6240684..76dfcea9 100644 --- a/tests/derive.test.tsx +++ b/tests/derive.test.tsx @@ -13,7 +13,7 @@ const sleep = (ms: number) => setTimeout(resolve, ms) }) -const { use } = ReactExports +const { use } = ReactExports as any // for TS < 4.3 FIXME later type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) From 03cd68a109fda4cfd38f24e5741c1bbf13878c6a Mon Sep 17 00:00:00 2001 From: daishi Date: Fri, 10 Nov 2023 12:23:08 +0900 Subject: [PATCH 8/8] any type for simplicity --- tests/async.test.tsx | 3 +-- tests/computed.test.tsx | 3 +-- tests/derive.test.tsx | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/async.test.tsx b/tests/async.test.tsx index d5401619..0c101d61 100644 --- a/tests/async.test.tsx +++ b/tests/async.test.tsx @@ -11,8 +11,7 @@ const sleep = (ms: number) => }) const { use } = ReactExports as any // for TS < 4.3 FIXME later -type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later -const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) +const use2 = (x: any) => (x instanceof Promise ? use(x) : x) it.skipIf(typeof use === 'undefined')('delayed increment', async () => { const state = proxy({ count: 0 }) diff --git a/tests/computed.test.tsx b/tests/computed.test.tsx index 7b7d0e33..2644dffa 100644 --- a/tests/computed.test.tsx +++ b/tests/computed.test.tsx @@ -8,8 +8,7 @@ import { proxy, snapshot, subscribe, useSnapshot } from 'valtio' import { addComputed, proxyWithComputed, subscribeKey } from 'valtio/utils' const { use } = ReactExports as any // for TS < 4.3 FIXME later -type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later -const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) +const use2 = (x: any) => (x instanceof Promise ? use(x) : x) const consoleWarn = console.warn beforeEach(() => { diff --git a/tests/derive.test.tsx b/tests/derive.test.tsx index 76dfcea9..ea0f9d74 100644 --- a/tests/derive.test.tsx +++ b/tests/derive.test.tsx @@ -14,8 +14,7 @@ const sleep = (ms: number) => }) const { use } = ReactExports as any // for TS < 4.3 FIXME later -type Awaited = T extends Promise ? V : T // for TS < 4.5 FIXME later -const use2 = (x: T): Awaited => (x instanceof Promise ? use(x) : x) +const use2 = (x: any) => (x instanceof Promise ? use(x) : x) it('basic derive', async () => { const computeDouble = vi.fn((x: number) => x * 2)