Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with rehydrating a tree structure in mobx-persist

I am having issues hydrating a store which contains a tree data structure consisting of TreeNode objects. I am using following library: https://github.com/pinqy520/mobx-persist

The problem is that the instances get hydrated as objects instead of TreeNodes. My guess is (after reading https://github.com/pinqy520/mobx-persist/issues/25) that I can't use a class definition for persist within the class itself, but I am not sure if this is really the cause.

Thats my TreeNode class:

export default class TreeNode {

    @persist id = Math.random();
    @persist @observable title = "Root";
    @persist @observable collapsed = false; // if node should be shown opened
    @persist('list', TreeNode) @observable childNodes  = []; // <- should this work?
    ...

Any hints wellcome!

like image 389
stoefln Avatar asked Nov 07 '22 22:11

stoefln


1 Answers

I think the problem is that the TreeNode class is not serializable and think that Mobx has the @serializable decorator. And as Self-referencing decorators work in Babel 5.x and Typescript so probably you can do as below:

@persist @serializable(list(object(TreeNode))) @observable childNodes = [];

or maybe without @persist, I have not tested

like image 91
Amir-Mousavi Avatar answered Dec 02 '22 02:12

Amir-Mousavi