How to check if a variable is a solid-Mutable or -Store?

Is there a way how you can detect if a variable is a createMutable/Store-proxy? It is for the purpose of working with another library's proxies.
6 Replies
fabiospampinato
I think the only way is to add some property to it and see if it throws
bigmistqke
bigmistqkeOP3y ago
import { DEV } from 'solid-js/store';
DEV?.isWrappable(x) === true
import { DEV } from 'solid-js/store';
DEV?.isWrappable(x) === true
would this be okay? it warns me that DEV can also be undefined
thetarnav
thetarnav3y ago
you can use the $PROXY symbol to check if it's a proxy at all DEV is not available in prod
bigmistqke
bigmistqkeOP3y ago
you just call it like an object? state[$PROXY]? ok nice that seems to do the trick!
fabiospampinato
const isMutable = store => {
if ( !store[$PROXY] ) return false; // Not a store at all
const symbol = Symbol ();
try {
store[symbol] = true;
delete store[symbol];
return true; // A mutable store
} catch {
return false; // A regular store
}
};
const isMutable = store => {
if ( !store[$PROXY] ) return false; // Not a store at all
const symbol = Symbol ();
try {
store[symbol] = true;
delete store[symbol];
return true; // A mutable store
} catch {
return false; // A regular store
}
};
bigmistqke
bigmistqkeOP3y ago
oo nice, it's for interop with another library, so for my usecase rn it doesn't matter too much if it's a store or a mutable, but good to know
Want results from more Discord servers?
Add your server