Always serializes to Immutable.Map if multiple immutable libs are used
See original GitHub issueWe’ve spent quite some time debugging one very strange issue. The effect was in Immutable.List
being serialized and later de-serialized into Immutable.Map
.
I will try to explain the problem as short as possible and suggest a solution at the end.
Summary
instanceof
comparisons of transit-js
fails to compare Immutable types if we have multiple immutable
libraries used.
Explanation
We are using Lerna monorepo and all internal packages there are symlinked (the same effect can happen if you use npm link
without doing work in monorepo at all)
Bootstrapped working project structure:
packages
app
node_modules
immutable
->lib
lib
node_modules
immutable
As you can see symlinked lib
will also have its own immutable
like:
packages
app
node_modules
immutable
->lib
node_modules
immutable
lib
node_modules
immutable
As soon as we export anything Immutable in lib
and use it in app
, trying to serialize the state would result in fallback to the default
case of transit-immutable-js
:
"default", transit.makeWriteHandler({
tag: function() {
return 'iM';
}
The reason for that is transit-js
tries to do a match by using instanceof
and lib->Immutable.List
does not match app->Immutable.List
Possible solution
Instead of passing a mapping of Immutable.X
to transit-js
always use default
case and use native ImmutableJS methods like Immutable.List.isList(obj)
to do matching.
We checked that in the app’s code and Immutable.List.isList(obj)
works perfectly no matter what immutable
is used there.
We did not check this on transit-immutable-js
code yet. Need to see if it even possible (would be if transit-js
somehow gives obj
back to tag
function of writeHandler
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:9 (2 by maintainers)
Top GitHub Comments
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.I actually wrote my owt transit library about 200 lines long that replaces both this one and whole transit-js. It is not hard especially if you are not trying to solve a one-fits-all usecase (not opensourse since we don’t want to support generic cases and just keep it focused on our own data sttuctures)
I need to find some time soon to backport part of its code here to solve this issue. It is actually pretty straightforward