Skip to content

Commit

Permalink
wip: valtio v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jul 4, 2024
1 parent d68103b commit 042bcfb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 66 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@
"jotai": "^2.8.1",
"jotai-valtio": "link:.",
"prettier": "^3.2.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react": "19.0.0-rc.0",
"react-dom": "19.0.0-rc.0",
"ts-expect": "^1.3.0",
"typescript": "^5.4.5",
"valtio": "^1.13.2",
"valtio": "2.0.0-rc.0",
"vite": "^5.2.11",
"vitest": "^1.6.0"
},
"peerDependencies": {
"jotai": ">=2.0.0",
"valtio": ">=1.0.0"
"valtio": ">=2.0.0"
}
}
83 changes: 30 additions & 53 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions src/atomWithProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ type Options = {
sync?: boolean;
};

// Currently atomWithProxy does not support overwriting Promise() with a primitive
// due to the requirement of valtio types to always be symmetric.
// Consequently, this would not work:
// setStatusState({ ...state, status: 'newStatus' })
// To overwrite a value that came from a promise you must do it via an immediately
// resolving promise:
// setStatusState({ ...state, status: Promise.resolve('newStatus') })
export function atomWithProxy<Value extends object>(
proxyObject: Value,
options?: Options,
Expand Down
3 changes: 2 additions & 1 deletion src/mutableAtom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { atom } from 'jotai/vanilla';
import { proxy, snapshot, subscribe } from 'valtio/vanilla';
import { deepClone } from 'valtio/vanilla/utils';

type Wrapped<T> = { value: T };

Expand Down Expand Up @@ -65,7 +66,7 @@ export function mutableAtom<Value>(
* create the proxy state and subscribe to it
*/
function createProxyState(getStore: () => Store<Value>) {
const proxyState = proxyFn({ value: initialValue });
const proxyState = proxyFn({ value: deepClone(initialValue) });
// We never unsubscribe, but it's garbage collectable.
subscribe(proxyState, onChange(getStore), true);
return proxyState;
Expand Down
2 changes: 1 addition & 1 deletion tests/atomWithProxy.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference types="react/experimental" />
/// <reference types="react/canary" />

import { afterEach, expect, test } from 'vitest';
import { StrictMode, Suspense } from 'react';
Expand Down

0 comments on commit 042bcfb

Please sign in to comment.