From 826f506b0c2b79139068a18264fa43482d416e20 Mon Sep 17 00:00:00 2001 From: daishi Date: Sat, 13 Jul 2024 12:05:28 +0900 Subject: [PATCH] support valtio v1 --- package.json | 4 ++-- pnpm-lock.yaml | 28 +++++++++++++++++++--------- src/index.ts | 22 +++++++++++++--------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index dffa7e2..ee97392 100644 --- a/package.json +++ b/package.json @@ -75,13 +75,13 @@ "react-dom": "^18.3.1", "ts-expect": "^1.3.0", "typescript": "^5.5.3", - "valtio": "^2.0.0-rc.0", + "valtio": "^1.13.2", "vite": "^5.3.3", "vitest": "^2.0.2", "zustand": "^4.5.4", "zustand-valtio": "link:." }, "peerDependencies": { - "valtio": ">=2.0.0-rc.0" + "valtio": ">=1.0.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e66ea74..d7cf334 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -69,8 +69,8 @@ devDependencies: specifier: ^5.5.3 version: 5.5.3 valtio: - specifier: ^2.0.0-rc.0 - version: 2.0.0-rc.0(@types/react@18.3.3)(react@18.3.1) + specifier: ^1.13.2 + version: 1.13.2(@types/react@18.3.3)(react@18.3.1) vite: specifier: ^5.3.3 version: 5.3.3(@types/node@20.14.10) @@ -1293,6 +1293,14 @@ packages: engines: {node: '>=6'} dev: true + /derive-valtio@0.1.0(valtio@1.13.2): + resolution: {integrity: sha512-OCg2UsLbXK7GmmpzMXhYkdO64vhJ1ROUUGaTFyHjVwEdMEcTTRj7W1TxLbSBxdY8QLBPCcp66MTyaSy0RpO17A==} + peerDependencies: + valtio: '*' + dependencies: + valtio: 1.13.2(@types/react@18.3.3)(react@18.3.1) + dev: true + /dir-glob@3.0.1: resolution: {integrity: sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==} engines: {node: '>=8'} @@ -2680,8 +2688,8 @@ packages: react-is: 16.13.1 dev: true - /proxy-compare@3.0.0: - resolution: {integrity: sha512-y44MCkgtZUCT9tZGuE278fB7PWVf7fRYy0vbRXAts2o5F0EfC4fIQrvQQGBJo1WJbFcVLXzApOscyJuZqHQc1w==} + /proxy-compare@2.6.0: + resolution: {integrity: sha512-8xuCeM3l8yqdmbPoYeLbrAXCBWu19XEYc5/F28f5qOaoAIMyfmBUkl5axiK+x9olUvRlcekvnm98AP9RDngOIw==} dev: true /punycode@2.3.1: @@ -3185,12 +3193,12 @@ packages: react: 18.3.1 dev: true - /valtio@2.0.0-rc.0(@types/react@18.3.3)(react@18.3.1): - resolution: {integrity: sha512-0DPHuNs8uSR9bRNDeYwRDln9tz1LQuFva/Vkoe88Cs7NMvuhPIDVwp0DWp+S8oRAAZYHugHHj8s9MKQ/K1aC4A==} + /valtio@1.13.2(@types/react@18.3.3)(react@18.3.1): + resolution: {integrity: sha512-Qik0o+DSy741TmkqmRfjq+0xpZBXi/Y6+fXZLn0xNF1z/waFMbE3rkivv5Zcf9RrMUp6zswf2J7sbh2KBlba5A==} engines: {node: '>=12.20.0'} peerDependencies: - '@types/react': '>=18.0.0' - react: '>=18.0.0' + '@types/react': '>=16.8' + react: '>=16.8' peerDependenciesMeta: '@types/react': optional: true @@ -3198,8 +3206,10 @@ packages: optional: true dependencies: '@types/react': 18.3.3 - proxy-compare: 3.0.0 + derive-valtio: 0.1.0(valtio@1.13.2) + proxy-compare: 2.6.0 react: 18.3.1 + use-sync-external-store: 1.2.0(react@18.3.1) dev: true /vite-node@2.0.2(@types/node@20.14.10): diff --git a/src/index.ts b/src/index.ts index ab36617..99c6768 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,12 @@ import type { StateCreator, StoreApi, StoreMutatorIdentifier } from 'zustand'; import { proxy, snapshot, subscribe } from 'valtio/vanilla'; -import type { Snapshot } from 'valtio/vanilla'; + +// TODO replace with Valtio's Snapshot type in v2 +type Snapshot = T extends (...args: never[]) => unknown + ? T + : T extends object + ? { readonly [K in keyof T]: Snapshot } + : T; type StoreWithProxy = { setState: never; @@ -9,16 +15,14 @@ type StoreWithProxy = { type Write = Omit & U; -type DeepWritable = T extends object - ? { - -readonly [K in keyof T]: DeepWritable; - } - : T; - type WithWithProxy = S extends { getState: () => Snapshot } ? Write>> : never; +type DeepWritable = T extends object + ? { -readonly [K in keyof T]: DeepWritable } + : T; + declare module 'zustand/vanilla' { interface StoreMutators { 'zustand-valtio': WithWithProxy; @@ -45,7 +49,7 @@ const withProxyImpl: WithProxyImpl = (initialObject) => (set, get, api) => { const updateState = () => { if (!mutating) { ++updating; - set(snapshot(proxyState), true); + set(snapshot(proxyState) as Snapshot, true); --updating; } }; @@ -82,7 +86,7 @@ const withProxyImpl: WithProxyImpl = (initialObject) => (set, get, api) => { }); } }); - return snapshot(proxyState); + return snapshot(proxyState) as Snapshot; }; export const withProxy = withProxyImpl as unknown as WithProxy;