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

[New Feature]是不是可以增加一个即用即创建的功能 #24

Closed
SoonIter opened this issue Jul 25, 2022 · 6 comments · May be fixed by #33
Closed

[New Feature]是不是可以增加一个即用即创建的功能 #24

SoonIter opened this issue Jul 25, 2022 · 6 comments · May be fixed by #33
Labels

Comments

@SoonIter
Copy link

Pinia在这方面的处理是,只有 需要使用model的组件 mount时,才会创建对应的model,onInit才会触发。
如果使用较多的model,在根部一开始就创建,直接占用了较高的内存。
假设有10个Model,现在App一启动,10个onInit同时都会触发。

即用即创建,由于用户视口有限,同时在线的model的数量是很少的。

我的想法是getModel(userModel).state.name,可以替代userModel.state.name
在getModel、useModel 时,同步创建对应的store。

@SoonIter
Copy link
Author

如果是这样的话,initialState也要改为

initialState(){ 
   return {
          name:"",
          age:1
   }
}

@geekact
Copy link
Member

geekact commented Jul 26, 2022

模型多的情况下确实可能出现这个问题。尤其是在RN这种把所有JS打包在一起的。

对于webpack或者vite打包的web项目则不存在这个问题,因为模型本身就是模块化的,支持代码分割,即自动分配到对应页面的chunk里了。

即用即创建,由于用户视口有限,同时在线的model的数量是很少的。

这是个很不错的建议,感谢你的付出。实现上我觉得应该让开发者无感知,getModel(userModel).state.name这种方式太繁琐,所有地方都要加,增加了开发者的心智负担。我觉得可以借鉴linux Copy on Write的思路,我们实现一种Register on Read的机制,即在部分属性入口自动判断是否需要注册。

@geekact
Copy link
Member

geekact commented Jul 27, 2022

最近在完善node框架,所以应该会到9月才会开始升级

geekact added a commit that referenced this issue Dec 6, 2022
@geekact
Copy link
Member

geekact commented Dec 6, 2022

昨晚实现了按需初始化模型的功能并发布了 1.3.0-next.0 版本,3342275 ,用户无感知,但仍需多测试一下,欢迎给予意见建议

@geekact
Copy link
Member

geekact commented Dec 6, 2022

大概思路是在一些入口增加逻辑判断,比如执行actions(reducers)、获取state、使用useModel等。

@geekact
Copy link
Member

geekact commented Oct 20, 2023

如今有了更好的选择,局部模型。 https://foca.js.org/#/advanced?id=%e5%b1%80%e9%83%a8%e6%a8%a1%e5%9e%8b

@geekact geekact closed this as completed Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants