robwalkerco / redux-persist-filesystem-storage

Redux persist adaptor for React Native filesystem storage
MIT License
196 stars 71 forks source link

Restore with AsyncStorage using redux-offline - looking for sample #26

Closed tobob closed 6 years ago

tobob commented 6 years ago

does someone use redux-offline with redux-persist-filesystem-storage and have sample of rehydrate from AsyncStorage as there is for pure persist -> https://github.com/robwalkerco/redux-persist-filesystem-storage#migration-from-previous-storage

tobob commented 6 years ago
  persistCallback: async (fsError, fsResult) => {
    if (_.isEmpty(fsResult)) {
      // if state from fs storage is empty try to read state from previous storage
      try {
        const asyncState = await getStoredState({ storage: AsyncStorage });
        if (!_.isEmpty(asyncState)) {
          // if data exists in `AsyncStorage` - rehydrate fs persistor with it
          // fsPersistor.rehydrate(asyncState, { serial: false }); <--- we can't access persistor from this place
        }
      } catch (getStateError) {
        console.warn('getStoredState error', getStateError);
      }
    }
  },
tobob commented 6 years ago

ok, I did turn off redux-persist for redux-offline (persist: null) and I did configure it manually in store configuration

zommerberg commented 4 years ago

@tobob Hello, can you give a sample of how you did that? Thanks!

tobob commented 4 years ago

@zommerberg I have an old version of @redux-offline/redux-offline (2.5.2-native.1), and redux-persist (4.5.0)

I think that in the new version it can work a bit different

import { autoRehydrate, persistStore, getStoredState } from 'redux-persist';
import reducer from './reducers';
import { createStore, applyMiddleware, compose } from 'redux';
import FilesystemStorage from 'redux-persist-filesystem-storage';
import AsyncStorage from '@react-native-community/async-storage';

const reduxOfflineConfig =  { persist: null };

const configureStore = () => {

  const middlewaresToApply = [];
  const store = createStore(
    reducer,
    compose(applyMiddleware(...middlewaresToApply), autoRehydrate()),
    offline(reduxOfflineConfig),
  );

  const fsPersistor = persistStore(
    store,
    {
      blacklist: ['form'],
      debounce: 500,
      storage: FilesystemStorage,
    },
    async (fsError, fsResult) => {
      if (_.isEmpty(fsResult)) {
        try {
          const asyncState = await getStoredState({ storage: AsyncStorage });
          if (!_.isEmpty(asyncState)) {
            fsPersistor.rehydrate(asyncState, { serial: false });
          }
        } catch (getStateError) {
          console.warn('getStoredState error', getStateError);
        }
      }
    },
  );
  return store;
};