From 1ad5b60b81443f74d316e0f06c23632ce538cb4c Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Mon, 20 Jan 2025 15:44:53 -0500 Subject: [PATCH] fix: avoid "double proxying" This can be reverted when the following PR is merged: https://github.com/pmndrs/valtio/pull/1038 --- src/index.ts | 2 +- src/runtime/proxy.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/runtime/proxy.ts diff --git a/src/index.ts b/src/index.ts index 991864d..e3fb9a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ export { getVersion, - proxy, ref, snapshot, type INTERNAL_Op, @@ -22,3 +21,4 @@ export { type ReactiveClass, type ReactiveProxy, } from './runtime/instance' +export { proxy } from './runtime/proxy' diff --git a/src/runtime/proxy.ts b/src/runtime/proxy.ts new file mode 100644 index 0000000..3e95e57 --- /dev/null +++ b/src/runtime/proxy.ts @@ -0,0 +1,10 @@ +import { proxy as createProxy, unstable_getInternalStates } from 'valtio' + +const { proxyStateMap } = unstable_getInternalStates() + +export function proxy(data: T): T { + if (proxyStateMap.has(data)) { + return data + } + return createProxy(data) +}