redux-persist-transform-immutable icon indicating copy to clipboard operation
redux-persist-transform-immutable copied to clipboard

List to Map, after rehydrate.

Open cbjs opened this issue 7 years ago • 13 comments

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

1

everything is ok, with [email protected], it happens when upgrading to [email protected]

cbjs avatar May 02 '17 11:05 cbjs

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

tdurand avatar May 03 '17 15:05 tdurand

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

cbjs avatar May 03 '17 15:05 cbjs

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

tdurand avatar May 03 '17 15:05 tdurand

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()]
    });

cbjs avatar May 03 '17 15:05 cbjs

Thanks !

tdurand avatar May 03 '17 15:05 tdurand

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

rt2zz avatar May 04 '17 17:05 rt2zz

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",

johanmic avatar May 09 '17 12:05 johanmic

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.

jeffberry avatar May 28 '17 05:05 jeffberry

Same here. :(

giautm avatar Jun 15 '17 09:06 giautm

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.

turnerhayes avatar Oct 27 '17 00:10 turnerhayes

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.

rt2zz avatar Oct 27 '17 20:10 rt2zz

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.

turnerhayes avatar Oct 29 '17 22:10 turnerhayes

@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.

fabriziomoscon avatar Oct 30 '17 15:10 fabriziomoscon