rt2zz / redux-persist

persist and rehydrate a redux store
MIT License
12.97k stars 867 forks source link

Redux-persist v6 not flushing the state #1098

Open gemma-ferreras opened 5 years ago

gemma-ferreras commented 5 years ago

My state is not being persisted, the flush function is returning an array of null, instead of resolved promises.

This is are the packages i am using: redux: v4.0.4 react-redux: v7.1.1 redux-persist: v6.0.0 electron-store: v5.0.0

This is my reducers configuration:

const appReducer = (history) => combineReducers({
    user:       persistReducer(persistedConfsByKeys.user, userReducer),
    envs:       persistReducer(persistedConfsByKeys.envs, envs),
    router:     connectRouter(history),
});
const persistedConfsByKeys = Ru.map(persistConfig, persistedReducerByKeys);
const persistedReducerByKeys = {
    user: {
        key: 'user'
    },
    envs: {
        key:        'envs',
        throttle:   3
    },
    router: {
        key: 'router'
    }
};
const persistConfig = (spec) => {
    const { key } = spec;
    const runMigration = getMigrationRunnerFn(key);
    const electronStore = new ElectronStore();

    return {
        key:       'persist:'+key,
        version:   2,
        storage:   createElectronStorage({ electronStore }),
        serialize: false,
        debug:      true,
        migrate:   (undefinedState, version) => {
            const state = storeService.store.getState();
            const reducerState = state[key];

            if (Ru.isNil(state)) {
                return B.resolve(state)
            }

            const stateVersion = Ru.path(['_persist', 'version'], state);

            if (Ru.isNotNil(stateVersion) && stateVersion >= version) {
                return B.resolve(state);
            }
runMigration(reducerState))
            return (
                runMigration(reducerState)
                    .tap(newState => {
                        console.log('key', key)
                        console.log('newstate', newState)

                        newState = Ru.assocPath(['_persist', 'version'], version, newState);
                        electronStore.set('persist:'+key, newState);
                    })
            )
        }
    };
};

This is my store:

const history = createHashHistory({
    hashType:               'slash',
    getUserConfirmation:    (message, callback) => callback(window.confirm(message))
});

const initPersistentStore = (initialState, enhancer) => {
    const store = createStore(rootReducer(history), initialState, enhancer);
    return { store, persistedRootReducer: rootReducer };
};

// create store
const { store, persistedRootReducer } = initPersistentStore(initialState, enhancer);
import ElectronStore    from 'electron-store';

import rootSelectors    from 'reducers/selectors';
import persistor        from '../../../../app';

const setUserDataOnReduxStorage = (userId, data) => {
  const electronStore2save = new ElectronStore({
    name: `users/${userId}/config`
  });
  const userData2save = rootSelectors.userData(data);
  electronStore2save.set(userData2save);
};

const getUserDataFromReduxStorage = userId => {
  const electronStore = new ElectronStore({ name: `users/${userId}/config` });
  const result = electronStore.get();
  return result;
};

const forceUpdateReduxPersistance = () => {
    console.log('---------- persistor ----------', persistor)

    persistor.flush()
        .then(response => console.log('*********** response ************', response))

};

export {
    setUserDataOnReduxStorage,
    getUserDataFromReduxStorage,
    forceUpdateReduxPersistance
};

export default {
  setUserDataOnReduxStorage,
  getUserDataFromReduxStorage,
  forceUpdateReduxPersistance
};

Screenshot 2019-10-03 at 17 21 35

rikur commented 5 years ago

Yeah my app broke with v6, back to 5.x