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

Add support for keepAlive #1949

Open
1 of 2 tasks
weiwei-lin opened this issue Aug 23, 2022 · 3 comments
Open
1 of 2 tasks

Add support for keepAlive #1949

weiwei-lin opened this issue Aug 23, 2022 · 3 comments
Labels
enhancement Possible enhancement help/PR welcome Help/Pull request from contributors to fix the issue is welcome level: intermediate

Comments

@weiwei-lin
Copy link

Feature request
Support equivalent of @computed({keepAlive: true}).
Note that keepAlive from mobx-utils isn't the same. Since it causes the computed value to always be re-evaluated even there's no other observers.

Is your feature request related to a problem? Please describe.

The lack of computed support make the friction of migrating from MobX to MST a lot higher.
And given that MST use a tree-like structure for managing states, arguably the chance of memory leak is even lower.

Are you willing to (attempt) a PR?

  • Yes
  • No
@coolsoftwaretyler
Copy link
Collaborator

@weiwei-lin - kind of like I mentioned in #1950 - are you still having trouble with this/interested in opening a PR? I don't know if I understand all the implications of this feature request, but I'd be happy to talk with you about it if it's still on your mind all this time later.

@coolsoftwaretyler coolsoftwaretyler added enhancement Possible enhancement help/PR welcome Help/Pull request from contributors to fix the issue is welcome level: intermediate labels Jun 25, 2023
@cmdcolin
Copy link
Contributor

cmdcolin commented Aug 14, 2024

i ran into a similar issue that i saw in a data intensive app that uses mobx-state-tree that i found. the full "scenario" that i was seeing was:

  1. while scrolling the app, it was unexpectedly slow (there were very noticeably scroll jankyness)
  2. when i did performance tracing, i saw it was calling a MST getter (e.g. "get mySlowMethod() {...}") repeatedly during the scrolling stuttering
  3. i thought, odd....why is that
  4. i added a trace() statement in the getter (via import {trace} from 'mobx')
  5. the trace() statement repeatedly was printing a statement like "'mySlowMethod' was suspended and it will recompute on the next access." during the scrolling stuttering
  6. i fixed it by adding a blank autorun that is a blank "usage" of the observer
  .views((self) => ({
    get mySlowMethod() {
      /* this is a heavy computation or is slow */
    },
  }))
  .actions((self) => ({
    afterAttach() {
      addDisposer(
        self,
        autorun(() => {
          console.log(self.mySlowMethod); // this simple method of making sure to access self.mySlowMethod keeps the slow getter alive. you can alternatively not console.log it and do something else with it if needed, just make sure to access it in an autorun
        }),
      );
    },
  }));

i posted about it here also
mobxjs/mobx#3911

@coolsoftwaretyler
Copy link
Collaborator

@cmdcolin - thank you so much! That's very useful insights. Also somewhat related to #2161, where the whole "observe it even if you don't do something with it" workaround keeps a computed value around.

I like this idea a lot. I will keep it labeled as help/PR welcome, because I'd welcome a PR implementing something like this. Although I do wonder if that's something more appropriate in the MobX side of things. Maybe both projects trying an implementation will lead to a best case scenario.

Really appreciate your time, and I'd welcome anyone reading this to reach out if they want to try implementing it. I am always happy to get people started contributing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Possible enhancement help/PR welcome Help/Pull request from contributors to fix the issue is welcome level: intermediate
Projects
None yet
Development

No branches or pull requests

3 participants