Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is there a method for lazy loading (in the sense of reducing bundle size) the views, actions on a model?#2242

Unanswered
cmdcolin asked this question inGeneral
Discussion options

Some of our models have a lot of code in e.g. views and actions, and this results in large bundle sizes

However, the only thing that "really needs to be eagerly loaded" are the actual types.model type

Is there a way to lazily import the views, actions, and things like this?

Thanks :)

You must be logged in to vote

Replies: 2 comments 4 replies

Comment options

Setting up views and actions on a model is a method call itself, so you could theoretically defer the call to.views and.actions elsewhere.

Although depending on how you bundle things up, I don't think this will really improve your bundle size, since your code is still going to import them somewhere in the bundle. It may improve your app startup time if the models don't need those views and actions until later on at runtime.

You must be logged in to vote
3 replies
@cmdcolin
Comment options

We have a really big single page app, but there are entire subtrees of the mobx-state-tree that are unused by 99% of our users, so those would be good to lazy load

Setting up views and actions on a model is a method call itself, so you could theoretically defer the call to .views and .actions elsewhere.

trying to think of where that elsewhere could be... my kneejerk is like "could lazy loaded .views and .actions be chained onto an instance of a model"? not sure if that would be doable...can keep brainstorming but ideas/codesandbox welcome

@cmdcolin
Comment options

also just for fun, our app is open source. mobx-state-tree from the start :)https://jbrowse.org

@coolsoftwaretyler
Comment options

"could lazy loaded .views and .actions be chained onto an instance of a model"? not sure if that would be doable...can keep brainstorming but ideas/codesandbox welcome

Yup, this is precisely what I mean. You can chain any number of.props,.views,.actions, or.volatile calls onto a model at runtime. This still won't improve yourbundle size, but you could do this dynamically on certain routes so that the SPA itself doesn't pay the cost of instantiating models until you're certain one might be needed. So your runtime performance could be improved.

Comment options

would types.lazy apply?#2156

You must be logged in to vote
1 reply
@coolsoftwaretyler
Comment options

types.lazy will run all of the model instantiation lazily, but I don't think that's going to reduce your bundle size. The model definition will still exist.

This would improve your runtime performance so models don't go through their instantiation phase until after some asynchronous code has completed.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
2 participants
@cmdcolin@coolsoftwaretyler

[8]ページ先頭

©2009-2025 Movatter.jp