mobx-sync icon indicating copy to clipboard operation
mobx-sync copied to clipboard

Persist Multiple Stores?

Open odesey opened this issue 4 years ago • 0 comments

I am attempting to utilize your package to persist multiple mobx stores as per the example here and have my stores setup in a similar fashion to this:

/*   RootStore.js  */
import {UserStore, TodoStore } from './index.js'
import { AsyncTrunk } from 'mobx-sync';
import AsyncStorage from '@react-native-community/async-storage';

class RootStore {
    constructor() {
        this.userStore = new UserStore()
        this.todoStore = new TodoStore()
    }
}

const rootStore = new RootStore();

const trunk = new AsyncTrunk(rootStore, {
  storageKey: 'myAppStore',
  storage: AsyncStorage,
  onError: error => console.error('TRUNK ERROR', error)
});

export { rootStore, RootStore, trunk };

/*  UserStore.js  */
import {rootStore} from './RootStore.js'

class UserStore {
    constructor(rootStore) {
        this.rootStore = rootStore
    }

    getTodos(user) {
        // access todoStore through the root store
        return this.rootStore.todoStore.todos.filter((todo) => todo.author === user)
    }
}

/*  TodoStore.js */
import {rootStore} from './RootStore.js'

class TodoStore {
    @observable todos = []

    constructor(rootStore) {
        this.rootStore = rootStore
    }
}

I then call the following function to hydrate the stores on app start:

const initApp = () => {
  trunk.init().then(() => {
    console.log('hydrate done', rootStore.userStore); //always undefined
  }
}

rootStore.userStore is always undefined, how can I persist multiple stores using this package?

Thanks.

mobx: 5.15.4 react-native: 0.61.5 mobx-sync: 3.0.0 @react-native-community/async-storage: 1.11.0

odesey avatar Jul 26 '20 05:07 odesey