-
Notifications
You must be signed in to change notification settings - Fork 342
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
help/Feature request: iscomputed #445
Comments
The goal of The process for new features for composition api must go through |
Shure but in vue3 i can check if a ref is a computed ref in @vue/composition-api not. So this is a misalignment betwen both. So this is not a new feature but a request that in @vue/composition-api we can also do this. The isComputed helper is only a method I Need and I will implement it in my code but it is not posible to write such a helper. |
out of curiosity - what't the use case for isComputed? @mathe42 |
It is for nuxt-composition-api (to fix nuxt-community/composition-api#19). We have a method useFetch and if it is used we want to hydrate the component state (so the hole return of setup is injected into the SSR generated HTML and read by the client. So useFetch is called on initial load only on server side. If setup returns a computed ref and we can not detect that we try to set it and get an error (currently caught wit try, catch) |
Hey @mathe42, as If you really think this API is useful, maybe you can open an issue or create a PR to the rfcs repo first. We would love to ship it as long as it got supported by Vue 3. Thanks. |
So I dont think this is only internal. Or did I miss something? |
// expose effect so computed can be stopped
effect: runner, |
This comment is just to clarify what / why I expect some changes in @vue/composition-api = this package.
Vue 3In the docs for Vue3 https://v3.vuejs.org/api/computed-watch-api.html#computed computed is just a ref. This is not correct for this take a look on where computed is exported / re-exported: https://github.com/vuejs/vue-next/blob/master/packages/reactivity/src/computed.ts#L22 As you can see computed has the type export function computed<T>(getter: ComputedGetter<T>): ComputedRef<T>
export function computed<T>(
options: WritableComputedOptions<T>
): WritableComputedRef<T> And return types: export interface ComputedRef<T = any> extends WritableComputedRef<T> {
readonly value: T
}
export interface WritableComputedRef<T> extends Ref<T> {
readonly effect: ReactiveEffect<T>
} So when I call isComputedSo we can check in Vue3 if a ref is a computed ref @vue/composition-apiIf we take a look in the code https://github.com/vuejs/composition-api/blob/master/src/apis/computed.ts#L15 computed Refs are not different than normal refs (the "readonly" we can not detect in javascript this is just in the typescript typings) export interface ComputedRef<T = any> extends WritableComputedRef<T> {
readonly value: T
}
export interface WritableComputedRef<T> extends Ref<T> {} So if we have a ref we can check if it is a ref with What does that mean?There are multiple options
Yes there is a way to create an isComputed helperimport { isRef } from '@vue/composition-api'
export function isComputed(cmp: unknown): boolean {
return (
isRef(cmp) &&
Object.getOwnPropertyDescriptors(cmp)
.value.set!.toString()
.includes('Computed property was assigned')
)
} This is a hack because checking if the code of the setter contains a string is not a good way to handle this. If @vue/composition-api changes this code breaks. Why not an RFCThis is only a problem with @vue/composition-api and not with vue3. It's more like a hidden misalignment between both. |
This is not a misalignment.
As explained:
There's no misalignment if they are not even mention on the docs. Created #447 and still needs to go through an RFC, but if merged you can fix the original issue by checking |
If your PR's get approved (and merged) this would solve my problem. Thanks for all your work @pikax & all who work on this Repro :D. |
How can I Check if a ref is a computed ref? Need this for nuxt-composition-api...
In vue3 this is easy with @vue/Composition-API not. (
const isComputed = ref => !!ref.effect
) See also https://github.com/vuejs/vue-next/blob/master/packages/reactivity/src/computed.ts#L60The text was updated successfully, but these errors were encountered: