rt2zz / redux-persist-immutable

Redux Persist Immutable
MIT License
114 stars 31 forks source link

One of the sources for assign has an enumerable key on the prototype chain #5

Open meodemsao opened 8 years ago

meodemsao commented 8 years ago

i'm using redux-persist-immutable for react native when i only using redux-persist is ok but when use redux-persist-immutable i have issue

One of the sources for assign has an enumerable key on the prototype chain. This is an edge case that we do not support. This error is a performance optimization and not spec compliant

my reducer

export default { settings: settings };

and this my store creator

`const redux = require('redux'); const createStore = redux.createStore; const combineReducers = redux.combineReducers; const applyMiddleware = redux.applyMiddleware; const compose = redux.compose;

const thunk = require('redux-thunk').default; const createLogger = require('redux-logger'); const constants = require('redux-persist/constants'); const REHYDRATE = constants.REHYDRATE;

const ReactNative = require('react-native'); const AsyncStorage = ReactNative.AsyncStorage;

const createActionBuffer = require('redux-action-buffer');

// const reduxPersist = require('redux-persist'); // const persistStore = reduxPersist.persistStore; // const autoRehydrate = reduxPersist.autoRehydrate; const reduxPersistImmutable = require('redux-persist-immutable'); const persistStore = reduxPersistImmutable.persistStore; const autoRehydrate = reduxPersistImmutable.autoRehydrate;

const isDebuggingInChrome = DEV && !!window.navigator.userAgent;

const storeCreator = (reducers, options) => { console.log(reducers); if (process.env.NODE_ENV !== 'production') { if (!reducers || Object.keys(reducers).length === 0) { console.error( createStore does not have a valid reducers object argument. Make sure the first argument passed to createStore is an object whose values are reducers. ) } }

options = Object.create({ persistStore: {}, purgeKeys: [], purgeAll: false, logger: {}, }, options);

const logger = options.logger; const purgeAll = options.purgeAll; const purgeKeys = options.purgeKeys; const persistStoreOptions = Object.create( {storage: AsyncStorage}, options.persistStore ); const loggerOptions = Object.create({collapsed: () => true}, logger);

const store = createStore( combineReducers(reducers), undefined, compose( autoRehydrate(), applyMiddleware(thunk, createActionBuffer(REHYDRATE), createLogger(loggerOptions) ) ) );

if (purgeAll) { console.log('1'); persistStore(store, persistStoreOptions).purge() } else if (purgeKeys.length) { console.log('2'); persistStore(store, persistStoreOptions).purge(purgeKeys) } else { console.log('3'); persistStore(store, persistStoreOptions) } if (isDebuggingInChrome) { window.store = store; } console.log(store); return store };

module.exports = storeCreator;`

meodemsao commented 8 years ago

i fix this issue by this way, in operator.js file

function _stateIterator(state, callback) { const listState = Object.keys(state).map(key => state[key]); return listState.forEach(callback); }

function _stateGetter(state, key) { return fromJS(state).get(key); };

rt2zz commented 8 years ago

It looks like your top level state is a plain object not an immutable Map. redux-persist-immutable currently only supports top level state as a Map. In the near future we will add support for plain objects, but I would recommend in the meantime sticking with redux-persist + redux-persist-transform-immutable

meodemsao commented 8 years ago

my reducer

`const initState = fromJS({ nightShift: false, fontSize: 18, });

export default function settings(state = initState, action) { switch (action.type) { case settingsAction.INCREASE_FONT_SIZE: return state.set('fontSize', action.fontSize + 1); case settingsAction.DECREASE_FONT_SIZE: return state.set('fontSize', action.fontSize - 1); case settingsAction.TOGGLE_NIGHT_SHIFT: const nightShift = state.get('nightShift'); console.log(nightShift); return state.set('nightShift', !nightShift); default: return state } }

`

i also import your lib into my project to use this via es6

meodemsao commented 8 years ago

when i fix `function _stateIterator(state, callback) { const listState = Object.keys(state).map(key => state[key]); return listState.forEach(callback); }

function _stateGetter(state, key) { return fromJS(state).get(key); }; `

change in lib

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; to var _extends = function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; is run ok