Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported Method for Detecting Proxies #15345

Open
SBrazzCode opened this issue Feb 20, 2025 · 6 comments
Open

Supported Method for Detecting Proxies #15345

SBrazzCode opened this issue Feb 20, 2025 · 6 comments

Comments

@SBrazzCode
Copy link

Describe the problem

A common concern with svelte 5's introduction of rune proxies is the inability to detect proxies i.e issue-13562. A common issue when migrating code is sudden errors erupting from libraries/apis that don't accept proxies. Adding to that issue, there is no supported way to method to detect if an object is a reactive proxy, so user's have to use try/catch or other homegrown methods to detect if an object is a proxy.

Describe the proposed solution

Can we have a supported method for detecting proxies

Ideally, the proxy check is something integrated in Svelte's current proxy logic (linked above). Alternatively, a more generic javascript proxy detection method could work.

Importance

would make my life easier

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Feb 20, 2025

I don't think this is really that necessary; if you're concerned about whether an object is a $state proxy, you can just use $state.snapshot to unproxy the object.

@webJose
Copy link
Contributor

webJose commented Feb 20, 2025

Interesting thing: The Svelte framework will always refer to the value the proxy is for instead of the proxy itself:

<script>
	let name = $state('John')
</script>

<pre>Is Proxy?  {name instanceof Proxy}</pre>
<pre>Is string?  {typeof name === 'string'}</pre>

And:

<script>
	let name = $state('John')

	const isProxy = $derived(name instanceof Proxy)
	const isString = $derived(typeof name === 'string')
</script>

<pre>Is Proxy?  {isProxy}</pre>
<pre>Is string?  {isString}</pre>

Tells you that name is not a proxy, and instead is a string. So how does one go about passing the actual proxy? Honestly curious.

@7nik
Copy link
Contributor

7nik commented Feb 20, 2025

name instanceof Proxy won't work because Proxy has no prototype to compare with.

If some code cannot accept proxies, as said @Ocean-OS, just do $state.snapshot - it will unproxify the object or return the value it isn't Svelte's proxy.

@brunnerh
Copy link
Member

The Svelte framework will always refer to the value the proxy is for instead of the proxy itself

That seems like a bit of a misunderstanding. Svelte will not create proxies for primitive values (strings, numbers, booleans) at all. You need to set the state to a plain object or array for a proxy to be created. This proxy then can be passed around.

@webJose
Copy link
Contributor

webJose commented Feb 20, 2025

I see. So I guess we learn something new everyday.

  • So Proxy has no prototype.
  • Primitive values don't create proxies.

Thanks for the lessons, @7nik and @brunnerh .

@Ocean-OS
Copy link
Contributor

Ocean-OS commented Feb 20, 2025

  • So Proxy has no prototype.
  • Primitive values don't create proxies.

Yeah, the new Proxy syntax is kind of a misnomer, since proxies aren't class instances. I think of Proxy as a sort of intrinsic that tells the JS engine to override certain operations on the provided object.
Additionally some object-related functions don't work with Proxies, such as structuredClone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants