Custom classes & tweaking issues #498
-
Hello @xaviergonz, While we absolutely love keystone (thanks again!) we are trying to reduce usage of it in place where we are running into memory constraints on mobile. We find that adding keystone to a class doubles the size of that object in v8 over a plain mobx class (i.e. a class using makeObservable). And we find that mobx classes are three times as big as a plain class. So we want to move to plain classes as much as possible whenever we are running into perf bottlenecks. By passing in a plain class to keystone we get the Error: I'm wondering if you support this kind of thing? Ideally we'd define a few functions that instruct keystone how to serialize/deserialize the class from json, and then keystone would assume that we'd handle reactivity etc for that class. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Does that class need to be serializable (fromSnapshot/getSnapshot support) or support for JSON patches and so on? Where I'm heading is that if that class is UI related rather than document related then maybe it doesn't need those capabilities and so it could be a mobx / plain class to save memory. If it is a document related model then probably it already needs all that and there's no way around it. Usually the pattern I find the best is having the "document model" exclusively made with mobx-keystone (and that way it is easier to serialize via fromSnapshot/getSnapshot) so it is not polluted with UI stuff, and then there's a UI model (probably made with mobx classes) that occasionally point model(s) within the document model. Also, an alternative that probably can be made without any changes to mobx-keystone is making your custom plain class "wrap around" a frozen value in the tree and make it so whenever something in your custom class changes then it will regenerate a plain JSON-compatible value that replaces the old frozen value. That way you get the custom class and the value is still useable by mobx-keystone. Assuming it is a document model (the architecture change doesn't solve your problem), or the frozen "trick" is not good enough, what you are asking is kind of like a "custom model" right? In order for it to fit into mobx-keystone it would need to support:
Do you think you'd be able to provide those from your custom model? (and if it's worth the effort) |
Beta Was this translation helpful? Give feedback.
Does that class need to be serializable (fromSnapshot/getSnapshot support) or support for JSON patches and so on?
Where I'm heading is that if that class is UI related rather than document related then maybe it doesn't need those capabilities and so it could be a mobx / plain class to save memory. If it is a document related model then probably it already needs all that and there's no way around it.
Usually the pattern I find the best is having the "document model" exclusively made with mobx-keystone (and that way it is easier to serialize via fromSnapshot/getSnapshot) so it is not polluted with UI stuff, and then there's a UI model (probably made with mobx classes) that occasionally poin…