Custom proxy handler #610
jacob-orbiit
started this conversation in
Ideas
Replies: 2 comments 14 replies
-
Let's see if you really need proxies for this case. This works capability-wise, right? const state = proxy({ user: null })
const getUserFromState = async () => {
if (!state.user) {
state.user = await getUser()
}
return state.user
} |
Beta Was this translation helpful? Give feedback.
8 replies
-
Let's change it to non async, and store a promise. const state = proxy({ user: null })
const getUserFromState = () => {
if (!state.user) {
state.user = getUser()
}
return state.user
} If that ☝️ works, the proxy version would be something like this 👇 . const state = new Proxy(proxy({ user: null }), {
get(target, prop) {
if (prop === 'user' && !target.user) {
target.user = getUser()
}
return target[prop]
},
}) |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I may be creating an X-Y problem for myself, but the scenario I'm looking for is "use data if it exists, otherwise fetch it" (with a manual override, like
refresh()
). This works well for fields with known names likeuser
:However, it's quite common to have collections keyed by ids. If I were setting up the
Proxy
myself for that, I would do something like:I read through the docs and README, but didn't find anything that seemed adequate for facilitating this; my apologies if this already exists.
Beta Was this translation helpful? Give feedback.
All reactions