rt2zz / redux-persist-transform-immutable

immutable support for redux-persist
112 stars 42 forks source link

List to Map, after rehydrate. #22

Open cbjs opened 7 years ago

cbjs commented 7 years ago

redux-persist: 4.6.0 redux-persist-transform-immutable: 4.2.0 immutable: 3.8.1

1

everything is ok, with react-native@0.43.4, it happens when upgrading to react-native@0.44.0

tdurand commented 7 years ago

I have the same pb, did you find a workaround ?

cbjs commented 7 years ago

no. I write a custom transformer, to fix this issue temporarily.

tdurand commented 7 years ago

Ok, well if you wanna share it after writing it it's welcome, I'll keep RN 0.43.4 for now.

cbjs commented 7 years ago

not an efficient one. @tdurand

import { createTransform } from 'redux-persist'

export const createFixTransform = () => {

  function inbound (state, key) {
    return state;
  }

  function _isList(value) {
    if (!value.keySeq) return false;

    let res = true, sum = 0, ksum = 0;
    value.keySeq().forEach((v, k) => {
      if (typeof v !== 'number') {
        res = false;
        return false;
      }
      sum += v;
      ksum += k;
      return res;
    })

    return res && sum == ksum;
  }

  function _traverse(value) {
    if (value && value.map) {
      let newValue = value.map((v) => _traverse(v));
      if (_isList(newValue)) return newValue.toList();
      return newValue;
    }

    return value;
  }

  function outbound (state, key) {
    if (!state) return state;
    return _traverse(state);
  }

  return createTransform(inbound, outbound);
};

    persistStore(store, {
      storage: AsyncStorage,
      transforms: [createFixTransform(), immutableTransform()]
    });
tdurand commented 7 years ago

Thanks !

rt2zz commented 7 years ago

that is curious. If you figure out an alternative please post. Perhaps updating deps across the board will help.

johanmic commented 7 years ago

bumped everything to latest versions and having the same issue

original: ["email", "facebook"] after REHYDRATE: {0: "email", 1: "facebook"}

"redux-persist": "^4.8.0",
"redux-persist-transform-immutable": "^4.3.0",
"react": "16.0.0-alpha.6",
"react-native": "^0.44.0",
"immutable": "^3.8.1",
jeffberry commented 7 years ago

I had this same problem, I ended up figuring out it was because react native had it's own instance of immutable installed in node_modules/react-native/node_modules/immutable. I was using a different version of Immutable in my app's package.json; once I switched that version to the same version react-native requires (~3.7.6) the second installation in the react-native folder went away, and my issues were fixed. Thought this might be helpful to some.

giautm commented 7 years ago

Same here. :(

turnerhayes commented 7 years ago

For some reason, the first time the transit-js marshaller encounters an immutable List, it has a write handler registered to it, but subsequent encounters, the List constructor is a different one from the initial one, so it has no handler and uses the default handler, which is to treat it as a Map. I haven't figured out why the List constructor that gets returned is different the subsequent times, though (if I compare the constructor to Immutable.List, it's equal the first time and not equal subsequent times). This may be a bug with the transit-immutable-js library.

rt2zz commented 7 years ago

this issue will likely be resolved by switching serialize libs https://github.com/rt2zz/redux-persist-transform-immutable/pull/25

Rolling this out however is non trivial since it will break backwards compat. We may need to ship it as a new module.

turnerhayes commented 7 years ago

Update on my issue; I think the problem was with my setup; I was using an Immutable record from another library (that I own), and that library had immutable as a dependency, so it was creating immutable collections with its own instance of Immutable. The app I was using it from had its own instance of immutable, so there were two instances of Immutable. I changed my library to have immutable as a peerDependency instead and it worked. For the record, it worked with both serialize libs.

fabriziomoscon commented 7 years ago

@turnerhayes thank you, this solved the issue for us as well. We prefer using peerDependency over switching serialize lib, because in this way we ensure that we use the same version of Immutable throughout our code and libraries used.