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

Creating a standalone ref/computed properties inside setup. #2057

Closed
an-sh opened this issue Sep 5, 2020 · 4 comments
Closed

Creating a standalone ref/computed properties inside setup. #2057

an-sh opened this issue Sep 5, 2020 · 4 comments

Comments

@an-sh
Copy link

an-sh commented Sep 5, 2020

Version

3.0.0-rc.10

Reproduction link

https://github.com/an-sh/standalone-computed-test

Steps to reproduce

Create a singleton class with ref and computed properties inside component's setup.

Use a singleton instance in a component that goes through several mount/unmount cycles.

What is expected?

ref and computed properties should behave the same way

What is actually happening?

Ref property stays reactive for all component instances , however a computed property will stop being reactive after the first component instance is unmounted.


Not sure what exactly should happen, it is easy to assume that anything created during a setup call should be coupled with a components life-cycle. But i don't see any options to enforce ref/computed values to be indefinitely global or be disposed at a certain point outside of components logic (like rxjs unsubscribe).

@HcySunYang
Copy link
Member

Currently, there is a binding relationship between the computed and component instance, so that computed's effect will be stopped when the component is unmounted. The singleton makes computed's effect never activate again after being stopped, maybe we need to add an option for computed(), such as global:

export interface WritableComputedOptions<T> {
  get: ComputedGetter<T>
  set: ComputedSetter<T>
  global?: boolean
}

It's false by default, when true, the users can stop it manually:

const doubleCount = computed({
  get: () => counter.value * 2,
  global: true
})
// stop it
stop(doubleCount.effect) // or directly compouted `stop(doubleCount)`

Or, we provide a globalComputed() function:

const doubleCount = globalComputed(() => counter.value * 2)
stop(doubleCount.effect)  // or directly compouted `stop(doubleCount)`

@posva
Copy link
Member

posva commented Sep 6, 2020

Duplicate of #1532

@posva posva marked this as a duplicate of #1532 Sep 6, 2020
@posva posva closed this as completed Sep 6, 2020
@posva
Copy link
Member

posva commented Sep 6, 2020

@HcySunYang I think it's worth posting that comment on the other issue. It has useful information.

@an-sh
Copy link
Author

an-sh commented Sep 6, 2020 via email

@github-actions github-actions bot locked and limited conversation to collaborators Nov 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants