-
-
Notifications
You must be signed in to change notification settings - Fork 253
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
proxyWithComputed and subscribeKey #350
Comments
Good point. Does |
Yes, subscribe works and subscribeKey works with the regular properties,
just not the computed ones
…On Wed, 2 Feb 2022, 14:56 Daishi Kato, ***@***.***> wrote:
Good point. Does subscribe work as expected with proxyWithComputed?
—
Reply to this email directly, view it on GitHub
<#350 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEP4NB2Y2GPA22ZNU6KTRTUZFA3JANCNFSM5NMIPSPQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Ah, now I get it. We used to have a different implementation, which is changed in #177. export const subscribeKey = <T extends object>(
proxyObject: T,
key: keyof T,
callback: (value: T[typeof key]) => void,
notifyInSync?: boolean
) => {
let prevValue = proxyObject[key]
return subscribe(
proxyObject,
() => {
const nextValue = proxyObject[key]
if (!Object.is(prevValue, nextValue)) {
callback((prevValue = nextValue))
}
},
notifyInSync
)
} The old one passes your test. I wonder if we should revert it, or accept the limitation (the |
Can you clarify why the new version isn't compatible? |
The new version of |
I leave it to you guys to decide which version you want, but I can explain I have a specific use case which I was trying to implement which lead to finding this issue. Essentially I have two calculated properties, one which triggers heavy updating of the UI when it changes and another which triggers a lighter update. They depend on different properties of the object, so update at different times. In order to make the UI responsive, I need to be able to detect the changes to the two calculated properties independently. I have also tried with derived, but came across #349 . I'm sure I can figure out a workaround, but |
Implementation-wise, I would like to keep the newer version, because the older version can easily be implemented by a user. So, in your use case, what is the real problem with If that really needs it, can you define this on your end? const subscribeKey = <T extends object, K extends keyof T>(
proxyObject: T,
key: K,
callback: (value: T[K]) => void
) => {
let prevValue = proxyObject[key]
return subscribe(
proxyObject,
() => {
const nextValue = proxyObject[key]
if (prevValue !== nextValue) {
callback(prevValue = nextValue)
}
}
)
} |
Okay, that will work. Thanks |
I will leave the current implementation just because it's harder for library users to implement (which uses I think we should cover this limitation and workaround in docs eventually, but closing this for now. |
This is definitely a bug, given that IMO, from the output of the proxy creation, whether a key is calculated or not should be irrelevant at the consumption side. If I had a dilemma of which one of competing implementations to keep in the lib as default, I'd definitely set my preference along the feature/capability aspect, rather than whether how hard users could potentially re-implement them. Users hardly care about that. On the other side, I, as a developer in a team who's responsible for recommending coding practices, have a much harder case to justify this state library when they see that I have to locally patch the lib. I understand the technical concern. Isn't there a way to include the old one as well in the lib? I would leave the old one, and add the new one under an experimental import namespace with a dumbed down typing on the key. |
Totally valid comment. Thanks for raising it again. Hm, yeah, I think we can revert to the original one, as new one doesn't provide any capabilities. |
Should we be able to use subscribeKey on a computed value? This test currently fails
The text was updated successfully, but these errors were encountered: